通过对二叉树的学习,我决定对以下创建和遍历的算法做一总结,首先,,递归很重要!!!递归很重要!!!!递归很重要!!!因为它的代码短呀。。。,当然能改为循环也重要。。。。本次不是很全面,,还有小部分没写,,不是因为我不会,,,就是因为我*********。。!!!!!!
第一次写的不好,,,看的人不许说我哟!!!!
{
if(ptr != NULL)
{
printf("%c ",ptr->data);
PreOrder(ptr->leftchild);
PreOrder(ptr->rightchild);
}
}
void InOrder(BtNode *ptr) //中序遍历
{
if(ptr != NULL)
{
InOrder(ptr->leftchild);
printf("%c ",ptr->data);
InOrder(ptr->rightchild);
}
}
void PastOrder(BtNode *ptr) //后序遍历
{
if(ptr != NULL)
{
PastOrder(ptr->leftchild);
PastOrder(ptr->rightchild);
printf("%c ",ptr->data);
}
}
BtNode * CreateTree1() //创建树
{
BtNode *s = NULL;
ElemType item;
scanf("%c",&item);
if(item != '#')
{
s = Buynode();
s->data = item;
s->leftchild = CreateTree1();
s->rightchild = CreateTree1();
}
return s;
}
BtNode *CreateTree2(char *&str)
{
BtNode *s = NULL;
if(str != NULL && *str != '#')
{
s = Buynode();
s->data = *str;
s->leftchild = CreateTree2(++str);
s->rightchild = CreateTree2(++str);
}
return s;
}
BtNode *CreateTree3(char * *const pstr)
{
BtNode *s = NULL;
if(pstr != NULL && *pstr != NULL && **pstr != '#')
{
s = Buynode();
s->data = **pstr;
s->leftchild = CreateTree3(&++(*pstr));
s->rightchild = CreateTree3(&++(*pstr));
}
}
int FindIs(char *is,int n,ElemType x)
{
for(int i = 0;i<n;++i)
{
if(is[i] == x)
return i;
}
return -1;
}
BtNode * Create(char *ps,char *is,int n) //根据前序和中序创建树
{
BtNode *s = NULL;
if(n > 0)
{
s = Buynode();
s->data = ps[0];
int pos = FindIs(is,n,ps[0]);
if(pos == -1) exit(1);
s->leftchild = Create(ps+1,is,pos);
s->rightchild = Create(ps+pos+1,is+pos+1,n-pos-1);
}
return s;
}
BtNode * CreatePI(char *ps,char *is)
{
if(ps == NULL || is == NULL )
{
return NULL;
}
else
{
int n = strlen(ps);
return Create(ps,is,n);
}
}
BtNode *Create(char *is,char *ls,int n)
{
}
BtNode *CreateIL(char *is,char *ls,int n)
{
if(is == NULL || ls == NULL || n<1)
{
return NULL;
}
else
{
return Create2(is,ls,n);
}
}
int Depth(BtNode *ptr) //深度
{
int m,n;
if (ptr==NULL)
{
return 0;
}
else
{
m = 1+Depth(ptr->leftchild);
n = 1+Depth(ptr->rightchild);
return m>n ? m:n;
}
}
int front=0,rear=1;
void LevelOrder(BtNode *ptr) //层序遍历
{
BtNode *q[100];
q[0]=ptr;
while(front<rear)
{
if(q[front])
{
printf("%c ",q[front]->data);
q[rear++]=q[front]->leftchild ;
q[rear++]=q[front]->rightchild ;
front++;
}
else
{
front++;
}
}
}
//BtNode *Findparent(BtNode *ptr,BtNode *child) //寻找双亲结点
{
if(ptr == NULL || child == NULL || ptr == child){
return NULL;
}
else
{
return Parent(ptr,child);
}
}
int Size(BtNode *ptr) //深度计算
{
if(ptr == NULL)
return 0;
else
{
return Size(ptr->leftchild)+Size(ptr->rightchild)+1;
}
}
int SizeLeaf(BtNode *ptr) //叶子结点
{
if(ptr == NULL)
{
return 0;
}
else if(ptr->leftchild == NULL && ptr->rightchild == NULL)
{
return 1;
}
else
{
return SizeLeaf(ptr->leftchild) + SizeLeaf(ptr->rightchild);
}
}
BtNode * FindValue(BtNode *ptr,ElemType x) //寻找某个结点
{
if(ptr == NULL || ptr->data == x)
{
return ptr;
}
else
{
BtNode *p = FindValue(ptr->leftchild,x);
if(NULL == p)
{
p = FindValue(ptr->rightchild,x);
}
return p;
}
}
int TwoDegreeCount(BTNode *root) //结点个数
{
int n=0;
if(root==NULL)
return 0;
if(root->left!=NULL && root->right!=NULL)
{
n=1;
}
int a=TwoDegreeCount(root->left) ;
int b=TwoDegreeCount(root->right);
int c=n+a+b;
return c;
}
bool Equal(BtNode *pa,BtNode *pb) //判等?
{
if(pa == NULL && pb == NULL)
{
return true;
}
else if(pa == NULL || pb == NULL)
{
return false;
}
return (Equal(pa->leftchild,pb->leftchild)&&
Equal(pa->rightchild,pb->rightchild));
}
void NiceInOrder(BtNode *ptr) //非递归中序
{
if(ptr == NULL)
return ;
Stack st;
Init_Stack(&st);
while(ptr != NULL || !Is_Empty(&st))
{
while(ptr != NULL)
{
push(&st,ptr);
ptr = ptr->leftchild;
}
ptr = gettop(&st);
pop(&st);
printf("%c ",ptr->data);
ptr = ptr->rightchild;
}
}
void NicePastOrder(BtNode *ptr) //非递归后序
{
if(ptr == NULL)
return ;
Stack st;
Init_Stack(&st);
BtNode *q = NULL;
while(ptr != NULL)
{
push(&st,ptr);
ptr = ptr->leftchild;
q = q->leftchild;
}
ptr = gettop(&st);
pop(&st);
if(ptr->rightchild == NULL || ptr->rightchild == tag )
{
printf("%c",ptr->data);
tag = ptr;
ptr == NULL;
}
else
{
push(&st,ptr);
ptr = ptr->rightchild;
}
}