---
title: "考研复习（7）树的基本操作"
author: "touchmm"
date: "2011-08-31T12:02:00Z"
tags: []
slug: "7"
draft: false
lastmod: "2011-10-24T15:28:13Z"
id: 1210716
url: "https://touchmm.iteye.com/blog/1210716"
---

这次是树的基本操作，包括生成树，求树深度，求叶子节点个数。。。  
附上二叉树的几个重要性质及证明（考研必考）。  
1.在二叉树的第i层至多有2^（i-1）个结点；  
2.深度为k的二叉树至多有2^(k)-1 个结点；  
3.对任何一棵二叉树T，如果其终端结点数为n0，度为2的结点数为n2，则n0=n2+1；（n=n0+n1+n2=n1+2n2+1）;  
4.具有n哥节点的完全二叉树深度为【log2n】+1；  
#include &lt;stdio.h&gt;  
#include&lt;stdlib.h&gt;  
#define maxSize 20  
#define maxWidth 20  
typedef struct node  
{  
char data;  
struct node \*left,\*right;  
}Btree;  
Btree* createNode(char ch,Btree \*l,Btree \*r);  
//先序遍历  
void preOrder(Btree \*BT)  
{  
if(BT!= NULL)  
{  
printf("%c",BT-&gt;data);  
preOrder(BT-&gt;left);  
preOrder(BT-&gt;right);  
}  
}  
//中序遍历  
void inOrder(Btree \*BT)  
{  
if(BT!= NULL)  
{  
inOrder(BT-&gt;left);  
printf("%c",BT-&gt;data);  
inOrder(BT-&gt;right);  
}  
}  
//后序遍历  
void postOrder(Btree \*BT)  
{  
if(BT!= NULL)  
{  
postOrder(BT-&gt;left);  
postOrder(BT-&gt;right);  
printf("%c",BT-&gt;data);  
}  
}  
//从括号表达式生成树  
void createTree(Btree \*\*BT,char \*str)//create a tree BT according by hollow function str  
{

Btree \*stack\[maxSize],\*p;  
int top=-1,k,j=0;//top is the point of stack,k is the tab of son,j is the point of str;  
char ch;  
\*BT=NULL;  
ch=str\[j];

while(ch!=NULL)  
{  
switch(ch)  
{  
case'(':top++;stack\[top]=p;k=1;//left son,push to stack  
break;  
case')':top--;//pop stack  
break;  
case ',':k=2;//right son  
break;  
default:  
/\*p=(Btree \*)malloc(sizeof(Btree));//set a node  
if(!(p))  
{  
exit(0);  
}  
p-&gt;data=ch;  
p-&gt;left=p-&gt;right=NULL;\*/  
p=createNode(ch,NULL,NULL);  
if(\*BT==NULL)//the root node  
{  
\*BT=p;  
}  
else  
{  
switch(k)  
{  
case 1:stack\[top]-&gt;left=p;  
break;  
case 2:stack\[top]-&gt;right=p;  
break;  
default:  
;  
}

}  
}  
j++;  
ch=str\[j];//get next char  
}  
}  
/*  
void createTree(Btree \*\*BT,char \*str)  
{  
int j=0;//top is the point of stack,k is the tab of son,j is the point of str;  
Btree \*T;  
char ch;  
ch=str\[j];  
if(ch=='') \*BT=NULL;  
else  
{  
if(!(T=))

}  
}  
\*/  
//生成节点  
Btree* createNode(char ch,Btree \*l,Btree \*r)  
{  
printf("Create now!\\n");  
Btree \*p;  
p=(Btree \*)malloc(sizeof(Btree));//set a node  
if(!(p))  
{  
exit(0);  
}  
p-&gt;data=ch;  
p-&gt;left=l;  
p-&gt;right=r;  
return p;  
}  
//复制树  
//copy a tree and return the new tree's point  
Btree* copyTree(Btree \*T)  
{

Btree \*newptl,\*newptr,\*newT;  
if(!T) return NULL;  
if(T-&gt;left!=NULL)  
{  
newptl=copyTree(T-&gt;left);

}  
else newptl=NULL;  
if(T-&gt;right!=NULL) newptr=copyTree(T-&gt;right);  
else newptr=NULL;  
newT=createNode(T-&gt;data,newptl,newptr);  
//newT=createNode(T-&gt;data,NULL,NULL);  
//newT=NULL;  
return newT;  
}  
//层次法打印树  
void disptree(Btree \*BT)  
{  
Btree \*stack\[maxSize],\*p;  
int level\[maxSize]\[2],top,n,i,width=4;  
if(BT!=NULL)  
{  
printf("Display a tree by hollow means.\\n");  
top=1;  
stack\[top]=BT;//push root point to stack.  
level\[top]\[0]=width;  
while(top&gt;0)  
{  
p=stack\[top];  
n=level\[top]\[0];  
for(i=1;i&lt;=n;i++)  
printf(" ");  
printf("%c",p-&gt;data);  
for(i=n+1;i&lt;maxWidth;i+=2)  
printf("--");  
printf("\\n");  
top--;  
if(p-&gt;right!=NULL)  
{  
top++;  
stack\[top]=p-&gt;right;  
level\[top]\[0]=n+width;  
level\[top]\[1]=2;  
}  
if(p-&gt;left!=NULL)  
{  
top++;  
stack\[top]=p-&gt;left;  
level\[top]\[0]=n+width;  
level\[top]\[1]=1;  
}  
}

}  
}  
int TreeDepth(Btree \*BT)//calculate the depth of tree  
{  
int leftDep,rightDep;  
if(BT==NULL) return 0;  
else  
{  
leftDep=TreeDepth(BT-&gt;left);  
rightDep=TreeDepth(BT-&gt;right);  
if(leftDep&gt;rightDep) return(leftDep+1);  
else return (rightDep+1);  
}  
}  
int NodeCount(Btree \*BT)//cout the nodes  
{  
if(BT==NULL) return 0;  
else return (NodeCount(BT-&gt;left)+NodeCount(BT-&gt;right)+1);  
}  
int LeafCount(Btree \*BT)//count the leafnodes  
{  
if(BT==NULL) return 0;  
else if(BT-&gt;left==NULL&&BT-&gt;right==NULL) return 1;  
else return (LeafCount(BT-&gt;left)+LeafCount(BT-&gt;right));  
}  
void PrintTree(Btree \*BT)//print tree  
{  
if(BT!=NULL)  
{  
printf("%c",BT-&gt;data);  
if(BT-&gt;left!=NULL||BT-&gt;right!=NULL)  
{  
printf("(");  
PrintTree(BT-&gt;left);  
if(BT-&gt;right!=NULL) printf(",");  
PrintTree(BT-&gt;right);  
printf(")");  
}  
}  
}  
main()  
{  
Btree \*B,\*C;  
char \*s="A(B(D,E(H,I)),C(G))";  
createTree(&B,s);  
printf("preOder:");  
preOrder(B);  
printf("\\n");  
printf("inOder:");  
inOrder(B);  
printf("\\n");  
printf("postOder:");  
postOrder(B);  
printf("\\n");  
printf("Display tree\\n");  
disptree(B);  
printf("copy tree\\n");  
//disptree(B);  
C=copyTree(B);  
preOrder(C);  
disptree(C);

printf("Deep: %d\\n",TreeDepth(B));  
printf("Sum of nodes: %d\\n",NodeCount(B));  
printf("Sum of leafnodes: %d\\n",LeafCount(B));  
PrintTree(B);  
printf("\\nHello,world!");  
}