This repository has been 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
/
Tango_Tree.cpp
147 lines (147 loc) · 4.41 KB
/
Tango_Tree.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
#include <iostream>
class TangoTreeNode {
public:
int key;
TangoTreeNode* left;
TangoTreeNode* right;
int size;
TangoTreeNode(int k) : key(k), left(nullptr), right(nullptr), size(1) {}
};
class TangoTree {
private:
TangoTreeNode* root;
public:
TangoTree() : root(nullptr) {}
void insert(int key) {
root = insertRecursive(root, key);
}
bool search(int key) const {
return searchRecursive(root, key);
}
void inOrderTraversal() const {
std::cout << "In-order traversal: ";
inOrderTraversalRecursive(root);
std::cout << std::endl;
}
void preOrderTraversal() const {
std::cout << "Pre-order traversal: ";
preOrderTraversalRecursive(root);
std::cout << std::endl;
}
void postOrderTraversal() const {
std::cout << "Post-order traversal: ";
postOrderTraversalRecursive(root);
std::cout << std::endl;
}
private:
TangoTreeNode* insertRecursive(TangoTreeNode* node, int key) {
if (node == nullptr) {
return new TangoTreeNode(key);
}
if (key < node->key) {
node->left = insertRecursive(node->left, key);
} else {
node->right = insertRecursive(node->right, key);
}
updateSize(node);
return balance(node);
}
bool searchRecursive(TangoTreeNode* node, int key) const {
if (node == nullptr) {
return false;
}
if (key == node->key) {
return true;
} else if (key < node->key) {
return searchRecursive(node->left, key);
} else {
return searchRecursive(node->right, key);
}
}
void inOrderTraversalRecursive(const TangoTreeNode* node) const {
if (node) {
inOrderTraversalRecursive(node->left);
std::cout << node->key << " ";
inOrderTraversalRecursive(node->right);
}
}
void preOrderTraversalRecursive(const TangoTreeNode* node) const {
if (node) {
std::cout << node->key << " ";
preOrderTraversalRecursive(node->left);
preOrderTraversalRecursive(node->right);
}
}
void postOrderTraversalRecursive(const TangoTreeNode* node) const {
if (node) {
postOrderTraversalRecursive(node->left);
postOrderTraversalRecursive(node->right);
std::cout << node->key << " ";
}
}
void updateSize(TangoTreeNode* node) {
if (node) {
node->size = 1;
if (node->left) {
node->size += node->left->size;
}
if (node->right) {
node->size += node->right->size;
}
}
}
TangoTreeNode* balance(TangoTreeNode* node) {
if (node == nullptr) {
return nullptr;
}
if (getSize(node->left) > 2 * getSize(node->right)) {
if (getSize(node->left->left) < getSize(node->left->right)) {
node->left = rotateLeft(node->left);
}
node = rotateRight(node);
} else if (getSize(node->right) > 2 * getSize(node->left)) {
if (getSize(node->right->right) < getSize(node->right->left)) {
node->right = rotateRight(node->right);
}
node = rotateLeft(node);
}
return node;
}
TangoTreeNode* rotateRight(TangoTreeNode* node) {
TangoTreeNode* newRoot = node->left;
node->left = newRoot->right;
newRoot->right = node;
updateSize(node);
updateSize(newRoot);
return newRoot;
}
TangoTreeNode* rotateLeft(TangoTreeNode* node) {
TangoTreeNode* newRoot = node->right;
node->right = newRoot->left;
newRoot->left = node;
updateSize(node);
updateSize(newRoot);
return newRoot;
}
int getSize(const TangoTreeNode* node) const {
return (node ? node->size : 0);
}
};
int main() {
TangoTree tangoTree;
tangoTree.insert(3);
tangoTree.insert(1);
tangoTree.insert(4);
tangoTree.insert(2);
tangoTree.insert(5);
tangoTree.inOrderTraversal();
tangoTree.preOrderTraversal();
tangoTree.postOrderTraversal();
int keyToSearch = 2;
if (tangoTree.search(keyToSearch)) {
std::cout << "Key " << keyToSearch << " found.\n";
} else {
std::cout << "Key " << keyToSearch << " not found.\n";
}
return 0;
}