二叉树基本操作C代码

  1 #include<stdio.h>  
  2 #include<malloc.h>  
  3 #define LEN sizeof(struct ChainTree)  
  4 struct ChainTree  
  5 {  
  6     int num;  
  7     struct ChainTree *left;  
  8     struct ChainTree *right;  
  9 };  
 10 /*函数功能:进行查找操作。*/  
 11 ChainTree *BinTreeFind(ChainTree *bt,int data)//在二叉树中查找值为data的结点。  
 12 {  
 13     ChainTree *p;  
 14     if(bt==NULL)  
 15         return NULL;  
 16     else  
 17     {  
 18         if(bt->num==data)  
 19         return bt;  
 20         else{  
 21             if(p=BinTreeFind(bt->left,data))  
 22                 return p;  
 23              else if(p=BinTreeFind(bt->right,data))  
 24                  return p;  
 25              else  
 26                  return NULL;  
 27           }  
 28     }  
 29 }  
 30 /*函数功能:先序遍历。*/  
 31 void BinTree_DLR(ChainTree *bt)  
 32 {  
 33     if(bt)  
 34     {  
 35         printf("%d",bt->num);  
 36         if(bt->left)  
 37         {  
 38             BinTree_DLR(bt->left);  
 39         }  
 40         if(bt->right)  
 41         {  
 42             BinTree_DLR(bt->right);  
 43         }  
 44     }  
 45     return;  
 46 }  
 47 /*函数定义:后序遍历。*/  
 48 void BinTree_LRD(ChainTree *bt)  
 49 {  
 50     if(bt)  
 51     {  
 52         BinTree_LRD(bt->left);  
 53         BinTree_LRD(bt->right);  
 54         printf("%d",bt->num);  
 55     }  
 56     return;  
 57 }  
 58 /*函数功能:中序遍历。*/  
 59 void BinTree_LDR(ChainTree *bt)  
 60 {  
 61     if(bt)  
 62     {  
 63         BinTree_LDR(bt->left);  
 64         printf("%d",bt->num);  
 65         BinTree_LDR(bt->right);  
 66     }  
 67     return;  
 68 }  
 69 /*函数功能初始化一个二叉树,建立根节点。*/  
 70 ChainTree *InitRoot()  
 71 {  
 72     ChainTree *node;  
 73     node=(struct ChainTree*)malloc(LEN);  
 74     printf("请输入根节点的值:");  
 75     scanf("%d",&node->num);  
 76     node->left=NULL;  
 77     node->right=NULL;  
 78     return node;  
 79 }  
 80   
 81 /*添加数据到二叉树*/  
 82 int BinTreeAddNode(ChainTree *bt,ChainTree *node,int n)  
 83 {  
 84     if(bt==NULL)  
 85     {  
 86         printf("\n父结点不在,请先设置父结点。");  
 87         return 0;  
 88     }  
 89     switch(n)  
 90     {  
 91         case 1:  
 92             if(bt->left)  
 93             {  
 94                 printf("左子树结点不为空。");  
 95                 return 0;  
 96             }  
 97             else  
 98                 bt->left=node;  
 99             break;  
100         case 2:  
101             if(bt->right)  
102             {  
103                 printf("右子树结点不为空。");  
104                 return 0;  
105             }  
106             else  
107                 bt->right=node;  
108             break;  
109             default:  
110                 printf("参数错误!\n");  
111             return 0;   
112     }  
113     return 1;  
114 }  
115   
116 /*函数功能:添加结点到二叉树。*/  
117 void AddNode(ChainTree *bt)  
118 {  
119     int data;  
120     int select;  
121     ChainTree *node,*parent;  
122     node=(struct ChainTree*)malloc(LEN);  
123     printf("\n输入二叉树结点数据");  
124     scanf("%d",&node->num);  
125     node->left=NULL;  
126     node->right=NULL;  
127     printf("\n输入父结点数据:");  
128     scanf("%d",&data);  
129     parent=BinTreeFind(bt,data);  
130     if(!parent)  
131     {  
132         printf("\n未找到父结点。");  
133     }  
134     printf("\n1.添加到左子树\n2.添加到右子树\n");  
135     do{  
136         scanf("%d",&select);  
137         if(select==1||select==2)  
138         BinTreeAddNode(parent,node,select);  
139         }while(select!=1&&select!=2);  
140 }  
141   
142 /*求二叉树叶子结点个数*/  
143 void CountLeaf(ChainTree *bt,int &count)  
144 {  
145     if(bt)  
146     {  
147         if((!bt->left)&&(!bt->right))  
148             count++;  
149         CountLeaf(bt->left,count);  
150         CountLeaf(bt->right,count);  
151     }  
152 }  
153   
154 /*求二叉树深度*/  
155 int BinTreeDepth(ChainTree *bt)  
156 {  
157     int dep1,dep2;  
158     if(bt==NULL)  
159     return 0;  
160     else  
161     {  
162         dep1=BinTreeDepth(bt->left);                //左子树深度(递归调用)  
163         dep2=BinTreeDepth(bt->right);              //右子树深度(递归调用)  
164         if(dep1>dep2)  
165             return dep1+1;  
166         else  
167             return dep2+1;  
168     }  
169 }  
170   
171 int main()  
172 {  
173     struct ChainTree *p1,*p2,*head,*p3;  
174     int select = 1000;  
175     int countleaf=0;                                        //该变量计算叶子结点个数  
176     while(select!=0){  
177         printf("\n1.设置二叉树根元素\n2.添加二叉树根节点\n3.先序遍历\n4.中序遍历\n5.后序遍历\n6.输出叶子结点个数\n7.求二叉树深度\n0.退出");  
178         scanf("%d",&select);  
179         switch(select)  
180         {  
181         case 1:  
182             head=InitRoot();  
183             break;  
184         case 2:  
185             AddNode(head);  
186             break;  
187         case 3:  
188             BinTree_DLR(head);  
189             break;  
190         case 4:  
191              BinTree_LDR(head);  
192              break;  
193         case 5:  
194             BinTree_LRD(head);  
195             break;  
196         case 6:  
197                countleaf=0;                                              //求二叉树叶子结点数  
198             CountLeaf(head,countleaf);  
199             printf("\n%d",countleaf);  
200             break;  
201         case 7:  
202             printf("二叉树深度为:%d\n",BinTreeDepth(head));  
203              break;  
204         case 0:  
205             select = 0;  
206             break;  
207         }  
208     }  
209 }  

 

posted on 2015-07-03 20:48  J·Marcus  阅读(1224)  评论(0编辑  收藏  举报

导航