/
heigheight_BST_completely_done_by_me.cpp
executable file
·154 lines (148 loc) · 3.8 KB
/
heigheight_BST_completely_done_by_me.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
#include <bits/stdc++.h>
using namespace std;
struct node
{
int val;
struct node *left=NULL;
struct node *right=NULL;
}*root=NULL;
void inorder(struct node *root)
{struct node *current=root;
if(current->left!=NULL)inorder(current->left);
cout<<current->val<<" ";
if(current->right!=NULL)inorder(current->right);
}
void preorder(struct node *root)
{struct node *current=root;
cout<<current->val<<" ";
if(current->left!=NULL)preorder(current->left);
if(current->right!=NULL)preorder(current->right);
}
struct node* createBST(queue<int>v)
{
struct node *current,*newnode,*prev;
if(root==NULL)
{
newnode=new struct node;
root=newnode;
current=root;
newnode->val=v.front();
//cout<<"node created "<<v.front()<<endl;(was used by me for debugging)
v.pop();
}
while(!v.empty())
{
int z;
z=v.front();
v.pop();
int j;
current=root;
while(current!=NULL)
{
prev=current;
if(z<current->val) current=current->left;
else if(z>current->val) current=current->right;
}
current=prev;
if(z<current->val)
{
current->left=new struct node;
current=current->left;
// cout<<"node created l "<<z<<endl;(was used by me for debugging)
current->val=z;
}
else
{
current->right=new struct node;
current=current->right;
// cout<<"node created r "<<z<<endl;(was used by me for debugging)
current->val=z;
}
}
return root;
}
struct node* insert(struct node*,int z)
{struct node *current,*newnode,*prev;
if(root==NULL)
{
newnode=new struct node;
root=newnode;
current=root;
newnode->val=z;
//cout<<"node created "<<v.front()<<endl;(was used by me for debugging)
}
else
{
current=root;
while(current!=NULL)
{
prev=current;
if(z<current->val) current=current->left;
else if(z>current->val) current=current->right;
}
current=prev;
if(z<current->val)
{ cout<<z<<" is Inserted at left of "<<current->val;
current->left=new struct node;
current=current->left;
// cout<<"node created l "<<z<<endl;(was used by me for debugging)
current->val=z;
}
else
{cout<<z<<" is Inserted at right of "<<current->val;
current->right=new struct node;
current=current->right;
// cout<<"node created r "<<z<<endl;(was used by me for debugging)
current->val=z;
}
}
return root;
}
int height(struct node *root)
{
struct node *current=root;
int retl=0,retr=0,fet=0;
if(current->left!=NULL)retl=1+height(current->left);
if(current->right!=NULL)retr=1+height(current->right);
if(current->left==NULL&¤t->right==NULL)
{
return 0;
}
return fet=retl>retr?retl:retr;
}
int main()
{
int n,i;
cout<<"Enter size of BST : ";
cin>>n;
cout<<endl;
queue<int>v;
cout<<"Enter the elements of BST : ";
for(i=0;i<n;i++)
{
int z;
cin>>z;
v.push(z);
}
struct node *root=createBST(v);
cout<<endl;
cout<<"Inoder traversal : ";
inorder(root);
cout<<endl;
cout<<"Preoder traversal : ";
preorder(root);
cout<<endl;
insert(root,19);//just use a loop here if u want to create of up to ur wish
insert(root,23);
root=insert(root,24);
root=insert(root,18);
int h=height(root);
cout<<"\n Height of BST = "<<h<<endl;
cout<<endl;
cout<<"Inoder traversal : ";
inorder(root);
cout<<endl;
cout<<"Preoder traversal : ";
preorder(root);
return 0;
}