-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBT.cpp
More file actions
executable file
·172 lines (156 loc) · 3.65 KB
/
BT.cpp
File metadata and controls
executable file
·172 lines (156 loc) · 3.65 KB
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
164
165
166
167
168
169
170
171
172
#include<iostream>
#include<cstring>
using namespace std;
void ini_cout();
void insert();
void preorder(int );
void inorder(int );
void postorder(int );
int find();
void del(int );
int left(int );
int right(int );
int tree[10000];
bool exist[10000];
int main(void)
{
int i, j, k;
string s_input;
ini_cout();
for(;;)
{
cin>>s_input;
if(s_input == "insert") insert();
else if(s_input == "done") break;
else if(s_input == "find") cout<<"found at "<<find()<<endl;
else if(s_input == "delete") del(find());
else if(s_input == "preorder") preorder(1);
else if(s_input == "inorder") inorder(1);
else if(s_input == "postorder") postorder(1);
else cout<<"invalid input";
if(s_input != "insert" && s_input != "delete" && s_input != "find") cout<<endl;
}
return 0;
}
void ini_cout()
{
int i;
for(i=0;i<10000;i++) exist[i] = false;
cout<<"command 1 : insert with one number \"insert 1\""<<endl;
cout<<"command 2 : inorder"<<endl;
cout<<"command 3 : find with one number \"find 1\""<<endl;
cout<<"command 4 : preorder"<<endl;
cout<<"command 5 : postorder"<<endl;
cout<<"command 6 : delete with one number \"delete 1\""<<endl;
cout<<"command 7 : done"<<endl;
}
void del(int x)
{
int victem;
if(x != -1) cout<<"found at : "<<x<<endl;
if(x == -1) return;
for(victem=x;;)
{
if(exist[right(victem)] == true)
{
for(victem = right(victem);exist[left(victem)];victem = left(victem)) ;
tree[x] = tree[victem];
x = victem;
}
else if(exist[left(victem)] == true)
{
for(victem = left(victem);exist[right(victem)];victem = right(victem)) ;
tree[x] = tree[victem];
x = victem;
}
else if(exist[left(victem)] == false && exist[right(victem)] == false) break;
}
tree[x] = tree[victem];
exist[victem] = false;
}
int find()
{
int i_input, now;
cin>>i_input;
for(now=1;;)
{
if(exist[now] == false) cout<<"not found !!! "<<endl;
if(exist[now] == false) return -1;
else if(tree[now] == i_input) return now;
else if(i_input > tree[now]) now = right(now);
else if(i_input <= tree[now]) now = left(now);
}
}
void insert()
{
int i_input;
int now;
cin>>i_input;
if(exist[1] == false)
{
tree[1] = i_input;
exist[1] = true;
return;
}
else
{
for(now = 1;;)
{
if(i_input > tree[now] && exist[right(now)] == true) now = right(now);
else if(i_input <= tree[now] && exist[left(now)] == true) now = left(now);
if(i_input > tree[now] && exist[right(now)] == false)
{
tree[right(now)] = i_input;
exist[right(now)] = true;
return;
}
else if(i_input <= tree[now] && exist[left(now)] == false)
{
tree[left(now)] = i_input;
exist[left(now)] = true;
return;
}
}
}
}
void preorder(int now)
{
if(exist[1] == false) cout<<"it is an empty tree";
else if(exist[now] == false) return;
else
{
cout<<tree[now]<<" ";
if(exist[left(now)] == true) preorder(left(now));
if(exist[right(now)] == true) preorder(right(now));
}
}
void inorder(int now)
{
if(exist[1] == false) cout<<"it is an empty tree";
else if(exist[now] == false) return;
else
{
if(exist[left(now)] == true) inorder(left(now));
cout<<tree[now]<<" ";
if(exist[right(now)] == true) inorder(right(now));
}
}
void postorder(int now)
{
if(exist[1] == false) cout<<"it is an empty tree";
else if(exist[now] == false) return;
else
{
if(exist[left(now)] == true) postorder(left(now));
if(exist[right(now)] == true) postorder(right(now));
cout<<tree[now]<<" ";
}
}
int left(int input)
{
return (input * 2);
}
int right(int input)
{
return (input * 2 + 1);
}