-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNOTES.txt
212 lines (158 loc) · 4.34 KB
/
NOTES.txt
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
BINARY TREE
Full
Leaves have no children
Nodes that do have children, have 2 children
Complete
Like binary heaps
Fill out top to bottom, left to right
Perfect
Both complete and FULL
All leaves are on the same level
Traversals
N = Node, L = Left, R = Right
Preorder
N L R
Inorder
L N R
Postorder
L R N
AVL TREES
COURSE:
https://www.youtube.com/watch?v=FNeL18KsWPc
Visualized:
https://www.cs.usfca.edu/~galles/visualization/AVLtree.html
Threshold: used to determine when to rebalance tree
Every node roots it's own subtree
Has a subtree to the left and to the right
Height:
H: -1 (EMPTY)
H: 0 (SINGLE NODE)
H: max(H(L), H(R)) + 1
Balance:
Balance(node) = Height(Left) - Height(Right)
Standard threshold = 1
Goal of AVL: keep |Balance(node)| <= Threshold
LEFT-HEAVY: B(n) > 0 (positive balance)
RIGHT-HEAVY: B(n) < 0 (negative balance)
Rotations:
Rotations fix imbalance
Left-Heavy:
(1) Right rotation
(2) Left-Right rotation
Right-Heavy:
(1) Left rotation
(2) Right-Left rotation
Insertion:
1. Standard BST insert
2. Fix AVL property
imbalance(x)
if (rightHeavy(x)) is RIGHT HEAVY
if (rightChild is rightHeavy or balanced) {
LR(x)
}
else {
RR(y)
LR(x)
}
else if (leftHeavy(x))
if (leftChild is leftHeavy or balanced) {
RR(x)
}
else {
LR(y)
RR(x)
}
Deletion:
if no child
return NULL
if one child
return child
if two children
swap predecessor of inorder (max of left subtree)
delete node in it's new location (can't have 2 children now)
update height (by going up to all parents)
check balance of CUR node, and up the callstack
Red-Black Trees
Visualized:
https://www.cs.usfca.edu/~galles/visualization/RedBlack.html
https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-046j-introduction-to-algorithms-sma-5503-fall-2005/video-lectures/lecture-10-red-black-trees-rotations-insertions-deletions/
https://www.codesdope.com/course/data-structures-red-black-trees/
https://www.codesdope.com/course/data-structures-red-black-trees-deletion/
https://en.wikipedia.org/wiki/Red%E2%80%93black_tree
Intro to algo:
PRE: P.23
RBT: P.308
Invariant:
1. Every node has a colour either red or black.
2. The root of the tree is always black.
3. All leaves are black
4. There are no two adjacent red nodes (A red node cannot have a red parent or red child).
5. Every path from a node (including root) to any of its descendants NULL nodes has the same number of black nodes.
Insertion
- BST Operation
- Set color to red
- Fix the Red-Black property violation (if any)
Insert(x) {
bstInsert(x);
x->color = red;
fixProperty(x);
}
fixProperty(x) {
while (x->parent == red) {
if (uncle == red) {
// case 1
uncle = black;
parent = black;
grandparent = red;
// grandparent might be in violation so we have to check it
x = grandparent;
} else {
if (x == leftChild) {
// case 2
rotateLeft(parent);
x = parent;
}
// case 3 (and 2.5)
swap(parent->color, grandparent->color);
rotateRight(grandParent);
}
}
// In case the color of the root changed
root = black;
}
Tree Structure
Types:
iterator
const_iterator
Constructors/Destructors:
Tree(const value_compare& comp);
Tree(const Tree& from)
~Tree();
Tree& operator=(const Tree& x);
Iterators:
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
Capacity:
size()
max_size()
Modifiers:
iterator insert(const value_type& val);
iterator insert(iterator position, const value_type& val);
void erase(const value_type& val);
void erase(iterator position);
void swap(TreeRB& x);
void clear();
Observers:
value_compare value_comp() const;
Operations
iterator find(const value_type& val);
const_iterator find(const value_type& val) const;
size_type count(const value_type& val) const;
iterator lower_bound(const value_type& k);
const_iterator lower_bound(const value_type& k) const;
iterator upper_bound(const value_type& k);
const_iterator upper_bound(const value_type& k) const;
ft::pair<iterator, iterator> equal_range(const value_type& val);
ft::pair<const_iterator, const_iterator> equal_range(const value_type& val) const;