
先把代码挂这吧
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <iostream>
using namespace std;
//binarysearch Tree
class binarysearchTree
{
private:
class node
{
public:
int val;
node * left;
node * right;
node * p;
node(int x):val(x),left(NULL),right(NULL),p(NULL){}
};
node * head;
public:
binarysearchTree(int x){head=new node (x);}
void Inorder(node *x)
{
if (x!=NULL)
{
Inorder(x->left);
cout<<x->val<<' ';
Inorder(x->right);
}
}
void Inorder()
{
Inorder(head);
}
node* Tree_search(int k,node *x)
{
if (x==NULL || k==x->val)
return x;
if (k<x->val)
return Tree_search(k,x->left);
else return Tree_search(k,x->right);
}
node* Tree_search(int k)
{
Tree_search(k,head);
}
node * Tree_search_iteractive(node *x,int k)
{
while (x!=NULL && k!=x->val)
{
if (k<x->val)
x=x->left;
else
x=x->right;
}
return x;
}
node* Tree_MINUMIN(node *x)
{
while (x->left!=NULL)
x=x->left;
return x;
}
node * Tree_MAXIMUM()
{
node *x=head;
while (x->right!=NULL)
x=x->right;
return x;
}
node *Tree_succsor(node *x)
{
if (x->left!=NULL)
return Tree_MINUMIN(x);
node *y=x->p;
while (y!=NULL && x==y->right)
{
x=y;
y=y->p;
}
return y;
}
void tree_insert(int x)
{
node *t= new node (x);
tree_insert(t);
}
void tree_insert(node *z)
{
node * y=NULL;
node * x=head;
while (x!=NULL)
{
y=x;
if (z->val <x->val)
x=x->left;
else x=x->right;
}
z->p=y;
if (y==NULL)
head=z;
else if (z->val<y->val)
y->left=z;
else y->right=z;
}
void tranparent(node *u,node* v)
{
if (u->p==NULL)
head=v;
else if (u->p->right==u)
u->p->right=v;
else u->p->left=v;
if (v!=NULL)
v->p=u->p;
}
void tree_delete(node *z)
{
if (z->left == NULL)
{
tranparent(z,z->right);
}
else if (z->left==NULL)
{
tranparent(z,z->left);
}
else
{
node *y=Tree_MINUMIN(z->right);
if (y->p!=z)
{
tranparent(y,y->right);
y->right=z->right;
y->right->p=y;
}
tranparent(z,y);
y->left=z->left;
y->left->p=y;
}
}
void tree_delete(int x)
{
node *z=Tree_search(x);
if (z==NULL)
cout<<"can't find such node"<<'n';
else
{
tree_delete(z);
}
return ;
}
};
int main(int argc, char const *argv[])
{
binarysearchTree *x=new binarysearchTree(1);
for (int i=2;i<10;i++)
{
x->tree_insert(i);
}
x->Inorder();
return 0;
}




近期评论