-
Notifications
You must be signed in to change notification settings - Fork 1
/
bst.cpp
197 lines (166 loc) · 4.98 KB
/
bst.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <bits/stdc++.h>
using namespace std;
struct Node {
int key;
struct Node *left, *right;
};
Node* newNode(int item){
Node* temp = new Node;
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
Node* insert(Node* Node, int key){
if (Node == NULL)
return newNode(key);
if (key < Node->key)
Node->left = insert(Node->left, key);
else
Node->right = insert(Node->right, key);
return Node;
}
Node* deleteNode(Node* root, int k){
if (root == NULL)
return root;
if (root->key > k) {
root->left = deleteNode(root->left, k);
return root;
}
else if (root->key < k) {
root->right = deleteNode(root->right, k);
return root;
}
if (root->left == NULL) {
Node* temp = root->right;
delete root;
return temp;
}
else if (root->right == NULL) {
Node* temp = root->left;
delete root;
return temp;
}
else {
Node* succParent = root;
Node *succ = root->right;
while (succ->left != NULL) {
succParent = succ;
succ = succ->left;
}
if (succParent != root)
succParent->left = succ->right;
else
succParent->right = succ->right;
root->key = succ->key;
delete succ;
return root;
}
}
int maxDepth(struct Node* Node){
if (Node==NULL)
return 0;
else
{
int lDepth = maxDepth(Node->left);
int rDepth = maxDepth(Node->right);
if (lDepth > rDepth)
return(lDepth+1);
else return(rDepth+1);
}
}
int getLeafCount(struct Node* Node){
if(Node == NULL)
return 0;
if(Node->left == NULL && Node->right == NULL)
return 1;
else
return getLeafCount(Node->left)+
getLeafCount(Node->right);
}
int countNonleaf(struct Node* root){
if (root == NULL || (root->left == NULL &&
root->right == NULL))
return 0;
return 1 + countNonleaf(root->left) +
countNonleaf(root->right);
}
void printPreorder(struct Node* Node){
if (Node == NULL)
return;
cout << Node->key << " ";
printPreorder(Node->left);
printPreorder(Node->right);
}
void printPostorder(struct Node* Node){
if (Node == NULL)
return;
printPostorder(Node->left);
printPostorder(Node->right);
cout << Node->key << " ";
}
void inOrder(struct Node *root){
stack<Node *> s;
Node *curr = root;
while (curr != NULL || s.empty() == false)
{
while (curr != NULL)
{
s.push(curr);
curr = curr->left;
}
curr = s.top();
s.pop();
cout << curr->key << " ";
curr = curr->right;
}
}
int main(){
//1. creating BST 5,3,2,4,7,6,8
Node* root = NULL;
root = insert(root, 5);
root = insert(root, 3);
root = insert(root, 2);
root = insert(root, 4);
root = insert(root, 7);
root = insert(root, 6);
root = insert(root, 8);
int a,x;
while(1){
cout<<"\n\n----------------------------------------------------------------------"<<endl;
cout<<"\n\n1.Insert in BST.\n2.Delete a Node\n3.Print Height of BST\n4.Node count (internal and external)\n5.Pre-order traversal(recursive)\n";
cout<<"6.Post-order Traversal(recursive)\n7.In-order Traversal(iterative)\n8.Exit\n\n\nEnter option:";
cin>>a;
cout<<"\n\n";
switch(a){
case 1: cout<<"Enter number to insert.";
cin>>x;
insert(root,x);
cout<<"\n\nNode Inserted...\n";
break;
case 2: cout<<"Enter number to delete ";
cin>>x;
root = deleteNode(root, x);
cout<<"\n"<<x<<"deleted\n";
break;
case 3: cout<<"Height of tree: "<<maxDepth(root);
break;
case 4: cout<<"External Nodes of the BST is : "<<getLeafCount(root)<<endl;
cout<<"Internal Nodes of the BST is : "<<countNonleaf(root);
break;
case 5: cout<<"\nPreorder traversal of binary tree is \n";
printPreorder(root);
break;
case 6: cout<<"\nPostorder traversal of binary tree is \n";
printPostorder(root);
break;
case 7: cout<<"In-order traversal of the BST (iterative) : "<<endl;
inOrder(root);
break;
case 8: cout<<"Exiting...";
exit(0);
default:cout<<"Enter a valid option.";
break;
}
}
return 0;
}