690 likes | 940 Views
2-3-4 Trees and Red-Black Trees. Gordon College Prof. Brinton. Regular Binary Trees. Insertion sequence: 9, 5, 20, 3, 7, 15, 75, 6, 12, 17, 35, 100, 18, 25, 40. Insertion sequence: 5, 15, 20, 3, 9, 7, 12, 17, 6, 75, 100, 18, 25, 35, 40. Balanced Trees.
E N D
2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton
Regular Binary Trees Insertion sequence: 9, 5, 20, 3, 7, 15, 75, 6, 12, 17, 35, 100, 18, 25, 40 Insertion sequence: 5, 15, 20, 3, 9, 7, 12, 17, 6, 75, 100, 18, 25, 35, 40
Balanced Trees • Need a new search-tree structure - a balanced binary search tree • Maintains balanced node when adding or removing them Extra time needed at insert and remove • Guarantees at worst O(log n) search time We start with 2-3-4 trees… - Perfectly balanced - Difficult and inefficient to implement
2-3-4 Trees Definitions 2-node - a data value and pointers to 2 subtrees 3-node - two data values and pointers to 3 subtrees 4-node - three data values and pointers to 4 subtrees A<B A<B<C
2-3-4 Tree What is the basic 2-3-4 search algorithm? 2-3-4 trees have search tree properties
Inserting into a 2-3-4 Tree C Insert begins with a single node and adds elements until it is full Insert another item 1. splitthe 4 node using the median value as the parent (promoting the median value to the parent level) 2. Insert the new item (inserted based on the BST rules for insertion) (always insert node in leaf of tree)
2-3-4 Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7
2-3-4 Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7
2-3-4 Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7
2-3-4 Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7
2-3-4 Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7
2-3-4 Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7
2-3-4 Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 (4,12,25) Proactive top-down approach to splitting a 4-node
2-3-4 Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 (4,12,25)
2-3-4 Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7
2-3-4 Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7
Another example Keys: A S E R C H I N G X What would the 2-3-4 tree look like after inserting this set of keys?
Another example Keys: A S E R C H I N G X A
Another example Keys: A S E R C H I N G X A S
Another example Keys: A S E R C H I N G X A E S
Another example Keys: A S E R C H I N G X E R S A
Another example Keys: A S E R C H I N G X E R S A C
Another example Keys: A S E R C H I N G X E H R S A C
Another example Keys: A S E R C H I N G X E R H I S A C
Another example Keys: A S E R C H I N G X E R H I N S A C
Another example Keys: A S E R C H I N G X E I R S G H N A C
Another example Keys: A S E R C H I N G X I E R S X G H N A C
2-3-4 Tree Facts • With N elements, the max number of nodes visited during the search for an element is int(log2 n) + 1 • Inserting an element into a tree with n elements requires splitting no more than int(log2 n) + 1 4-nodes (often far fewer) • Problem: allocated a large amount of wasted space 3n + 1 unused pointers (n - nodes) 28 wasted pointers
2-3-4 Warmup Exercise Create a 2-3-4 tree from the following sequence of numbers: 45 6 23 5 78 9 10 11 48 99 12 55
2-3-4 Warmup Exercise Create a 2-3-4 tree from the following sequence of numbers: 45 6 23 5 78 9 10 11 48 99 12 55
Red-Black Trees • Designed to represent 2-3-4 tree without the additional link overhead. • Colors are used to represent the 3-node and 4-node. Red-Black trees are simple binary trees with a color indicator that is used to maintain certain properties - these properties keep the tree balanced.
N I H I N N H Red-Black Nodes 2-nodes simple binary node (black node) 4-nodes center value becomes the parent (black) with outside values becoming the children (red)
A B A B or B A Red-Black Nodes 3-nodes Note: Red-black trees are not unique However, the corresponding 2-3-4 tree is unique
N I N H Red-Black Nodes A Use color grouping of nodes to indicate the corresponding nodes in the 2-3-4 tree B B A
Converting a 2-3-4 Tree to Red-Black Tree Example Top-down conversion algorithm: (start at the root) Apply red-black tree representation to each node Repeat for next level…
I H I N N H Converting a 2-3-4 Tree to Red-Black Tree Example
Converting a 2-3-4 Tree to Red-Black Tree Example How could this be different?
Red-Black Tree Properties • The Root of a red-black tree is BLACK • A RED parent never has a RED child – there are never 2 RED nodes in succession • Every path from the root to an empty subtree (NULL pointer) has the same number of BLACK nodes. a BLACK node corresponds to a level change in the 2-3-4 tree
Inserting Nodes Guidelines • Maintain root as BLACK node • Enter a new node as a RED node – since each new node enters a 2-node or a 3-node. • Whenever it results in two RED nodes in succession – rotate nodes to create a BLACK parent. • When scanning down a path to find insertion location – split any 4-node.
Inserting Nodes Inserting a 2 3-node 5 12
5 12 2 Inserting Nodes Inserting a 2 3-node 4-node 5 12
Inserting Nodes Inserting a 14 5 12
Inserting Nodes Inserting a 14 5 12 5 12 14
Inserting Nodes Inserting a 14 5 12 12 5 14 5 12 Single left rotation 14
Inserting Nodes Inserting a 10 5 12
Inserting Nodes Inserting a 10 5 12 5 12 10
Inserting Nodes Inserting a 10 5 12 10 5 12 5 12 right - left rotation 10