-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeQueue_height.cpp
More file actions
209 lines (175 loc) · 3.53 KB
/
Copy pathTreeQueue_height.cpp
File metadata and controls
209 lines (175 loc) · 3.53 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include<iostream>
using namespace std;
struct node
{
int item, ht;
node *l, *r;
node(int x)
{
item = x; l = 0; r = 0; ht=0;
}
};
typedef node *link;
link h=0;
class QUEUE
{
private:
link *q; int N, head, tail;
public:
QUEUE(int maxN)
{
q = new link[maxN + 1];
N = maxN + 1;
head = N;
tail = 0;
}
int empty() const
{
return head % N == tail;
}
void put(link item)
{
q[tail++] = item; tail = tail % N;
}
link get()
{
head = head % N; return q[head++];
}
};
link findnode(link h, int pitem)
{
QUEUE q(100);
q.put(h);
while (!q.empty())
{
h = q.get();
if (h->item == pitem) break;
if (h->l != 0) q.put(h->l);
if (h->r != 0) q.put(h->r);
}
if (h->item != pitem)
{
cout << "Not find node with item " << pitem << endl;
return 0;
}
return h;
}
void traverse(link h)
{
if (h == 0) return;
QUEUE q(100);
link t = 0;
q.put(h);
while (!q.empty())
{
t = q.get();
cout << t->item << " ";
if (t->l != 0) q.put(t->l);
if (t->r != 0) q.put(t->r);
}
cout << endl;
}
void putLeft(int p, int c) // putting the node of value 'c' on the left of node of value 'p'
{
link t = findnode(h, p);
if (t != 0)
{
if (t->l == 0)
{
t->l = new node(c);
cout << "Node with item " << c << " has been added" << endl;
}
else
{
cout << "Error: Node with item " << p << " has had left node" << endl;
}
}
}
void putRight(int p, int c)// putting the node of value 'c' on the right of node of value 'p'
{
link t = findnode(h, p);
if (t != 0)
{
if (t->r == 0)
{
t->r = new node(c);
cout << "Node with item " << c << " has been added" << endl;
}
else
{
cout << "Error: Node with item " << p << " has had left node" << endl;
}
}
}
void traverseSubtree(int i)
{
link t = findnode(h, i);
if (t != 0)
{
cout << "level-order traversal of the asked subtree:";
traverse(t);
cout << endl;
}
}
int height(link h)
{
QUEUE q(100);
q.put(h);
h->ht=1;
while(!q.empty())
{
h =q.get();
int maxh = h->ht;
if(h->l!=0)
{
q.put(h->l);
h->l->ht = h->ht+ 1;
}
if(h->r!=0)
{
q.put(h->r);
h->r->ht = h->ht+1;
}
}
return h->ht;
}
int main()
{
QUEUE q(0); //tree
char dialogue;
int root,parent,child,sub;
do
{
cin>>dialogue;
switch(dialogue)
{
case 'C': // Create the root node with item 'root'
cin >>root;
if (h == 0)
{
h = new node(root);
cout << "Root node with item " << root << " has been created" << endl;
}
else
{
cout << "Error: Tree is not empty" << endl;
}
break;
case 'L': // create a node 'child' to the left of node 'parent'
cin>>parent>>child;
putLeft(parent,child);
break;
case 'R': // create a node 'child' to the right of node 'parent'
cin>>parent>>child;
putRight(parent,child);
break;
case 'P': //Print the height of tree in a level order traversal manner
cout<<"The tree height is "<<height(h)<< " (using the level order traversal of the entire tree)"<<endl;
break;
case 'S': //Print subtree of node 'p' in level order traversal manner
cin>>sub;
traverseSubtree(sub);
break;
}
}while(dialogue !='0');
}