forked from oviiii-m/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST.cpp
More file actions
205 lines (195 loc) · 5.28 KB
/
BST.cpp
File metadata and controls
205 lines (195 loc) · 5.28 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
#include <iostream>
using namespace std;
#include<queue>
#include<climits>
template <typename T>
class BinaryTreeNode
{
public:
T data;
BinaryTreeNode* left;
BinaryTreeNode* right;
BinaryTreeNode(T data)
{
this->data = data;
left = NULL;
right = NULL;
}
};
template <typename T>
class Node
{
public:
T data;
Node* next;
Node(T data)
{
this->data = data;
next = NULL;
}
};
BinaryTreeNode<int>* takeinput()
{
int rootdata;
cout << "Enter root data: ";
cin >> rootdata;
if (rootdata == -1)
return NULL;
BinaryTreeNode<int>* root = new BinaryTreeNode<int>(rootdata);
queue<BinaryTreeNode<int>*> q;
q.push(root);
while (!q.empty())
{
BinaryTreeNode<int>* front = q.front();
q.pop();
int leftchild;
cout << "Enter left child of " << front->data << ": ";
cin >> leftchild;
if (leftchild != -1)
{
BinaryTreeNode<int>* lchild = new BinaryTreeNode<int>(leftchild);
front->left = lchild;
q.push(lchild);
}
int rightchild;
cout << "Enter right child of " << front->data << ": ";
cin >> rightchild;
if (rightchild != -1)
{
BinaryTreeNode<int>* rchild = new BinaryTreeNode<int>(rightchild);
front->right = rchild;
q.push(rchild);
}
}
return root;
}
//isBst approach 1 time Complexity=O(n*h) h=height of tree which is bad
int maximum(BinaryTreeNode<int>* root)
{
if(root==NULL)
return INT_MIN;
return max(root->data,max(maximum(root->left),maximum(root->right)));
}
int minimum(BinaryTreeNode<int>* root)
{
if(root==NULL)
return INT_MAX;
return min(root->data,min(minimum(root->left),minimum(root->right)));
}
bool isBST(BinaryTreeNode<int> *root){
if(root==NULL)
return true;
int leftmax=maximum(root->left);
int rightmin=minimum(root->right);
bool output=root->data>leftmax&&root->data<rightmin&&isBST(root->left)&&isBST(root->right);
return output;
}
// isBST approach2 time Complexity=O(n) we will use pair class
class IsBSTReturn {
public:
bool isBST;
int minimum;
int maximum;
};
IsBSTReturn isBST2(BinaryTreeNode<int>* root) {
if (root == NULL) {
IsBSTReturn output;
output.isBST = true;
output.minimum = INT_MAX;
output.maximum = INT_MIN;
return output;
}
IsBSTReturn leftOutput = isBST2(root->left);
IsBSTReturn rightOutput = isBST2(root->right);
int minimum = min(root->data, min(leftOutput.minimum, rightOutput.minimum));
int maximum = max(root->data, max(leftOutput.maximum, rightOutput.maximum));
bool isBSTFinal = (root->data > leftOutput.maximum) && (root->data <= rightOutput.minimum) &&
leftOutput.isBST && rightOutput.isBST;
IsBSTReturn output;
output.minimum = minimum;
output.maximum = maximum;
output.isBST = isBSTFinal;
return output;
}
//isBST approach 3 we apply i.e constraints to our root i.e root Range=(-infin,+infin),root->left Range=(-infin,root->data-1),root->right Range=(root->data,+infin)
bool isBST3(BinaryTreeNode<int>* root,int min=INT_MIN,int max=INT_MAX)
{
if (root == NULL)
return true;
if (root->data<min || root->data>max)
return false;
bool isLeft = isBST3(root->left,min,root->data-1);
bool isRight = isBST3(root->right,root->data,max);
return isLeft && isRight;
}
//Construct BST from sorted Array;
BinaryTreeNode<int>* ConstructBST(int* input, int si, int ei)
{
if (si > ei)
return NULL;
int mid = (si + ei) / 2;
BinaryTreeNode<int>* root = new BinaryTreeNode<int>(input[mid]);
root->left = ConstructBST(input, si, mid - 1);
root->right = ConstructBST(input, mid + 1, ei);
return root;
}
//BST to Sorted LL
Node<int>* ConstructLL(BinaryTreeNode<int>* root) {
if (root == NULL)
return NULL;
if (root->left == NULL && root->right == NULL) {
Node<int>* output = new Node<int>(root->data);
return output;
}
Node<int>* r = new Node<int>(root->data);
Node<int>* lhead = ConstructLL(root->left);
Node<int>* temp = lhead;
if (lhead != NULL)
{
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = r;
}
r->next = ConstructLL(root->right);
if (lhead == NULL)
return r;
else return lhead;
}
//Find Path in BST
vector<int>* findPath(BinaryTreeNode<int>* root, int data) {
if (root == NULL)
return NULL;
if (root->data == data)
{
vector<int>* output = new vector<int>();
output->push_back(root->data);
return output;
}
if (root->data > data)
{
vector<int>* leftoutput = findPath(root->left, data);
if (leftoutput != NULL)
leftoutput->push_back(root->data);
return leftoutput;
}
else if (root->data < data)
{
vector<int>* rightoutput = findPath(root->right, data);
if (rightoutput != NULL)
rightoutput->push_back(root->data);
return rightoutput;
}
else
{
return NULL;
}
}
// 9 2 6 1 3 5 7 -1 -1 -1 -1 -1 -1 -1 -1
int main()
{
BinaryTreeNode<int>* root = takeinput();
cout << endl;
cout <<isBST3(root);
}