Skip to content

Commit 0e44f2b

Browse files
authored
Create 二叉排序树与平衡二叉树的实现.cpp
1 parent e0ee4da commit 0e44f2b

File tree

1 file changed

+398
-0
lines changed

1 file changed

+398
-0
lines changed
Lines changed: 398 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,398 @@
1+
/* 实验七 二叉排序树与平衡二叉树的实现 */
2+
3+
/*变量名要用有意义的名字!!!
4+
*变量名要用有意义的名字!!!
5+
*变量名要用有意义的名字!!!*/
6+
#include<iostream>
7+
#include<cmath>
8+
using namespace std;
9+
10+
class Node
11+
{
12+
public:
13+
int elem;
14+
int height;
15+
Node *left;
16+
Node *right;
17+
Node(int _elem)
18+
{
19+
elem=_elem;
20+
left=NULL;
21+
right=NULL;
22+
height=0;
23+
}
24+
};
25+
26+
class BST
27+
{
28+
public:
29+
Node *root;
30+
BST()
31+
{
32+
root=NULL;
33+
}
34+
void insert(int n);
35+
void insert(Node *p,int n);
36+
Node* search(int n);
37+
Node* search(Node *p,int n);
38+
void inorder(Node *p);
39+
void asl(int num);
40+
Node* pop(Node *p,int n);
41+
Node* get_parent(Node *p);
42+
Node* findmin();
43+
};
44+
45+
class AVL:public BST
46+
{
47+
public:
48+
AVL()
49+
{
50+
root=NULL;
51+
}
52+
Node* insert(int n);
53+
Node* insert(Node *p,int n);
54+
int getheight(Node *p);
55+
int max(int a,int b);
56+
Node* ll(Node *p);
57+
Node* lr(Node *p);
58+
Node* rr(Node *p);
59+
Node* rl(Node *p);
60+
};
61+
62+
void BST::insert(int n)
63+
{
64+
if (root==NULL)
65+
{
66+
root=new Node(n);
67+
}
68+
else
69+
{
70+
insert(root,n);
71+
}
72+
}
73+
74+
void BST::insert(Node *p,int n)
75+
{
76+
if (n>p->elem)
77+
{
78+
if (p->right==NULL)
79+
{
80+
p->right=new Node(n);
81+
}
82+
else
83+
{
84+
insert(p->right,n);
85+
}
86+
}
87+
if (n<p->elem)
88+
{
89+
if (p->left==NULL)
90+
{
91+
p->left=new Node(n);
92+
}
93+
else
94+
{
95+
insert(p->left,n);
96+
}
97+
}
98+
}
99+
100+
Node* BST::get_parent(Node *p)
101+
{
102+
Node *q=root;
103+
if (p==root) return NULL;
104+
while (true)
105+
{
106+
if (q->right==p||q->left==p)
107+
{
108+
return q;
109+
}
110+
if (p->elem>q->elem)
111+
{
112+
q=q->right;
113+
}
114+
else
115+
{
116+
q=q->left;
117+
}
118+
}
119+
}
120+
121+
Node* BST::search(int n)
122+
{
123+
return search(root,n);
124+
}
125+
126+
Node* BST::search(Node *p,int n)
127+
{
128+
if (p==NULL)
129+
{
130+
return NULL;
131+
}
132+
if (p->elem==n)
133+
{
134+
return p;
135+
}
136+
else
137+
{
138+
if (p->elem>n)
139+
{
140+
return search(p->left,n);
141+
}
142+
else
143+
{
144+
return search(p->right,n);
145+
}
146+
}
147+
}
148+
149+
void BST::inorder(Node *p)
150+
{
151+
Node *q=p;
152+
if (q==NULL) return;
153+
inorder(q->left);
154+
cout<<q->elem<<" ";
155+
inorder(q->right);
156+
}
157+
158+
void BST::asl(int num)
159+
{
160+
double ASL=((num+1)/num)*(log(num+1)/log(2))-1;
161+
cout<<"该树成功查找的平均查找长度为"<<ASL<<endl;
162+
}
163+
164+
Node* BST::pop(Node *p,int n)
165+
{
166+
if (!p)
167+
{
168+
return p;
169+
}
170+
else
171+
{
172+
if (n<p->elem)
173+
{
174+
p->left=pop(p->left,n);
175+
}
176+
else
177+
{
178+
if (n>p->elem)
179+
{
180+
p->right=pop(p->right,n);
181+
}
182+
else
183+
{
184+
if (p->left&&p->right)
185+
{
186+
Node *tmp=findmin();
187+
p->elem=tmp->elem;
188+
p->right=pop(p->right,tmp->elem);
189+
}
190+
else
191+
{
192+
Node *tmp=p;
193+
if (!p->left)
194+
{
195+
p=p->right;
196+
}
197+
else
198+
{
199+
if (!p->right)
200+
{
201+
p=p->left;
202+
}
203+
}
204+
delete tmp;
205+
}
206+
}
207+
}
208+
}
209+
return p;
210+
}
211+
212+
Node* BST::findmin()
213+
{
214+
Node *cur=root;
215+
if (cur==NULL)
216+
{
217+
return NULL;
218+
}
219+
while (cur)
220+
{
221+
if (cur->left==NULL)
222+
{
223+
return cur;
224+
}
225+
else
226+
{
227+
cur=cur->left;
228+
}
229+
}
230+
}
231+
232+
Node* AVL::insert(int n)
233+
{
234+
if (root==NULL)
235+
{
236+
root=new Node(n);
237+
return root;
238+
}
239+
else
240+
{
241+
return insert(root,n);
242+
}
243+
}
244+
245+
Node* AVL::insert(Node *p,int n)
246+
{
247+
if (!p)
248+
{
249+
p=new Node(n);
250+
p->height=0;
251+
p->left=NULL;
252+
p->right=NULL;
253+
}
254+
if (n<p->elem)
255+
{
256+
p->left=insert(p->left,n);
257+
int rheight=getheight(p->right);
258+
int lheight=getheight(p->left);
259+
if (lheight-rheight==2)
260+
{
261+
if (n<p->left->elem)
262+
{
263+
p=ll(p);
264+
}
265+
else
266+
{
267+
p=lr(p);
268+
}
269+
}
270+
}
271+
else
272+
{
273+
if (n>p->elem)
274+
{
275+
p->right=insert(p->right,n);
276+
int rheight=getheight(p->right);
277+
int lheight=getheight(p->left);
278+
if (rheight-lheight==2)
279+
{
280+
if (n>p->right->elem)
281+
{
282+
p=rr(p);
283+
}
284+
else
285+
{
286+
p=rl(p);
287+
}
288+
}
289+
}
290+
}
291+
p->height=max(getheight(p->left),getheight(p->right));
292+
return p;
293+
}
294+
295+
int AVL::getheight(Node *p)
296+
{
297+
if (!p)
298+
{
299+
return 0;
300+
}
301+
else
302+
{
303+
return p->height;
304+
}
305+
}
306+
307+
int AVL::max(int a,int b)
308+
{
309+
return (a>b)?a:b;
310+
}
311+
312+
Node* AVL::ll(Node *p)
313+
{
314+
Node *tmp=p->left;
315+
p->left=tmp->right;
316+
tmp->right=p;
317+
p->height=max(getheight(p->left),getheight(p->right));
318+
tmp->height=max(getheight(tmp->left),getheight(tmp->right));
319+
return tmp;
320+
}
321+
322+
Node* AVL::rr(Node *p)
323+
{
324+
Node *tmp=p->right;
325+
p->right=tmp->left;
326+
tmp->left=p;
327+
p->height=max(getheight(p->left),getheight(p->right));
328+
tmp->height=max(getheight(p->left),getheight(p->right));
329+
return tmp;
330+
}
331+
332+
Node* AVL::lr(Node *p)
333+
{
334+
p->left=rr(p->left);
335+
return ll(p);
336+
}
337+
338+
Node* AVL::rl(Node *p)
339+
{
340+
p->right=ll(p->right);
341+
return rr(p);
342+
}
343+
344+
int main()
345+
{
346+
int val,x,
347+
num=0,
348+
choice;
349+
cout<<"请选择建立二叉排序树或平衡二叉树:"<<endl<<"1.二叉排序树"<<endl<<"2.平衡二叉树"<<endl;
350+
cin>>choice;
351+
cout<<"请输入数据:"<<endl;
352+
if (choice==1)
353+
{
354+
BST *bst=new BST();
355+
while (1)
356+
{
357+
cin>>val;
358+
bst->insert(val);
359+
num++;
360+
if (cin.get()=='\n') break;
361+
}
362+
Node *Root=bst->root;
363+
bst->inorder(Root);
364+
cout<<endl;
365+
bst->asl(num);
366+
cout<<"请输入要查找的x:";
367+
cin>>x;
368+
if (bst->search(x))
369+
{
370+
bst->pop(Root,x);
371+
bst->inorder(Root);
372+
cout<<endl;
373+
}
374+
else
375+
{
376+
cout<<""<<x<<endl;
377+
}
378+
}
379+
else if (choice==2)
380+
{
381+
AVL *avl=new AVL();
382+
while (1)
383+
{
384+
cin>>val;
385+
avl->insert(val);
386+
num++;
387+
if (cin.get()=='\n') break;
388+
}
389+
Node *Root=avl->root;
390+
avl->inorder(Root);
391+
cout<<endl;
392+
avl->asl(num);
393+
}
394+
395+
396+
system("pause");
397+
return 0;
398+
}

0 commit comments

Comments
 (0)