Skip to content

Commit 402946d

Browse files
authored
Merge pull request #132 from FLYFLY-H/master
ok
2 parents ebc421d + d180df9 commit 402946d

File tree

4 files changed

+733
-0
lines changed

4 files changed

+733
-0
lines changed
Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
int flag ;
4+
5+
typedef enum
6+
{
7+
true,
8+
false
9+
}bool;
10+
11+
typedef enum status
12+
{
13+
OK,
14+
ERROR,
15+
OVERFLOW
16+
}Status;
17+
18+
typedef struct BiTNode
19+
{
20+
int data;
21+
struct BiTNode *lchild,*rchild;
22+
} BiTNode,*BiTree;
23+
24+
bool SearchBST(BiTree T,int key,BiTree f,BiTree *p)
25+
{
26+
if( !T )
27+
{
28+
*p = f;
29+
return false;
30+
}
31+
else
32+
{
33+
if(key == T->data)
34+
{
35+
p = &T;
36+
return true;
37+
}
38+
else if( key > T->data)
39+
{
40+
return SearchBST( T->rchild,key,T,p);
41+
}
42+
else
43+
{
44+
return SearchBST( T->lchild,key,T,p);
45+
}
46+
}
47+
};
48+
49+
50+
51+
//向二叉排序树插入一个新的结点
52+
bool InsertBST (BiTree *T,int e)
53+
{
54+
if( *T == NULL)
55+
{
56+
*T = (BiTree)malloc(sizeof(BiTNode));
57+
(*T)->data = e;
58+
(*T)->lchild = (*T)->rchild = NULL;;
59+
return true;
60+
}
61+
if(e == (*T)->data)
62+
{
63+
return false;
64+
}
65+
if(e < (*T)->data)
66+
{
67+
return InsertBST(&(*T)->lchild,e);
68+
}
69+
if(e > (*T)->data)
70+
{
71+
return InsertBST(&(*T)->rchild,e);
72+
}
73+
};
74+
75+
//建立BST
76+
Status CreatBST(BiTree *T,int a[],int n)
77+
{
78+
int i;
79+
*T = NULL;
80+
for(i = 0; i < n; i++)
81+
{
82+
InsertBST(&(*T),a[i]);
83+
}
84+
return OK;
85+
};
86+
87+
bool Delete(BiTree *T)
88+
{
89+
BiTree L,S;
90+
if(!(*T)->lchild && !(*T)->rchild)
91+
{
92+
*T = NULL;
93+
}
94+
else if(!(*T)->lchild)
95+
{
96+
L = *T;
97+
*T = (*T)->rchild;
98+
free(L);
99+
}
100+
else if(!(*T)->rchild)
101+
{
102+
L = *T;
103+
*T = (*T)->lchild;
104+
free(L);
105+
}
106+
else
107+
{
108+
L = *T;
109+
S = (*T)->lchild;
110+
while(S->rchild)
111+
{
112+
L = S;
113+
S = S->rchild;
114+
}
115+
(*T)->data = S->data;
116+
if( L != *T )
117+
{
118+
L->rchild = S->lchild;
119+
}
120+
else
121+
{
122+
L->lchild = S->lchild;
123+
}
124+
free(S);
125+
}
126+
return true;
127+
};
128+
129+
bool DeleteBST(BiTree *T,int key)
130+
{
131+
if(!T)
132+
{
133+
return false;
134+
}
135+
else
136+
{
137+
if(key == (*T)->data)
138+
{
139+
Delete(T);
140+
return true;
141+
}
142+
else if(key<(*T)->data)
143+
{
144+
return DeleteBST(&(*T)->lchild,key);
145+
}
146+
else
147+
{
148+
return DeleteBST(&(*T)->rchild,key);
149+
}
150+
}
151+
};
152+
153+
Status print(int data,FILE*pfile)
154+
{
155+
156+
char *d = ", ";
157+
if (NULL == pfile)
158+
{
159+
return ERROR;
160+
}
161+
if (NULL != pfile)
162+
{
163+
if (flag == 1)
164+
{
165+
fwrite(d, sizeof(d), 1, pfile);
166+
}
167+
fprintf(pfile, "%d",data);
168+
flag = 1;
169+
170+
}
171+
}
172+
173+
void preOrderTraverse(BiTree T,FILE*pfile)
174+
{
175+
if(T)
176+
{
177+
print(T->data,pfile);
178+
preOrderTraverse(T->lchild,pfile);
179+
preOrderTraverse(T->rchild,pfile);
180+
}
181+
};
182+
183+
int main()
184+
{
185+
int a[12] = { 8, 10, 14, 3, 1, 6, 4, 7, 5, 19, 22, 30 };
186+
int b[5] = { 13, 8, 5, 20, 6 };
187+
int n = 12,i;
188+
bool m;
189+
char d = '\n';
190+
FILE *pfile;
191+
BiTree T,f,p;
192+
T = (BiTree)malloc(sizeof(BiTNode));
193+
p = (BiTree)malloc(sizeof(BiTree));
194+
f = (BiTree)malloc(sizeof(BiTree));
195+
pfile = fopen("output.txt", "a");
196+
CreatBST(&T,a,n);
197+
preOrderTraverse(T,pfile);
198+
fwrite(&d, sizeof(d), 1, pfile);
199+
for(i = 0;i < 5;i++)
200+
{
201+
m = SearchBST(T,b[i],f,&p);
202+
//printf("现在查找%d: ",b[i]);
203+
if( m == true )
204+
{
205+
//printf("找到%d,在二叉排列树中删除%d\n",b[i],b[i]);
206+
DeleteBST(&T, b[i]);
207+
}
208+
else
209+
{
210+
//printf("未找到%d,在二叉排列树中插入%d\n",b[i],b[i]);
211+
InsertBST(&T,b[i]);
212+
}
213+
//printf("查找过%d的二叉排列树为:",b[i]);
214+
flag = 0;
215+
preOrderTraverse(T,pfile);
216+
fwrite(&d, sizeof(d), 1, pfile);
217+
}
218+
return 0;
219+
}
220+
221+
222+
223+
224+
225+
226+
227+
//BiTree p, s;
228+
//if( !SearchBST( *T, e, NULL, &p ) )
229+
//{
230+
// s = (BiTree)malloc(sizeof(BiTNode));
231+
// s->data = e;
232+
// s->lchild = s->rchild = NULL;
233+
// if( !p )
234+
// {
235+
// *T = s;/* 插入s为根节点,此前树为空树 */
236+
// }
237+
// else if( e > p->data )
238+
// {
239+
// p->rchild = s;/* 插入s为右孩子 */
240+
// }
241+
// else if( e < p->data)
242+
// {
243+
// p->lchild = s;/* 插入s为左孩子 */
244+
// }
245+
// return true;
246+
//}
247+
// else
248+
// {
249+
// return false;
250+
// }

2017-1/FlyFly-H/二叉排序树的基本操作/output.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)