summaryrefslogtreecommitdiff
path: root/doc/unit/09/README.md
blob: 1ee9d51c564347b041c51a4af64eb06dd4832e7d (plain)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# 2-4 Trees

* is a rooted tree with the following properties:

* height: all leaves have the same depth
* degree: every internal node has 2, 3 or 4 children.

lemma:

> A 2-4 tree with n leaves has height at most log n.

# Red-Black Trees

[Lecture Video](https://www.youtube.com/watch?v=JMZkuYa04tY)

* A binary search tree with logarithmic height.

1. tree with `n` values has a height of most 2logn
1. The `add(x)` and `remove(x)` operations on a red-black tree run in `O(logn)` worst case time.
1. The amortized number of rotations performed during an `add(x)` or `remove(x)` operation is constant.

Each node, `u`, has a colour which is either `red` or `black`.

* red: is represented by the value 0.
* black: is represented by the value 1.

A red-black tree implements the SSet interface and supports
operations `add(x)`, `remove(x)`, and `find(x)` in O(logn) worst-case time
per operation.


```java
class Node<T> extends BSTNode<Node<T>, T> {
  byte colour;

  void flipRight(Node<T> u) {
    swapColours(u, u.left);
    rotateRight(u);
  }

  void flipLeft(Node<T> u) {
    swapColours(u, u.right);
    rotateLeft(u);
  }

  boolean add(T x) {
    Node<T> u = newNode(x);
    u.colour = red;
    boolean added = add(u);
    if (added)
      addFixup(u);
    return added;
  }

  void addFixup(Node<T> u) {
    while (u.colour == red) {
      if (u == r) {
        u.colour = black;
        return;
      }
      Node<T> w = u.parent;
      if (w.left.colour == black) {
        flipLeft(w);
        u = w;
        w = u.parent;
      }
      if (w.colour == black)
        return;
      Node<T> g = w.parent;
      if (g.right.colour == black) {
        flipRight(g);
        return;
      } else {
        pushBlack(g);
        u.x = w.x;
        u = w.right;
      }
    }
    splice(w);
    u.colour += w.colour;
    u.parent = w.parent;
    removeFixup(u);
    return true;
  }

  void removeFixup(Node<T> u) {
    while (u.colour > black) {
      if (u == r) {
        u.colour = black;
      } else if (u.parent.left.colour == red) {
        u = removeFixupCase1(u);
      } else if (u == u.parent.left) {
        u = removeFixupCase2(u);
      } else {
        u = removeFixupCase3(u);
      }
    }
    if (u != r) {
      Node<T> w = u.parent;
      if (w.right.colour == red && w.left.colour == black) {
        flipLeft(w);
      }
    }
  }

  Node<T> removeFixupCase3(Node<T> u) {
    Node<T> w = u.parent;
    Node<T> v = w.left;
    pullBlack(w);
    flipRight(w);
    Node<T> q = w.left;
    if (q.colour == red) {
      rotateRight(w);
      flipLeft(v);
      pushBlack(q);
      return q;
    } else {
      if (v.left.colour == red) {
        pushBlack(v);
        return v;
      } else {
        flipLeft(v);
        return w;
      }
    }
  }
}
```

The following properties are satisfied before and after any operation:

* black-height: there are the same # of black nodes on every root to the leaf path. (sum of colours on any root to leaf path is the same.)
* no-red-edge: No two red nodes are adjacent. (except the root, `u.colour + u.parent.colour >= 1`)

The root is black.


## AVL Trees

AVL trees are height-balanced.

* height-balanced: at each node `u`, the height of the subtree rooted at `u.left` and the subtree rooted at `u.right` differ by at most one.

AVL trees have a smaller height than red-black trees. The height
balancing can be maintained during `add(x)` and `remove(x)` operations
by walking back up the path to the root and performing a rebalancing
operation at each node `u` where the height of `u`'s left and right subtrees differ by two.

AVL is a self balancing binary search tree in which each node maintains
extra information called a balance factor whose value is either -1, 0, or +1.

The balance factor is determined by calculating the difference
between the height of the left subtree and that of the right subtree of that node.

Balance Factor = (Height of left subtree - height of right subtree) or (height of right subtree - height of left subtree).

The self balancing property of an avl tree is maintained by the balance factory.

E.g.

```plaintext
        (33) 1
      /     \
   (9) -1    (53) -1
  /   \        \
(8) 0 (21) 1    (61) 0
     /
  (11) 0
```

### Operations

* Left Rotate: nodes on the right is transformed into arrangement on the left.
* Right Rotate: nodes on the left are transformed into arrangment on the right.
* Left-Right and Right-Left Rotate: shift left then right.


Left Rotate:

```plaintext
1.
  (x)
    \
    (y)

2.

   (y)
  /
(x)

```

Right Rotate:

```plaintext
1.
  (y)
  /
(x)

2.
(x)
  \
  (y)
```

Left-Right Rotate:

```plaintext
1.
  (z)
  /
(x)
  \
  (y)

2.
    (z)
    /
  (y)
  /
(x)

3.
  (y)
  / \
(x) (z)
```


```plaintext
1.

(z)
  \
   (x)
  /
(y)

2.

(z)
  \
   (y)
     \
     (x)

3.
   (y)
  /   \
(z)   (x)
```