-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbbt.cpp
More file actions
155 lines (131 loc) · 3.68 KB
/
Copy pathbbt.cpp
File metadata and controls
155 lines (131 loc) · 3.68 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
// self-balancing binary search tree
// g++ bbt.cpp -o bbt -std=c++17
// insertion O(logn)
// deletion O(logn)
#include <cstddef>
#include <algorithm>
#include <iostream>
#include <string>
template<typename T>
class BBT {
private:
struct node {
T value;
node* left;
node* right;
// height of the subtree rooted at this node
std::size_t h;
node(T v) : value(v),left(nullptr),right(nullptr),h(1) {}
};
node* root{nullptr};
int height(node* n) {
if(!n) {return 0;}
return n->h;
}
void updateHeight(node* n) {
n->h = 1 + std::max(height(n->left), height(n->right));
}
int balance(node* n) {
return height(n->left) - height(n->right);
}
node* rotateRight(node* n) {
node* newRoot = n->left;
node* moved = newRoot->right;
n->left = moved;
newRoot->right = n;
updateHeight(n);
updateHeight(newRoot);
return newRoot;
}
node* rotateLeft(node* n) {
node* newRoot = n->right;
node* moved = newRoot->left;
n->right = moved;
newRoot->left = n;
updateHeight(n);
updateHeight(newRoot);
return newRoot;
}
node* insert_(node* current, T value) {
// return what the subtree root should be
if(!current) {
node* n = new node(value);
return n;
}
// BST Insertion
if(value < current->value) {
current->left = insert_(current->left, value);
}
else if(value > current->value) {
current->right = insert_(current->right, value);
} else {
return current;
}
// Self-Balance
updateHeight(current);
int bal = balance(current);
if(bal > 1) { // left-heavy
if(value < current->left->value) { // left-left
return rotateRight(current);
} else { // left-right
current->left = rotateLeft(current->left);
return rotateRight(current);
}
} else if(bal < -1) { // right-heavy
if(value > current->right->value) { // right-right
return rotateLeft(current);
} else { // right-left
current->right = rotateRight(current->right);
return rotateLeft(current);
}
} else {
// no rebalance
return current;
}
}
void printTree_(node* current, const std::string& prefix, bool isLast, const std::string& label) {
if(!current) {
return;
}
std::cout << prefix;
if(!label.empty()) {
std::cout << (isLast ? "`-- " : "|-- ") << label;
}
std::cout << current->value << " (h=" << current->h << ")\n";
if(current->left) {
printTree_(current->left,
prefix + (label.empty() ? "" : (isLast ? " " : "| ")),
!current->right,
"L: ");
}
if(current->right) {
printTree_(current->right,
prefix + (label.empty() ? "" : (isLast ? " " : "| ")),
true,
"R: ");
}
}
public:
void insert(T value) {
root = insert_(root, value);
}
void remove(T value) {}
void printTree() {
if(!root) {
std::cout << "(empty)\n";
return;
}
printTree_(root, "", true, "");
}
};
int main() {
BBT<int> t;
t.insert(0);
t.insert(1);
t.insert(2);
t.insert(3);
t.insert(4);
t.insert(5);
t.printTree();
return 0;
}