4.3.1二叉树的遍历
function.h
#include
#include
typedef char BiElemType;
typedef struct BiTNode{
BiElemType c;
struct BiTNode *lchild;
struct BiTNode *rchild;
}BiTNode,*BiTree;
typedef struct tag{
BiTree p;
struct tag *pnext;
}tag_t,*ptag_t;
#define MaxSize 50
typedef BiTree ElemType;
typedef struct{
ElemType data[MaxSize];
int top;
}SqStack;
void InitStack(SqStack &S);
bool StackEmpty(SqStack &S);
bool Push(SqStack &S,ElemType x);
bool Pop(SqStack &S,ElemType &x);
bool GetTop(SqStack &S,ElemType &x);
typedef struct LinkNode{
ElemType data;
struct LinkNode *next;
}LinkNode;
typedef struct{
LinkNode *front,*rear;
}LinkQueue;
void InitQueue(LinkQueue &Q);
bool IsEmpty(LinkQueue Q);
void EnQueue(LinkQueue &Q,ElemType x);
bool DeQueue(LinkQueue &Q,ElemType &x);
stack.cpp
#include "function.h"
void InitStack(SqStack &S)
{
S.top=-1;
}
bool StackEmpty(SqStack &S)
{
if(S.top==-1)
return true;
else
return false;
}
bool Push(SqStack &S,ElemType x)
{
if(S.top==MaxSize-1)
{
return false;
}
S.data[++S.top]=x;
return true;
}
bool Pop(SqStack &S,ElemType &x)
{
if(-1==S.top)
return false;
x=S.data[S.top--];
return true;
}
bool GetTop(SqStack &S,ElemType &x)
{
if(-1==S.top)
return false;
x=S.data[S.top];
return true;
}
queue.cpp
#include "function.h"
void InitQueue(LinkQueue &Q)
{
Q.front=Q.rear=(LinkNode*)malloc(sizeof(LinkNode));
Q.front->next=NULL;
}
bool IsEmpty(LinkQueue Q)
{
if(Q.front==Q.rear)
return true;
else
return false;
}
void EnQueue(LinkQueue &Q,ElemType x)
{
LinkNode *s=(LinkNode *)malloc(sizeof(LinkNode));
s->data=x;s->next=NULL;
Q.rear->next=s;
Q.rear=s;
}
bool DeQueue(LinkQueue &Q,ElemType &x)
{
if(Q.front==Q.rear) return false;
LinkNode *p=Q.front->next;
x=p->data;
Q.front->next=p->next;
if(Q.rear==p)
Q.rear=Q.front;
free(p);
return true;
}
main.cpp
#include "function.h"
void preOrder(BiTree p)
{
if(p!=NULL)
{
putchar(p->c);
preOrder(p->lchild);
preOrder(p->rchild);
}
}
void InOrder(BiTree p)
{
if(p!=NULL)
{
InOrder(p->lchild);
putchar(p->c);
InOrder(p->rchild);
}
}
void PostOrder(BiTree p)
{
if(p!=NULL)
{
PostOrder(p->lchild);
PostOrder(p->rchild);
putchar(p->c);
}
}
void InOrder2(BiTree T)
{
SqStack S;
InitStack(S);BiTree p=T;
while(p||!StackEmpty(S))
{
if(p)
{
Push(S,p);
p=p->lchild;
}else{
Pop(S,p);putchar(p->c);
p=p->rchild;
}
}
}
void LevelOrder(BiTree T)
{
LinkQueue Q;
InitQueue(Q);
BiTree p;
EnQueue(Q,T);
while(!IsEmpty(Q))
{
DeQueue(Q,p);
putchar(p->c);
if(p->lchild!=NULL)
EnQueue(Q,p->lchild);
if(p->rchild!=NULL)
EnQueue(Q,p->rchild);
}
}
int main()
{
BiTree pnew;
int i,j,pos;
char c;
BiTree tree=NULL;
ptag_t phead=NULL,ptail=NULL,listpnew,pcur;
while(scanf("%c",&c)!=EOF)
{
if(c=='\n')
{
break;
}
pnew=(BiTree)calloc(1,sizeof(BiTNode));
pnew->c=c;
listpnew=(ptag_t)calloc(1,sizeof(tag_t));
listpnew->p=pnew;
if(NULL==tree)
{
tree=pnew;
phead=listpnew;
ptail=listpnew;
pcur=listpnew;
continue;
}else{
ptail->pnext=listpnew;
ptail=listpnew;
}
if(NULL==pcur->p->lchild)
{
pcur->p->lchild=pnew;
}else if(NULL==pcur->p->rchild)
{
pcur->p->rchild=pnew;
pcur=pcur->pnext;
}
}
printf("--------前序遍历----------\n");
preOrder(tree);
printf("\n--------中序遍历------------\n");
InOrder(tree);
printf("\n--------后序遍历------------\n");
PostOrder(tree);
printf("\n--------中序遍历非递归------\n");
InOrder2(tree);
printf("\n--------层次遍历-----------\n");
LevelOrder(tree);
printf("\n");
system("pause");
}
4.3.2线索二叉树
#include
#include
#include
typedef char ElemType;
typedef struct ThreadNode{
ElemType data;
struct ThreadNode *lchild,*rchild;
int ltag,rtag;
}ThreadNode,*ThreadTree;
void BulidThreadTree(ThreadTree &T)
{
ThreadTree arr[5];
int i;
for(i=0;i<5;i++)
{
arr[i]=(ThreadTree)malloc(sizeof(ThreadNode));
memset(arr[i],0,sizeof(ThreadNode));
arr[i]->data='A'+i;
}
arr[0]->lchild=arr[1];
arr[0]->rchild=arr[2];
arr[1]->rchild=arr[3];
arr[2]->lchild=arr[4];
T=arr[0];
}
void InThread(ThreadTree &p,ThreadTree &pre)
{
if(p!=NULL){
InThread(p->lchild,pre);
if(p->lchild==NULL){
p->lchild=pre;
p->ltag=1;
}
if(pre!=NULL&&pre->rchild==NULL){
pre->rchild=p;
pre->rtag=1;
}
pre=p;
InThread(p->rchild,pre);
}
}
void CreateInThread(ThreadTree T)
{
ThreadTree pre=NULL;
if(T!=NULL){
InThread(T,pre);
pre->rchild=NULL;
pre->rtag=1;
}
}
ThreadNode *Firstnode(ThreadNode *p)
{
while(p->ltag==0)
p=p->lchild;
return p;
}
int main()
{
ThreadTree T;
ThreadTree p;
BulidThreadTree(T);
CreateInThread(T);
p=Firstnode(T);
printf("最左下结点值为 %c\n",p->data);
system("pause");
}
4.5二叉排序列
#include
#include
typedef int KeyType;
typedef struct BSTNode{
KeyType key;
struct BSTNode *lchild,*rchild;
}BSTNode,*BiTree;
int BST_Insert(BiTree &T,KeyType k)
{
if(NULL==T)
{
T=(BiTree)malloc(sizeof(BSTNode));
T->key=k;
T->lchild=T->rchild=NULL;
return 1;
}
else if(k==T->key)
return 0;
else if(k<T->key)
return BST_Insert(T->lchild,k);
else
return BST_Insert(T->rchild,k);
}
void Creat_BST(BiTree &T,KeyType str[],int n)
{
T=NULL;
int i=0;
while(i<n)
{
BST_Insert(T,str[i]);
i++;
}
}
BSTNode *BST_Search(BiTree T,KeyType key,BiTree &p)
{
p=NULL;
while(T!=NULL&&key!=T->key)
{
p=T;
if(key<T->key) T=T->lchild;
else T=T->rchild;
}
return T;
}
void DeleteNode(BiTree &root,KeyType x){
if(root == NULL){
return;
}
if(root->key>x){
DeleteNode(root->lchild,x);
}else if(root->key<x){
DeleteNode(root->rchild,x);
}else{
if(root->lchild == NULL){
BiTree tempNode = root;
root = root->rchild;
free(tempNode);
}else if(root->rchild == NULL){
BiTree tempNode = root;
root = root->lchild;
free(tempNode);
}else{
BiTree tempNode = root->lchild;
if(tempNode->rchild!=NULL){
tempNode = tempNode->rchild;
}
root->key = tempNode->key;
DeleteNode(root->lchild,tempNode->key);
}
}
}
void InOrder(BiTree T)
{
if(T!=NULL)
{
InOrder(T->lchild);
printf("%3d",T->key);
InOrder(T->rchild);
}
}
int main()
{
BiTree T;
BiTree parent;
BiTree search;
KeyType str[]={54,20,66,40,28,79,58};
Creat_BST(T,str,7);
InOrder(T);
printf("\n");
search=BST_Search(T,40,parent);
if(search)
{
printf("找到对应结点,值=%d\n",search->key);
}else{
printf("未找到对应结点\n");
}
DeleteNode(T,66);
InOrder(T);
printf("\n");
system("pause");
}