❖ Red-Black Trees |
Red-black trees are a representation of 2-3-4 trees using BST nodes.
❖ ... Red-Black Trees |
Definition of a red-black tree
Search algorithm: standard BST search
❖ ... Red-Black Trees |
Representing 4-nodes in red-black trees:
Some texts colour the links rather than the nodes.
❖ ... Red-Black Trees |
Red-black tree implementation:
typedef enum {RED,BLACK} Colour; typedef struct node *RBTree; typedef struct node { int data; // actual data Colour colour; // relationship to parent RBTree left; // left subtree RBTree right; // right subtree } node; #define colour(tree) ((tree) != NULL && (tree)->colour) #define isRed(tree) ((tree) != NULL && (tree)->colour == RED)
RED
BLACK
❖ ... Red-Black Trees |
New nodes are always red ...
RBTree newNode(Item it) { RBTree new = malloc(sizeof(Node)); assert(new != NULL); data(new) = it; color(new) = RED; left(new) = right(new) = NULL; return new; }
.. because they're always inserted into a leaf node
❖ ... Red-Black Trees |
Node.red
❖ Searching in Red-black Trees |
Search method is standard BST search:
searchRedBlack(tree,item):
| Input tree, item
| Output true if item found in tree,
| false otherwise
|
| if tree is empty then
| return false
| else if item < data(tree) then
| return SearchRedBlack(left(tree),item)
| else if item > data(tree) then
| return SearchRedBlack(right(tree),item)
| else // found
| return true
| end if
❖ Insertion in Red-Black Trees |
Insertion is more complex than for standard BSTs
❖ ... Insertion in Red-Black Trees |
High-level description of insertion algorithm:
insertRedBlack(tree,item): | Input red-black tree, item | Output tree with item inserted | | tree = insertRB(tree,item,false) | colour(tree) = BLACK | return tree
Having restructured the tree, it then makes the root node BLACK
❖ ... Insertion in Red-Black Trees |
Overview of the recursive function ...
insertRB(tree,item,inRight):
| Input tree, item, inRight
| // inRight = direction of last branch
| Output tree with item inserted
|
| if tree is empty then return newNode(item)
| if data(tree) = item then return tree
| if isRed(left(tree)) ∧ isRed(right(tree)) then
| split 4-node
| end if
| recursive insert cases (cf. regular BST)
| re-arrange links/colours after insert
| return modified tree
❖ ... Insertion in Red-Black Trees |
Splitting a 4-node, in a red-black tree:
Algorithm:
if isRed(left(tree)) ∧ isRed(right(tree)) then colour(tree) = RED colour(left(tree)) = BLACK colour(right(tree)) = BLACK end if
❖ ... Insertion in Red-Black Trees |
Simple recursive insert (a la BST):
Algorithm:
if item < data(tree)) then
left(tree) = insertRB(left(tree),item,false)
re-arrange links/colours after insert
else // item larger than data in root
right(tree) = insertRB(right(tree),item,true)
re-arrange links/colours after insert
end if
❖ ... Insertion in Red-Black Trees |
Re-arrangement #1: two successive red links = newly-created 4-node
Algorithm:
if isRed(left(tree)) ∧ isRed(left(left(tree))) then tree=rotateRight(tree) colour(tree)=BLACK colour(right(tree))=RED end if
❖ ... Insertion in Red-Black Trees |
Re-arrangement #2: "normalise" direction of successive red links
Algorithm:
if inRight ∧ isRed(tree) ∧ isRed(left(tree)) then tree=rotateRight(tree) end if
❖ ... Insertion in Red-Black Trees |
Example of insertion, starting from empty tree:
22, 12, 8, 15, 11, 19, 43, 44, 45, 42, 41, 40, 39
To see how built: www.cs.usfca.edu/~galles/visualization/RedBlack.html
❖ Red-black Tree Performance |
Cost analysis for red-black trees:
Note: red-black trees were popularised by Sedgewick.
Produced: 18 Jun 2020