二叉树的操作

来源:互联网 发布:linux vi 模式 编辑:程序博客网 时间:2024/06/05 22:30

//二叉树的操作---遍历、建立、查找、判断等价、求结点个数、求深度

//所有函数均以递归函数实现,学习递归算法内涵,上机进行调试。

#include<cstdio>

#include<queue>

using namespacestd;

typedef charElementType;

struct Node

{

       ElementType data;

       Node *lchild,*rchild;

};

Node*creat1()        //以先序方式建立二叉树(递归)

{

       Node* t;

       char ch;

       ch=getchar();

       if(ch==' ')

              t=NULL;

       else

       {

              t=new Node;

              t->data =ch;

              t->lchild =creat1();

              t->rchild =creat1();

       }

       return t;

}

Node*creat2()    //以层序方式建立二叉树(借助队列,非递归)

{

       char x;

       Node* t,*bt;

       queue<Node*> q;

       x = getchar();

       if(x == ' ')

              return NULL;

       else

       {

              bt = new Node;

              bt->data = x;

              q.push(bt);

       }

       while(!q.empty())

       {

              t = q.front();q.pop();

              x = getchar();

              if(x == ' ')

                     t->lchild = NULL;

              else

              {

                     t->lchild = new Node;

                     t->lchild->data = x;

                     q.push(t->lchild);

              }

              x = getchar();

              if(x == ' ')

                     t->rchild = NULL;

              else

              {

                     t->rchild = new Node;

                     t->rchild->data = x;

                     q.push(t->rchild);

              }

       }

       return bt;

}

voidpreorder(Node* t)  //前序遍历

{

       if(t)

       {

              printf("%c",t->data);

              preorder(t->lchild );

              preorder(t->rchild );

       }

}

void inorder(Node*t)  //中序遍历

{

       if(t)

       {

              inorder(t->lchild );

              printf("%c ",t->data);

              inorder(t->rchild);

       }

}

void postorder(Node*t)  //后序遍历

{

       if(t)

       {

              postorder(t->lchild );

              postorder(t->rchild );

              printf("%c ",t->data);

       }

}

Node* copy(Node*t1)  //二叉树的拷贝

{

       Node* newt;

       if(t1==NULL) return NULL;

       else

       {

              newt=new Node;

              newt->data = t1->data ;

              newt->lchild =copy(t1->lchild);  

              newt->rchild=copy(t1->rchild );

       }

       return newt;

}

intnumofnode(Node* t)  //求二叉树结点个数

{

       if(t==NULL)return 0;

       else

              return numofnode(t->lchild ) +numofnode(t->rchild ) + 1;

}

int depth(Node*t)  //求二叉树深度

{

       int lh,rh;

       if(t==NULL)return 0;

       else

       {

              lh = depth(t->lchild );

              rh = depth(t->rchild );

              return lh > rh ?  lh + 1 : rh + 1;

       }

}

int isequal(Node*t1,Node* t2)//判断二叉树是否等价

{

       int t;

       if(!t1 && !t2) return 1;

       else

       {

              if(t1 && t2)

                     if(t1->data==t2->data )

                            if(isequal(t1->lchild,t2->lchild ))

                                   t=isequal(t1->rchild,t2->rchild );

       }

       return t;

}

int main()

{

       Node* t1, *t2;

       t1=creat1();

       printf("先序遍历该二叉树:\n");

       preorder(t1);

       printf("中序遍历该二叉树:\n");

       inorder(t1);

       printf("后序遍历该二叉树:\n");

       postorder(t1);

       /*t2=copy(t1);   

       if(isequal(t1,t2))

              printf("等价\n");

       else

              printf("不等价\n");

       printf("\n二叉树中结点的个数:%d\n",numofnode(t1));

       printf("二叉树深度:%d\n",depth(t1));

       */

       return 0;

}

原创粉丝点击