-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVaibhav_21104094_Assignment6.cpp
More file actions
190 lines (147 loc) · 4.25 KB
/
Vaibhav_21104094_Assignment6.cpp
File metadata and controls
190 lines (147 loc) · 4.25 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
/* Vaibhav
21104094
Assignment 6
Comparing Binary Search and Binary Search Tree (BST)
*/
#include <bits/stdc++.h>
using namespace std;
//Creating node of a tree
class BinaryTreeNode{
public:
int data;
BinaryTreeNode*left;
BinaryTreeNode*right;
BinaryTreeNode(int data){
this -> data = data;
left = NULL;
right = NULL;
}
};
//Function to insert data in a Binary Search Tree
BinaryTreeNode* insertInBST(BinaryTreeNode*root, int data){
if(root == NULL){
BinaryTreeNode* new_node = new BinaryTreeNode(data);
root=new_node;
return root;
}
if(data > root -> data){
root -> right = insertInBST(root->right,data);
}
else if(data < root->data){
root -> left = insertInBST(root->left,data);
}
else{
return root;
}
return root;
}
//Function to print a tree using Level Order Traversal
void levelOrderTraversal(BinaryTreeNode* root){
queue <BinaryTreeNode*> q;
q.push(root);
q.push(NULL);
while(!q.empty()){
BinaryTreeNode* temp=q.front();
q.pop();
if(temp == NULL){
cout << endl;
if(!q.empty()){q.push(NULL);};
}
else{
cout << temp->data<<" ";
if(temp->left){
q.push(temp->left);
}
if(temp->right){
q.push(temp->right);
}
}
}
}
//Function to get min value node in a Binary Search Tree
BinaryTreeNode* min_value_node(BinaryTreeNode* node){
BinaryTreeNode* current = node;
while (current && current->left != NULL){
current = current -> left;
}
return current;
}
//Function to delete a node in Binary Search Tree
BinaryTreeNode* deleteNode(BinaryTreeNode* root, int key)
{
if(root == NULL){
return root;
}
if(key < root->data){
root->left = deleteNode(root->left, key);
}
else if(key > root->data){
root->right = deleteNode(root->right, key);
}
else{
if(root->left==NULL and root->right==NULL){
return NULL;
}
else if(root -> left == NULL){
BinaryTreeNode* temp = root -> right;
free(root);
return temp;
}
else if(root -> right == NULL){
BinaryTreeNode* temp = root -> left;
free(root);
return temp;
}
BinaryTreeNode* temp = min_value_node(root -> right);
root -> data = temp -> data;
root -> right = deleteNode(root->right, temp->data);
}
return root;
}
//Function to print an array
void printArr(int arr[],int n){
for(int i = 0; i < n; i++){
cout << arr[i] << " ";
}
}
int main(){
//Taking number of element in array as input
int n;
cout << "\nEnter number elements in the ARRAY to form BST:";
cin >> n;
int arr[n];
cout << "\nEnter elements of the array to form BST:";
//Taking elements of array as input
for(int i = 0; i < n; i++){
cin >> arr[i];
}
//printing the array
cout << "\nArray:[";
printArr(arr,n);
cout << "]";
cout << endl;
//root is the top most node of Binary Search Tree
BinaryTreeNode* root = NULL;
//Inserting element of the array in Binary Search Tree
for(int i =0 ;i < n; i++){
root=insertInBST(root,arr[i]);
}
//Displaying Binary Search Tree using Level Order Traversal
cout << "\nBinary Search Tree using Level Order Traversal\n" << endl;
levelOrderTraversal(root);
//key is the data to be deleted from Binary Search Tree
int key;
cout << "\nEnter data to be deleted from BST:";
cin >> key;
//deleting key from Binary Search Tree
root = deleteNode(root,key);
//Displaying Binary Search Tree after deletion of key
cout << "\nBinary Search Tree after deleting:" << key << endl;
cout<<endl;
levelOrderTraversal(root);
}
/*
If an array has n elements and all elements are inserted in binary
search tree than the tree has space complexity of O(n) and the array
has also the space complexity of O(n)
*/