-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree.cpp
More file actions
215 lines (191 loc) · 3.29 KB
/
Tree.cpp
File metadata and controls
215 lines (191 loc) · 3.29 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
210
211
212
213
214
215
#include<iostream>
int summ = 0;
class List;
class TreeNode;
class node;
class List{
node *current, *head;
public:
List();
void add(TreeNode* val);
void print();
};
class TreeNode{
public:
TreeNode* left;
TreeNode* right;
int val;
TreeNode(int val);
void pre_order(List &l);
void post_order();
void in_order();
void dfs();
void sum();
int get_sum();
};
TreeNode::TreeNode(int val)
{
this->val = val;
left = right = NULL;
}
void TreeNode::pre_order(List &l)
{
static int i = 0;
if(!i)
{
std::cout << "Pre _order " << std::endl;
i++;
}
std::cout << val <<std::endl;
std::cout << "Sum is " << summ << std::endl;
if(left != NULL)
{
std::cout << "This is left " ;
l.add(left);
left->pre_order(l);
}
if(right != NULL)
{
std::cout << "This is right " ;
l.add(right);
right->pre_order(l);
}
return;
}
/*
void TreeNode::dfs()
{
TreeNode* temp;
while(!is_empty)
{
temp = q.dequeue();
std::cout << temp.val << std::endl;
if(temp.left)
q.enqueue(temp.left);
if(temp.right)
q.enqueue(temp.right);
}
}
*/
void TreeNode::post_order()
{
static int i = 0;
if(!i)
{
std::cout << "Post _order " << std::endl;
i++;
}
if(left != NULL)
left->post_order();
if(right != NULL)
right->post_order();
std::cout << val << std::endl;
return;
}
void TreeNode::in_order()
{
static int i = 0;
if(!i)
{
std::cout << "In_order " << std::endl;
i++;
}
if(left != NULL)
left->in_order();
std::cout << val << std::endl;
if(right != NULL)
right->in_order();
return;
}
void TreeNode::sum()
{
summ += val;
std::cout << val <<std::endl;
if(left != NULL)
{
left->sum();
}
if(right != NULL)
{
right->sum();
}
}
int TreeNode::get_sum()
{
return summ;
}
class node{
public:
node* next;
TreeNode* val;
node(TreeNode* val);
};
node::node(TreeNode* val)
{
this->val = val;
next = NULL;
}
List::List()
{
head = current = NULL;
}
void List::add(TreeNode* val)
{
if(head == NULL)
{
head = new node(val);
current = head;
return;
}
node* n = new node(val);
current->next = n;
current = current->next;
}
void List::print()
{
std::cout << "Let's Save whole tree into the List " << std::endl;
current = head;
std::cout << "[ ";
while(current != NULL)
{
std::cout << current->val->val << " ";
current = current->next;
}
std::cout << "]" << std::endl;
}
int main()
{
TreeNode* t = new TreeNode(1);
List l;
l.add(t);
t->right = new TreeNode(2);
// l.add(t->right);
t->right->right = new TreeNode(55);
// l.add(t->right->right);
t->right->right->left = new TreeNode(87);
// l.add(t->right->right->left);
t->right->right->right = new TreeNode(101);
// l.add(t->right->right->right);
t->right->right->right->right = new TreeNode(1019);
// l.add(t->right->right->right->right);
t->right->right->right->left = new TreeNode(200);
// l.add(t->right->right->right->left);
t->left = new TreeNode(3);
// l.add(t->left);
t->left->left = new TreeNode(5);
// l.add(t->left->left);
t->left->right = new TreeNode(15);
// l.add(t->left->right);
t->pre_order(l);
std::cout << std::endl;
t->post_order();
std::cout << std::endl;
t->in_order();
t->sum();
std::cout << "Total sum of the Tree is --> " << t->get_sum() << std::endl;
l.print();
//t.left = new TreeNode(5);
//t.left->left = new TreeNode(10);
std::cin.get();
return 0;
}