-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreeAlgorithms.cpp
More file actions
127 lines (110 loc) · 2.58 KB
/
BinaryTreeAlgorithms.cpp
File metadata and controls
127 lines (110 loc) · 2.58 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
#include<bits/stdc++.h>
using namespace std;
// all binary search tree algorithm
class node{
public:
int data;
node* left;
node* right;
node(int d){
data=d;
left=NULL;
right=NULL;
}
};
node* build_bst(node* root){
int data;
cin>>data;
if(data==-1)return NULL;
root=new node(data);
root->left=build_bst(root->left);
root->right=build_bst(root->right);
return root;
}
// checking for bst
bool checkBst(node* root, int min=INT_MIN,int max=INT_MAX){
if(root==NULL)return true;
if(min<=root->data && max>=root->data && checkBst(root->left,min,root->data)&& checkBst(root->right,root->data,max)){
return true;
}
else return false;
}
// checking if bst is balanced or not
class helper{
public:
int h;
bool balanced;
};
helper isBalanced(node* root){
helper p;
if(root==NULL){
p.h=0;
p.balanced=true;
return p;
}
helper l=isBalanced(root->left);
helper r=isBalanced(root->right);
p.h=max(l.h,r.h)+1;
if(l.balanced && r.balanced && abs(l.h-r.h)<=1){
p.balanced=true;
}
else p.balanced=false;
return p;
}
// level order traversal of binary tree
int height(node* root){
if(root==NULL)return 0;
return max(height(root->left),height(root->right))+1;
}
vector<vector<int> > levelOrder(node* root){
queue<node*> q;
q.push(root);
q.push(NULL);
vector<vector<int> > v(height(root));
int i=0;
node* prev_node=NULL;
while(!q.empty()){
node* cur_node=q.front();
q.pop();
if(cur_node)
{
v[i].push_back(cur_node->data);
}
if(cur_node && cur_node->left!=NULL){q.push(cur_node->left);}
if(cur_node && cur_node->right!=NULL){q.push(cur_node->right);}
if(cur_node==NULL && prev_node!=NULL){q.push(NULL);i++;}
prev_node=cur_node;
}
return v;
}
void in_order(node* root){
if(root==NULL)return;
else{
in_order(root->left);
cout<<root->data<<" ";
in_order(root->right);
}
}
int main(){
node* root=build_bst(root);
cout<<" \n the in order traversal of tree "<<endl;
in_order(root);
cout<<" printing the level order traversal of the tree "<<endl;
vector<vector<int> >v=levelOrder(root);
cout<<" comming here "<<endl;
for(int i=0;i<v.size();i++){
for(int j=0;j<v[i].size();j++){
cout<<v[i][j]<<" ";
}
cout<<endl;
}
cout<<" checking bst "<<endl;
bool bst=checkBst(root);
if(bst)cout<<" is bst "<<endl;
else cout<<" is not bst "<<endl;
cout<<" checking id bst is balanced "<<endl;
helper bal=isBalanced(root);
if(bal.balanced)cout<<" is balanced "<<endl;
else cout<<" is not balanced "<<endl;
}
//10 5 -1 -1 15 6 -1 -1 20 -1 -1