1 #include<stdio.h>
2 #include<stdlib.h>
3 #define OK 1
4 #define TRUE 1
5 #define FALSE 0
6 #define ERROR -1
7 #define OVERFLOW -2
8 typedef int Status;
9 typedef int KeyType;
10 typedef int TElemType;
11 typedef struct BiTNode{
12 TElemType data;
13 BiTNode *lchild,*rchild;
14 }BiTNode,*BiTree;
15 Status SearchBST(BiTree T,KeyType key,BiTree f,BiTree &p)
16 {
17 if(!T) {p=f;return FALSE;}
18 else if(key==T->data) {p=T;return TRUE;}
19 else if(key<T->data) return SearchBST(T->lchild,key,T,p);
20 else return SearchBST(T->rchild,key,T,p);
21 }
22 Status InsertBST(BiTree &T,TElemType e)
23 {
24 BiTree p,s;
25 if(!SearchBST(T,e,NULL,p))
26 {
27 s=(BiTree)malloc(sizeof(BiTNode));
28 s->data=e;
29 s->lchild=s->rchild=NULL;
30 if(!p) T=s;
31 else if(e<p->data) p->lchild=s;
32 else p->rchild=s;
33 return TRUE;
34 }
35 else
36 {
37 puts("二叉排序树中已经存在该元素!");
38 return FALSE;
39 }
40 }
41 Status Delete(BiTree &p)
42 {
43 BiTree s,q;
44 if(!p->rchild)
45 {
46 q=p;p=p->lchild;
47 free(q);
48 }
49 else if(!p->lchild)
50 {
51 q=p;p=p->rchild;
52 free(q);
53 }
54 else
55 {
56 q=p;s=p->lchild;
57 while(s->rchild)
58 {
59 q=s;s=s->rchild;
60 }
61 p->data=s->data;
62 if(q!=p) q->rchild=s->lchild;
63 else q->lchild=s->lchild;
64 free(s);
65 }
66 return TRUE;
67 }
68 Status DeleteBST(BiTree &T,KeyType key)
69 {
70 if(!T) return FALSE;
71 else
72 {
73 if(key==T->data) return Delete(T);
74 else if(key<T->data) return DeleteBST(T->lchild,key);
75 else return DeleteBST(T->rchild,key);
76 }
77 }
78 Status CreateBiTree(BiTree &T)
79 {
80 int i,n;
81 TElemType treeElem[100];
82 T=NULL;
83 puts("请输入你要创建的二叉排序树的结点个数:");
84 scanf("%d",&n);
85 puts("请输入结点数值:");
86 for(i=0;i<n;i++)
87 {
88 scanf("%d",&treeElem[i]);
89 InsertBST(T,treeElem[i]);
90 }
91 return OK;
92 }
93 void PreOrderTraverse(BiTree T)
94 {
95 if(T)
96 {
97 printf("%d ",T->data);
98 PreOrderTraverse(T->lchild);
99 PreOrderTraverse(T->rchild);
100 }
101 }
102 void InOrderTraverse(BiTree T)
103 {
104 if(T)
105 {
106 InOrderTraverse(T->lchild);
107 printf("%d ",T->data);
108 InOrderTraverse(T->rchild);
109 }
110 }
111 void PostOrderTraverse(BiTree T)
112 {
113 if(T)
114 {
115 PostOrderTraverse(T->lchild);
116 PostOrderTraverse(T->rchild);
117 printf("%d ",T->data);
118 }
119 }
120 int CountLeaf(BiTree T)
121 {
122 int count=0;
123 if(T)
124 {
125 if(!(T->lchild||T->rchild))
126 count++;
127 count+=CountLeaf(T->lchild);
128 count+=CountLeaf(T->rchild);
129 }
130 return count;
131 }
132 int Depth(BiTree T)
133 {
134 int depthval;
135 int depthLeft,depthRight;
136 if(!T) depthval=0;
137 else
138 {
139 depthLeft=Depth(T->lchild);
140 depthRight=Depth(T->rchild);
141 depthval=1+(depthLeft>depthRight?depthLeft:depthRight);
142 }
143 return depthval;
144 }
145 Status PrintTreeInfo(BiTree T)
146 {
147 puts("当前排序二叉树为:");
148 puts("前序遍历排序二叉树:");
149 PreOrderTraverse(T);
150 puts("\n中序遍历排序二叉树:");
151 InOrderTraverse(T);
152 puts("\n后序遍历排序二叉树:");
153 PostOrderTraverse(T);
154 printf("\n排序二叉树的叶子个数为:%d\n",CountLeaf(T));
155 printf("排序二叉树的深度为:%d\n",Depth(T));
156 putchar('\n');
157 return OK;
158 }
159 Status DestroyBiTree(BiTree &T)
160 {
161 if(T)
162 {
163 DestroyBiTree(T->lchild);
164 DestroyBiTree(T->rchild);
165 delete T;
166 T=NULL;
167 }
168 return OK;
169 }
170 Status main()
171 {
172 BiTree T;
173 TElemType e;
174 CreateBiTree(T);
175 PrintTreeInfo(T);
176 puts("请输入你要插入的结点的值:");
177 scanf("%d",&e);
178 InsertBST(T,e);
179 PrintTreeInfo(T);
180 puts("请输入你要删除的结点的值:");
181 scanf("%d",&e);
182 DeleteBST(T,e);
183 PrintTreeInfo(T);
184 DestroyBiTree(T);
185 system("pause");
186 return OK;
187 }