二叉树建立、遍历(前序,中序,后序),求叶节点个数,求节点个数

来源:互联网 发布:马勒第六交响曲知乎 编辑:程序博客网 时间:2024/06/05 17:47

二叉树是笔试面试中考试最频繁的数据结构之一,主要包括,程序建立一个二叉树,三种次序遍历二叉树,返回叶子节点的数目,求二叉树节点的总数等。建立一个二叉树节点的数据结构

typedef struct Node
{
int data;
struct Node *left,*right;
}Node;,结构体内包括数据,左子树,又子树;

一、建立二叉树的程序代码如下

[cpp] view plaincopyprint?
  1. Node *CreatBTree() //建立二叉树  
  2. {  
  3.     Node *t;  
  4.     int x;  
  5.     scanf("%d",&x);  
  6.     if(x==0)  
  7.     {  
  8.         t=NULL;  
  9.     }  
  10.     else  
  11.     {  
  12.         t=(Node*)malloc(sizeof(Node));  
  13.         t->data=x;  
  14.         printf("\n请输入%d结点的左子结点:",t->data );  
  15.         t->left=CreatBTree();  
  16.         printf("\n请输入%d结点的右子结点:",t->data );  
  17.         t->right=CreatBTree();  
  18.     }  
  19.     return t;  
  20. }  

二、前序遍历二叉树

[cpp] view plaincopyprint?
  1. void preVisit(Node *T)  
  2. {  
  3.     if(T==NULL) return;  
  4.     else  
  5.     {  
  6.         printf("%3d",T->data);  
  7.         preVisit(T->left);  
  8.         preVisit(T->right);  
  9.     }  
  10. }  

三、中序遍历二叉树

[cpp] view plaincopyprint?
  1. void middVisit(Node *T)  
  2. {  
  3.     if(T==NULL) return;  
  4.     else  
  5.     {  
  6.         middVisit(T->left);  
  7.         printf("%3d",T->data);  
  8.         middVisit(T->right);  
  9.     }  
  10. }  

四、后序遍历二叉树

[cpp] view plaincopyprint?
  1. void lastVisit(Node *T)  
  2. {  
  3.     if(T==NULL) return;  
  4.     else  
  5.     {  
  6.         lastVisit(T->left);  
  7.         lastVisit(T->right);  
  8.         printf("%3d",T->data);  
  9.     }  
  10. }  
五、返回叶子节点数目

[cpp] view plaincopyprint?
  1. int leafnum(Node *T)  
  2. {  
  3.     if (!T)  
  4.     {  
  5.         return 0;  
  6.     }  
  7.     else if ((!T->left)&&(!T->right))  
  8.     {  
  9.         return 1;  
  10.     }  
  11.     else  
  12.     {  
  13.         return ((leafnum(T->left)+leafnum(T->right)));  
  14.     }  
  15. }  

六、返回节点总数目

[cpp] view plaincopyprint?
  1. int Nodenum(Node *T)  
  2. {  
  3.     if (T)  
  4.     {  
  5.         return 1+Nodenum(T->left)+Nodenum(T->right);  
  6.     }  
  7.     if (T==NULL)  
  8.     {  
  9.         return 0;  
  10.     }  
  11.   
  12. }  

七、测试程序;
[cpp] view plaincopyprint?
  1. int menu();  
  2. void main()  
  3. {  
  4.     Node *T=NULL;  
  5.     int choice;  
  6.     do{  
  7.         choice=menu();  
  8.         if(choice==1)  
  9.         {  
  10.             printf("二叉树的建立,以输入“0”表示结束:!\n");  
  11.             printf("请输入根结点:\n");  
  12.             T=CreatBTree();  
  13.             printf("二叉树成功建立");  
  14.         }  
  15.         else if(choice==2)  
  16.         {  
  17.             printf("先序遍历二叉树 :\n");  
  18.             preVisit(T);  
  19.         }  
  20.         else if(choice==3)  
  21.         {  
  22.             printf("中序遍历二叉树:\n");  
  23.             middVisit(T);  
  24.         }  
  25.         else if(choice==4)  
  26.         {  
  27.             printf("后序遍历二叉树 :\n ");  
  28.             lastVisit(T);  
  29.         }  
  30.         else if(choice==5)  
  31.         {  
  32.             int ct=10;  
  33.             ct=leafnum(T);  
  34.             printf(" 二叉树的叶子结点数为 : \n");  
  35.             printf("%d\n",ct);  
  36.               
  37.         }  
  38.         else if(choice==7)  
  39.         {  
  40.             int count=Nodenum(T);  
  41.             printf("该二叉树总共有%d个结点。\n",count);  
  42.         }  
  43.         else if(choice==8)  
  44.         exit(0);  
  45.     }while(choice<=8);  
  46. }  

[cpp] view plaincopyprint?
  1. int menu()  
  2. {  
  3.         int choice;  
  4.         printf("\n");  
  5.         printf(" 二叉树 \n");  
  6.         printf(" ***************************\n");  
  7.         printf(" * *\n");  
  8.         printf(" * 主菜单 *\n");  
  9.         printf(" * 1 建立二叉树 *\n");  
  10.         printf(" * 2 先序遍历二叉树 *\n");  
  11.         printf(" * 3 中序遍历二叉树 *\n");  
  12.         printf(" * 4 后序遍历二叉树 *\n");  
  13.         printf(" * 5 二叉树的叶子结点数 *\n");  
  14.         printf(" * 7 二叉树的所有结点数 *\n");  
  15.         printf(" * 8 退出程序运行 *\n");  
  16.         printf(" ****************************\n");  
  17.         printf(" 请输入您的选择(1,2,3,4,5,6,7,8): \n");  
  18.         scanf("%d",&choice);  
  19.         return choice;  
  20. }