-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBSTbasic.cpp
More file actions
126 lines (118 loc) · 3.04 KB
/
BSTbasic.cpp
File metadata and controls
126 lines (118 loc) · 3.04 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
#include<iostream>
using namespace std;
struct Node{
int key;
Node *left, *right;
Node(int k){
key = k;
left = right = NULL;
}
};
bool search(Node* root, int val){ //Time: O(h), aux space(recursive):O(h), aux space by iterative:O(1) (here we're talking about normal BST)
if(root == NULL){
return false;
}
if(root -> key == val){
return true;
}
else if(root -> key < val){
return search(root -> right, val);
}
else{
return search(root -> left, val);
}
}
Node* insert(Node* root, int x){ //Time : O(h), Aux.space = O(1)
Node *temp = new Node(x);
Node* curr = root;
Node* parent = NULL;
while(curr != NULL){
parent = curr;
if(curr -> key == x){
return root;
}
else if(curr -> key < x){
curr = curr -> right;
}
else{
curr = curr -> left;
}
}
if(parent == NULL){
return temp;
}
if(parent -> key < x){
parent -> right = temp;
}
else {
parent -> left = temp;
}
return root;
}
Node* insertRecursive(Node* root, int x){//Time: O(h), Aux.space:O(h)
if(root == NULL){
return new Node(x);
}
else if(root -> key < x){
root -> right = insertRecursive(root -> right, x);
}
else{
root -> left = insertRecursive(root -> left, x);
}
return root;
}
void preorderTraversal(Node* root){
if(root != NULL){
cout << root -> key << " ";
preorderTraversal(root -> left);
preorderTraversal(root -> right);
}
}
Node* getSuccessor(Node* curr){ //works only when right child of a node is not empty
curr = curr -> right;
while(curr != NULL && curr -> left != NULL){ //closest value is the leftmost leaf of the right child
curr = curr -> left;
}
return curr;
}
Node* delNode(Node* root, int x){ //Time: O(h), aux.space = O(h)
if(root == NULL){
return root;
}
if(root -> key < x){
root -> right = delNode(root -> right, x);
}
else if(root -> key > x){
root -> left = delNode(root -> left, x);
}
else{
if(root -> left == NULL){ //if there is only right child present or both children are not there
Node* temp = root -> right;
delete root;
return temp;
}
else if(root -> right == NULL){ //if there is only left child present
Node* temp = root -> left;
delete root;
return temp;
}
else{ //if both children are present
Node* succ = getSuccessor(root);
root -> key = succ -> key;
root -> right = delNode(root -> right, succ -> key);
}
}
return root;
}
int main(){
Node* root = NULL;
root = insert(root, 10);
root = insert(root, 20);
root = insert(root, 8);
root = insert(root, 5);
root = insertRecursive(root, 30);
root = delNode(root, 10);
preorderTraversal(root);
// cout << search(root, 20);
return 0;
}