-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-binary-tree.cpp
99 lines (80 loc) · 2.11 KB
/
build-binary-tree.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
#include<iostream>
#define endl "\n"
using namespace std;
// Basic Structure of a Tree
struct Node{
int data;
struct Node *left;
struct Node *right;
Node(int val){
data = val;
left = NULL;
right = NULL;
}
};
/* Construct a binary tree using Pre, In and Post order traversals */
/* Complexity in Both cases : O(n^2) */
// Utility function to search the element in Inorder
int search (int in[],int start ,int end,int curr){
for(int i=start;i<=end;++i){
if(in[i] == curr)
return i;
}
return -1;
}
// Build a binary Tree using Pre & In order (using Recursion)
Node* buildTreePreIn(int pre[],int in[],int start, int end){
static int idx = 0;
if(start > end)
return NULL;
int curr = pre[idx];
idx++;
Node* node = new Node(curr);
if(start == end)
return node;
int pos = search(in,start,end,curr);
node->left = buildTreePreIn(pre,in,start,pos-1);
node->right = buildTreePreIn(pre,in,pos+1,end);
return node;
}
// Build a binary Tree using Post & In order (using Recursion)
Node* buildTreePostIn(int post[],int in[],int start,int end){
static int idx = end;
if(start > end)
return NULL;
int curr = post[idx];
idx--;
Node* node = new Node(curr);
if(start == end)
return node;
int pos = search(in,start,end,curr);
node->right = buildTreePostIn(post,in,pos+1,end);
node->left = buildTreePostIn(post,in,start,pos-1);
return node;
}
// Inorder function to check if tree was built successfully!!
void inOrder(struct Node* root){
if(root == NULL)
return;
inOrder(root->left);
cout<<root->data<<" ";
inOrder(root->right);
}
int main(){
//n=5
int preorder[] = {1,2,4,3,5};
int inorder[] = {4,2,1,5,3}; // This order should match the output
int postorder[] = {4,2,5,3,1};
Node* t1 = buildTreePreIn(preorder,inorder,0,4);
Node* t2 = buildTreePostIn(postorder,inorder,0,4);
/* Tree t
1
/ \
2 3
/ /
4 5
*/
cout<<"Output Inorder : ";
inOrder(t2);
return 0;
}