-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeTraversal.cpp
More file actions
206 lines (143 loc) · 4.19 KB
/
TreeTraversal.cpp
File metadata and controls
206 lines (143 loc) · 4.19 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
//Print binary tree in reverse level order
/*
1 1
/ \ / \
2 3 2 3
/\ /\ / \
4 5 6 7 4 5
print == > 4,5,6,7,2,3,1
*/
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
int FeederArray[]={1,2,3,4,5,6,7,8,9};
//template<class T>
//T max(T i, T j) { return ( (i>=j)?i:j); }
#define max(i,j) ((i>=j)?i:j)
#define MAX_TREE_HEIGHT_WIDTH 25
int MaxLeftColFromRoot=0;
int MaxRightColFromRoot=0;
int MaxWidth=0;
bool ColCalculated=false;
bool LevelCalculated = false;
int TreeHeight=0;
int TreeArray[MAX_TREE_HEIGHT_WIDTH][MAX_TREE_HEIGHT_WIDTH];
struct node {
int mVal;
node* mRight;
node* mLeft;
int mAtWhicLevel;
int mAtWhichColumn;
node(int val=0) : mVal(val), mRight(NULL), mLeft(NULL), mAtWhicLevel(0),mAtWhichColumn(0) {}
~node() {
if(mLeft) { delete mLeft; }
if(mRight) { delete mRight; }
mLeft=mRight=NULL; //just for readability else it doesn't adds any value;
}
};
void DFSTravelNFillTreeArray(node* root) {
if(!root) return;
TreeArray[TreeHeight-root->mAtWhicLevel][root->mAtWhichColumn-MaxLeftColFromRoot]=root->mVal;
DFSTravelNFillTreeArray(root->mLeft);
DFSTravelNFillTreeArray(root->mRight);
}
void SetCoumns(node* root, int col) { // col is the col number of the root node coming in param
if(!root) { return; }
if(root->mLeft) {
root->mLeft->mAtWhichColumn=col-1;
if(root->mLeft->mAtWhichColumn < MaxLeftColFromRoot){
MaxLeftColFromRoot = root->mLeft->mAtWhichColumn;
}
}
if(root->mRight) {
root->mRight->mAtWhichColumn=col+1;
if(root->mRight->mAtWhichColumn > MaxRightColFromRoot){
MaxRightColFromRoot = root->mRight->mAtWhichColumn;
}
}
if(root->mLeft) { SetCoumns(root->mLeft,root->mLeft->mAtWhichColumn); }
if(root->mRight) { SetCoumns(root->mRight,root->mRight->mAtWhichColumn); }
}
int Setlevel(node* root) {
if(!root) { return -1; }
root->mAtWhicLevel= max<int>( Setlevel(root->mLeft), Setlevel(root->mRight) )+1;
return root->mAtWhicLevel;
}
void PrintAtLevel(node* root, int level, bool LeftToRight) { // can be used in zigzag as well
if(!root) return;
if(level == root->mAtWhicLevel) { cout<<root->mVal<<"\t"; }
if(LeftToRight) { //printing left to right nodes at this level
PrintAtLevel(root->mLeft,level,LeftToRight);
PrintAtLevel(root->mRight,level,LeftToRight);
}else { //printing left to right nodes at this level
PrintAtLevel(root->mRight,level,LeftToRight);
PrintAtLevel(root->mLeft,level,LeftToRight);
}
}
void print_reverse_level(node* root) {
if(!TreeHeight) {
TreeHeight = Setlevel(root);//levels are set;
}
LevelCalculated = true;
for(int counter=0;counter<=TreeHeight;counter++) {
PrintAtLevel(root,counter,true);
}
}
node* ConstructTreeFromArray(int ArraySize) {
int counter=0;
node* root =new node(FeederArray[counter++]);
node* iterator=root;
std::queue<node*> Q; Q.push(root);
while(counter<ArraySize-2) {
iterator= Q.front();Q.pop();
iterator->mLeft= new node(FeederArray[counter++]); Q.push(iterator->mLeft);
iterator->mRight= new node(FeederArray[counter++]); Q.push(iterator->mRight);
}
if(counter<ArraySize) {
iterator= Q.front();Q.pop();
iterator->mLeft= new node(FeederArray[counter++]); Q.push(iterator->mLeft);
}
if(counter<ArraySize) {
iterator->mRight= new node(FeederArray[counter++]); Q.push(iterator->mRight);
}
return root;
}
void PrintBinaryTree(node* root) {
memset(TreeArray,0,sizeof(TreeArray));
SetCoumns(root,0);
ColCalculated = true;
MaxWidth = MaxRightColFromRoot - (MaxLeftColFromRoot);
if(!TreeHeight) {
TreeHeight = Setlevel(root);//levels are set;
}
//Travers Any Way & fill the data
DFSTravelNFillTreeArray(root);
//Now, just print
cout<<"\n\n\n";
for(int i=0;i<=TreeHeight;i++){
cout<<"\n";
for(int j =0;j<=MaxWidth;j++) {
if(0==TreeArray[i][j]) { cout<<" "; }
else { cout<<TreeArray[i][j]; }
cout<<" ";
}
}
cout<<"\n\n\n";
for(int i=0;i<=TreeHeight;i++) {
cout<<"\n";
for(int j =0;j<=MaxWidth;j++) {
cout<<TreeArray[i][j]<<" ";
}
}
cout<<"\n\n\n";
}
//now main
int main() {
cout<<endl;
node* root = ConstructTreeFromArray(sizeof(FeederArray)/sizeof(int));
PrintBinaryTree(root);
print_reverse_level(root);
cout<<endl;
return 0;
}