《二叉树的基本操作(C语言、源代码)(共3页).doc》由会员分享,可在线阅读,更多相关《二叉树的基本操作(C语言、源代码)(共3页).doc(3页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上二叉树的基本操作(源代码)#include stdio.h#include malloc.h#define MAX 100typedef struct nodeint date;struct node *lchild,*rchild;bitnode,*bitree;bitree createbitree(bitree bt)int m;printf(m=);scanf(%d,&m);if(m=-1) bt=NULL;elsebt=(bitnode *)malloc(sizeof(bitnode);bt-date=m;bt-lchild=createbitree(bt-
2、lchild);bt-rchild=createbitree(bt-rchild);return bt;/树的建立void leveltraverse(bitree bt)bitree queueMAX,p=bt;int rear=0,front=0; if(p!=NULL)queue+rear=p;while(frontdate);if(p-lchild!=NULL)queue+rear=p-lchild;if(p-rchild!=NULL)queue+rear=p-rchild;/树的层次遍历void preordertraverse(bitree bt)if(bt!=NULL) prin
3、tf(%dt,bt-date); preordertraverse(bt-lchild); preordertraverse(bt-rchild);/树的先序遍历void inordertraverse(bitree bt)if(bt!=NULL) inordertraverse(bt-lchild); printf(%dt,bt-date); inordertraverse(bt-rchild);/树的中序遍历void postordertraverse(bitree bt)if(bt!=NULL) postordertraverse(bt-lchild); postordertravers
4、e(bt-rchild); printf(%dt,bt-date);/树的后序遍历int bitreedepth(bitree bt)int h,lh,rh;if(bt=NULL) h=0;elselh=bitreedepth(bt-lchild);rh=bitreedepth(bt-rchild);if(lh=rh)h=lh+1;elseh=rh+1;return h;/求树的深度int bitreeleaf(bitree bt)int m=0; if(!bt)return 0;else if(bt-lchild=NULL&bt-rchild=NULL)return 1; m+=bitree
5、leaf(bt-lchild)+bitreeleaf(bt-rchild);return m;/求树的叶子树int countbitreenode(bitree bt)if(!bt)return 0;elsereturn countbitreenode(bt-lchild)+countbitreenode(bt-rchild)+1;/求树的节点数void OperateBitree(bitree bt) printf(n);printf(The level traversal of the bitree is:nt);leveltraverse(bt);printf(nn);printf(Th
6、e preorder traversal of the bitree is:nt);preordertraverse(bt);printf(nn);printf(The inorder traversal of the bitree is:nt);inordertraverse(bt);printf(nn); printf(The postorder traversal of the bitree is:nt);postordertraverse(bt); printf(nn);printf(The number of the bitreenode is %d.n,countbitreenode(bt);printf(The depth of the bitree is %d.n,bitreedepth(bt);printf(The number of the leaves of the bitree is %d.nn,bitreeleaf(bt);/树的基本操作void main()bitree bt; bt=createbitree(bt); OperateBitree(bt);专心-专注-专业