This repository was archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExponential_Search_Tree.cpp
More file actions
166 lines (166 loc) · 4.53 KB
/
Exponential_Search_Tree.cpp
File metadata and controls
166 lines (166 loc) · 4.53 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
#include <iostream>
#include <vector>
using namespace std;
class ESTNode {
public:
int key;
int size;
ESTNode* left;
ESTNode* right;
ESTNode(int k) : key(k), size(1), left(nullptr), right(nullptr) {}
};
class ExponentialSearchTree {
private:
ESTNode* root;
void updateSize(ESTNode* node) {
if (node != nullptr) {
node->size = 1 + getSize(node->left) + getSize(node->right);
}
}
int getSize(ESTNode* node) const {
return (node != nullptr) ? node->size : 0;
}
ESTNode* rotateRight(ESTNode* y) {
ESTNode* x = y->left;
y->left = x->right;
x->right = y;
updateSize(y);
updateSize(x);
return x;
}
ESTNode* rotateLeft(ESTNode* x) {
ESTNode* y = x->right;
x->right = y->left;
y->left = x;
updateSize(x);
updateSize(y);
return y;
}
ESTNode* insert(ESTNode* root, int key) {
if (root == nullptr) {
return new ESTNode(key);
}
if (key < root->key) {
root->left = insert(root->left, key);
root = rotateRight(root);
} else {
root->right = insert(root->right, key);
root = rotateLeft(root);
}
return root;
}
ESTNode* deleteNode(ESTNode* root, int key) {
if (root == nullptr) {
return root;
}
if (key < root->key) {
root->left = deleteNode(root->left, key);
} else if (key > root->key) {
root->right = deleteNode(root->right, key);
} else {
if (root->left == nullptr) {
ESTNode* temp = root->right;
delete root;
return temp;
} else if (root->right == nullptr) {
ESTNode* temp = root->left;
delete root;
return temp;
}
if (getSize(root->left) > getSize(root->right)) {
root = rotateRight(root);
root->right = deleteNode(root->right, key);
} else {
root = rotateLeft(root);
root->left = deleteNode(root->left, key);
}
}
return root;
}
ESTNode* search(ESTNode* root, int key) const {
if (root == nullptr || root->key == key) {
return root;
}
if (key < root->key) {
return search(root->left, key);
} else {
return search(root->right, key);
}
}
void inOrderTraversal(ESTNode* root) const {
if (root != nullptr) {
inOrderTraversal(root->left);
cout << root->key << " ";
inOrderTraversal(root->right);
}
}
void preOrderTraversal(ESTNode* root) const {
if (root != nullptr) {
cout << root->key << " ";
preOrderTraversal(root->left);
preOrderTraversal(root->right);
}
}
void postOrderTraversal(ESTNode* root) const {
if (root != nullptr) {
postOrderTraversal(root->left);
postOrderTraversal(root->right);
cout << root->key << " ";
}
}
public:
ExponentialSearchTree() : root(nullptr) {}
void insert(int key) {
root = insert(root, key);
}
void deleteNode(int key) {
root = deleteNode(root, key);
}
ESTNode* search(int key) const {
return search(root, key);
}
void inOrderTraversal() const {
inOrderTraversal(root);
cout << endl;
}
void preOrderTraversal() const {
preOrderTraversal(root);
cout << endl;
}
void postOrderTraversal() const {
postOrderTraversal(root);
cout << endl;
}
};
int main() {
ExponentialSearchTree est;
est.insert(50);
est.insert(30);
est.insert(70);
est.insert(20);
est.insert(40);
est.insert(60);
est.insert(80);
cout << "In-order traversal: ";
est.inOrderTraversal();
cout << "Pre-order traversal: ";
est.preOrderTraversal();
cout << "Post-order traversal: ";
est.postOrderTraversal();
cout << "Searching for key 40: ";
ESTNode* result = est.search(40);
if (result != nullptr) {
cout << "Found!" << endl;
} else {
cout << "Not Found!" << endl;
}
est.deleteNode(40);
cout << "After deleting key 40:" << endl;
cout << "In-order traversal: ";
est.inOrderTraversal();
cout << "Pre-order traversal: ";
est.preOrderTraversal();
cout << "Post-order traversal: ";
est.postOrderTraversal();
return 0;
}