-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathTree_using_queue.cpp
196 lines (167 loc) · 3.42 KB
/
Tree_using_queue.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
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
#include <bits/stdc++.h>
using namespace std;
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define PB push_back
typedef long long ll;
#define endl "\n"
using namespace std;
class Node
{
public:
Node *lchild;
int data;
Node *rchild;
};
class Tree
{
private:
Node *root;
public:
Tree();
void CreateTree();
void Preorder(Node *p);
void Preorder() { Preorder(root); }
void Inorder(Node *p);
void Inorder() { Inorder(root); }
void Postorder(Node *p);
void Postorder() { Postorder(root); }
void Levelorder(Node *p);
void Levelorder() { Levelorder(root); }
int Height(Node *p);
int Height() { return Height(root); }
};
Tree::Tree()
{
root = NULL;
}
void Tree::CreateTree()
{
Node *p;
Node *t;
int x;
queue<Node *> q;
root = new Node;
cout << "Enter root data : ";
cin >> x;
root->data = x;
root->lchild = NULL;
root->rchild = NULL;
q.emplace(root);
while (!q.empty())
{
p = q.front();
q.pop();
cout << "Enter left child data of [-1 if you don't want] " << p->data << " : ";
cin >> x;
if (x != -1)
{
t = new Node;
t->data = x;
t->lchild = NULL;
t->rchild = NULL;
p->lchild = t;
q.emplace(t);
}
cout << "Enter right child data of [-1 if you don't want] " << p->data << " : ";
cin >> x;
if (x != -1)
{
t = new Node;
t->data = x;
t->lchild = NULL;
t->rchild = NULL;
p->rchild = t;
q.emplace(t);
}
}
}
void Tree::Preorder(Node *p)
{
if (p)
{
cout << p->data << ", ";
Preorder(p->lchild);
Preorder(p->rchild);
}
}
void Tree::Inorder(Node *p)
{
if (p)
{
Inorder(p->lchild);
cout << p->data << ", ";
Inorder(p->rchild);
}
}
void Tree::Postorder(Node *p)
{
if (p)
{
Postorder(p->lchild);
Postorder(p->rchild);
cout << p->data << ", " << endl;
}
}
void Tree::Levelorder(Node *p)
{
queue<Node *> q;
cout << root->data << ", ";
q.emplace(root);
while (!q.empty())
{
p = q.front();
q.pop();
if (p->lchild)
{
cout << p->lchild->data << ", ";
q.emplace(p->lchild);
}
if (p->rchild)
{
cout << p->rchild->data << ", ";
q.emplace(p->rchild);
}
}
}
int Tree::Height(Node *p)
{
int l = 0;
int r = 0;
if (p == NULL)
{
return 0;
}
l = Height(p->lchild);
r = Height(p->rchild);
if (l > r)
{
return l + 1;
}
else
{
return r + 1;
}
}
int main()
{
Tree bt;
bt.CreateTree();
cout << endl;
cout << "Preorder : " << endl;
bt.Preorder();
cout << endl;
cout << "Inorder : " << endl;
bt.Inorder();
cout << endl;
cout << "Postorder : " << endl;
bt.Postorder();
cout << endl;
cout << "Levelorder : " << endl;
bt.Levelorder();
cout << endl;
cout << "Height : " << bt.Height() << endl;
return 0;
}