-
Notifications
You must be signed in to change notification settings - Fork 0
/
Construct_Binary_Search_Tree_From_PreOrder_Traversal.cpp
177 lines (145 loc) · 4.24 KB
/
Construct_Binary_Search_Tree_From_PreOrder_Traversal.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
//{ Driver Code Starts
// Initial template for C++
/* A O(n) iterative program for construction of BST from preorder traversal */
#include <bits/stdc++.h>
using namespace std;
/* A binary tree node has data, pointer to left child
and a pointer to right child */
typedef struct Node {
int data;
struct Node *left, *right;
} Node;
// A Stack has array of Nodes, capacity, and top
typedef struct Stack {
int top;
int capacity;
Node** array;
} Stack;
// A utility function to create a new tree node
Node* newNode(int data) {
Node* temp = (Node*)malloc(sizeof(Node));
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
// A utility function to create a stack of given capacity
Stack* createStack(int capacity) {
Stack* stack = (Stack*)malloc(sizeof(Stack));
stack->top = -1;
stack->capacity = capacity;
stack->array = (Node**)malloc(stack->capacity * sizeof(Node*));
return stack;
}
// A utility function to check if stack is full
int isFull(Stack* stack) {
return stack->top == stack->capacity - 1;
}
// A utility function to check if stack is empty
int isEmpty(Stack* stack) {
return stack->top == -1;
}
// A utility function to push an item to stack
void push(Stack* stack, Node* item) {
if (isFull(stack))
return;
stack->array[++stack->top] = item;
}
// A utility function to remove an item from stack
Node* pop(Stack* stack) {
if (isEmpty(stack))
return NULL;
return stack->array[stack->top--];
}
// A utility function to get top node of stack
Node* peek(Stack* stack) {
return stack->array[stack->top];
}
bool canRepresentBST(int pre[], int n) {
// Create an empty stack
stack<int> s;
// Initialize current root as minimum possible
// value
int root = INT_MIN;
// Traverse given array
for (int i = 0; i < n; i++) {
// If we find a node who is on right side
// and smaller than root, return false
if (pre[i] < root)
return false;
// If pre[i] is in right subtree of stack top,
// Keep removing items smaller than pre[i]
// and make the last removed item as new
// root.
while (!s.empty() && s.top() < pre[i]) {
root = s.top();
s.pop();
}
// At this point either stack is empty or
// pre[i] is smaller than root, push pre[i]
s.push(pre[i]);
}
return true;
}
// A utility function to print postorder traversal of a Binary Tree
void printPostorder(Node* node) {
if (node == NULL)
return;
printPostorder(node->left);
printPostorder(node->right);
printf("%d ", node->data);
}
// } Driver Code Ends
// User function template in C++
/*
typedef struct Node
{
int data;
struct Node *left, *right;
} Node;
// A utility function to create a new tree node
Node* newNode( int data )
{
Node* temp = (Node *)malloc( sizeof( Node ) );
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
*/
class Solution {
public:
// Function that constructs BST from its preorder traversal.
Node* Bst(int pre[], int size) {
int i = 0;
int maxi = INT_MAX;
return build(pre, size, i, maxi);
}
Node* build(int pre[], int size, int &i, int bound)
{
if(i == size || pre[i] > bound) return NULL;
Node* root = newNode(pre[i++]);
root->left = build(pre, size, i, root->data);
root->right = build(pre, size, i, bound);
return root;
}
};
//{ Driver Code Starts.
int main() {
// Note that size of arr[] is considered 100 according to
// the constraints mentioned in problem statement.
int arr[1000], x, t, n;
// Input the number of test cases you want to run
cin >> t;
// One by one run for all input test cases
while (t--) {
// Input the size of the array
cin >> n;
// Input the array
for (int i = 0; i < n; i++)
cin >> arr[i];
Solution ob;
printPostorder(ob.Bst(arr, n));
cout << endl;
}
return 0;
}
// } Driver Code Ends