Skip to content

Commit 833bae6

Browse files
author
axptwig
committed
Readme Update
1 parent 285a1e9 commit 833bae6

File tree

1 file changed

+63
-2
lines changed

1 file changed

+63
-2
lines changed

Red Black Trees/README.md

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,69 @@
11
# Red Black Tree
22

3-
Under Construction.
3+
Red-black trees are an evolution of binary search trees that aim to keep the tree balanced without affecting the complexity of the primitive operations. This is done by coloring each node in the tree with either red or black and preserving a set of properties that guarantee that the deepest path in the tree is not longer than twice the shortest one.
4+
## Motivation:
5+
* We want a balanced binary search tree
6+
* Height of the tree is O(log n)
7+
* Red-Black Tree is an implementation of a balanced binary search tree
48

5-
See also [Wikipedia](https://en.wikipedia.org/wiki/Red%E2%80%93black_tree).
9+
10+
## Invariants:
11+
1. Every node is colored either red or black
12+
2. All leaf (nil) nodes are colored with black; if a node’s child is missing then we will assume that it has a nil child in that place and this nil child is always colored black.
13+
3. Both children of a red node must be black nodes.
14+
4. Every path from a node n to a descendent leaf has the same number of black nodes (not counting node n).
15+
16+
## Methods:
17+
18+
Since the red-black tree is a balanced BST, it supports the functions:
19+
* find(key)
20+
* Predecessor(key)
21+
* Successor(key)
22+
* Minimum(key)
23+
* maximum(key)
24+
* insert(key)
25+
* delete(key)
26+
27+
Since an insertion or deletion may violate one of the invariant's of the red-black tree we must either change colors, or perform rotation actions.
28+
29+
#Rotation
30+
To ensure that its color scheme and properties don’t get thrown off, red-black trees employ a key operation known as rotation. Rotation is a binary operation, between a parent node and one of its children, that swaps nodes and modifys their pointers while preserving the inorder traversal of the tree (so that elements are still sorted). There are two types of rotations: left rotation and right rotation. Left rotation swaps the parent node with its right child, while right rotation swaps the parent node with its left child.
31+
32+
###Left-Rotation
33+
```c++
34+
y ← x->right
35+
x->right ← y->left
36+
y->left->p ← x
37+
y->p ← x->p
38+
if x->p = Null
39+
then T->root ← y
40+
else if x = x->p->left
41+
then x->p->left ← y
42+
else x->p->right ← y
43+
y->left ← x
44+
x->p ← y
45+
```
46+
###Right-Rotation
47+
```c++
48+
y ← x->left
49+
x->left ← y->right
50+
y->right->p ← x
51+
y->p ← x->p
52+
if x->p = Null
53+
then T->root ← y
54+
else if x = x->p->right
55+
then x->p->right ← y
56+
else x->p->left ← y
57+
y->right ← x
58+
x->p ← y
59+
```
60+
###Insertion
61+
When adding a new node to a binary search tree, note that the new node will always be a leaf in the tree. To insert a new node, all we will do is navigate the BST starting from the root. If the new node value is smaller than the current node value, we go left – if it is larger, we go right. When we reach a leaf node, the last step is to attach the new node as a child to this leaf node in a way that preserves the BST constraint. We must recheck the RBTree invariants to see if any were violated
62+
63+
###Deletion
64+
The same concept behind red-black tree insertions applies here. Removing a node from a red-black tree makes use of the BST deletion procedure and then restores the red-black tree properties in O(log n). The total running time for the deletion process takes O(log n) time, then, which meets the complexity requirements for the primitive operations.
65+
66+
See also [Wikipedia](https://en.wikipedia.org/wiki/Red%E2%80%93black_tree).
667

768
*Written for the Swift Algorithm Club by Ashwin Raghuraman*
869

0 commit comments

Comments
 (0)