-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBST.cpp
99 lines (98 loc) · 1.93 KB
/
BST.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include"BST.h";
binarysearchtree::binarysearchtree()
{
this->data=0;
this->left=NULL;
this->right=NULL;
}
binarysearchtree::binarysearchtree(int n)
{
this->data=n;
this->left=NULL;
this->right=NULL;
}
binarysearchtree::~binarysearchtree()
{
}
int binarysearchtree::getdata()
{
return this->data;
}
binarysearchtree* binarysearchtree::Max()
{
if(this->right==NULL)
return this;
else
return this->right->Max();
}
binarysearchtree* binarysearchtree::Min()
{
if(this->left==NULL)
return this;
else
return this->left->Min();
}
binarysearchtree::Insert(int n)
{
if(n>this->data)
{
if(this->right==NULL)
{
binarysearchtree* tmp=new binarysearchtree;
tmp->left=NULL;
tmp->right=NULL;
tmp->data=n;
}
else
this->right->Insert(n);
}
else if(n<this->left)
{
if(this->left==NULL)
{
binarysearchtree* tmp=new binarysearchtree;
tmp->left=NULL;
tmp->right=NULL;
tmp->data=n;
}
else
this->left->Insert(n);
}
}
binarysearchtree::Mirror()
void showpost(binarysearchtree* node,bool &first)
{
if(node==NULL)
return;
showpost(node->left,first);
showpost(node->right,first);
//²Ä¤@ӼƤ§«e¤£¥ÎªÅ®æ
if(!first)cout<<" ";
else first=false;
cout<<node->data;
}
void showpre(binarysearchtree* node,first)
{
if(node==NULL)
return;
if(!first)cout<<" ";
else first=false;
cout<<node->data;
showpre(node->left,first);
showpre(node->right,first);
}
int Height(binarysearchtree* node)
{
if(node==NULL)
return 0;
int L=Height(node->left);
int R=Height(node->right);
(L>=R)?return (L+1):return (R+1);
}
int Size(binarysearchtree* node)
{
if(node==NULL)
return 0;
else
return Size(node->left)+Size(node->right)+1;
}