forked from akibzaman/Beginners-Data-Structure
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreorderInordertoBinary.cpp
181 lines (150 loc) · 3.05 KB
/
preorderInordertoBinary.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
#include <bits/stdc++.h>
using namespace std;
class treeNode
{
public:
int data;
treeNode *leftChild;
treeNode *rightChild;
treeNode(int value)
{
data = value;
leftChild = NULL;
rightChild = NULL;
}
};
void printTree(treeNode *root, int level);
void spacePrint(int level);
void inOrder(treeNode *root, string &chk) // Left Root Right
{
if (root == NULL)
return;
inOrder(root->leftChild, chk);
chk += to_string(root->data);
inOrder(root->rightChild, chk);
}
void preOrderTraversal(treeNode *root, string &chk) // Root Left Right
{
if (root == NULL)
return;
chk += to_string(root->data);
preOrderTraversal(root->leftChild, chk);
preOrderTraversal(root->rightChild, chk);
}
void postOrder(treeNode *root, string &chk) // Left Right Root
{
if (root == NULL)
return;
postOrder(root->leftChild, chk);
postOrder(root->rightChild, chk);
chk += to_string(root->data);
}
void printTree(treeNode *root, int level)
{
if (root == NULL) // WHEN THE TREE IS EMPTY
{
return;
}
if (root->leftChild == NULL && root->rightChild == NULL) // CASE 1
{
cout << root->data << endl;
return;
}
else // CASE 2
{
cout << endl;
spacePrint(level);
cout << "Root: " << root->data << endl;
}
if (root->leftChild != NULL)
{
spacePrint(level);
cout << "Left: ";
printTree(root->leftChild, level + 1);
}
if (root->rightChild != NULL)
{
spacePrint(level);
cout << "Right: ";
printTree(root->rightChild, level + 1);
}
}
void spacePrint(int level)
{
for (int i = 0; i < level; i++)
{
cout << " ";
}
}
int searchInorder(int inOrder[], int current, int start, int end)
{
for (int i = start; i <= end; i++)
{
if (inOrder[i] == current)
{
return i;
}
}
return -1;
}
treeNode *buildTreePreIn(int preOrder[], int inOrder[], int start, int end)
{
static int id = 0;
int current = preOrder[id];
id++;
treeNode *newNode = new treeNode(current);
if (start == end)
{
return newNode;
}
int pos = searchInorder(inOrder, current, start, end);
newNode->leftChild = buildTreePreIn(preOrder, inOrder, start, pos - 1);
newNode->rightChild = buildTreePreIn(preOrder, inOrder, pos + 1, end);
return newNode;
}
int main()
{
int n;
cin >> n;
int preOrder[n], inOrder[n];
for (int i = 0; i < n; i++)
{
cin >> preOrder[i];
}
for (int i = 0; i < n; i++)
{
cin >> inOrder[i];
}
treeNode *root = buildTreePreIn(preOrder, inOrder, 0, n - 1);
string chkPre = "";
preOrderTraversal(root, chkPre);
cout<< chkPre << endl << endl;
return 0;
}
/*
9
0 1 3 4 2 5 7 8 6
3 1 4 0 7 5 8 2 6
9
0 1 2
1 3 4
2 5 6
3 -1 -1
4 -1 -1
5 7 8
6 -1 -1
7 -1 -1
8 -1 -1
Root: 0
Left:
Root: 1
Left: 3
Right: 4
Right:
Root: 2
Left:
Root: 5
Left: 7
Right: 8
Right:6
*/