This course covers topics in algorithms and data structures including algorithm analysis, sorting, heaps, binary search trees, hashing techniques and hash tables, and (as time permits) graphs and graph-based algorithms. Key points in these slides are: 2-3-4 Trees, Tree Insertion, Removal, Removal Refresher, Efficient Removal, Nearest Sibling, Adjacent, Red Black Trees
Download 2-3-4 Trees - Algorithms and Abstract Data Structures - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!
CPSC 223
Algorithms & Data Abstract Structures
Lecture 23:
2-3-4 Trees (cont.)
Today …
• 2-3-4 Trees (cont.) [Ch 12: 651-670]
CPSC 223 ‐‐ Fall 2010
2-3-4 Trees
CPSC 223 ‐‐ Fall 2010
2-3-4 Trees
A 2-3-4 Tree is a 2-3 Tree that also allows 4-nodes
That is, nodes with 3 items and 4 children
More efficient insertion
Split 3-Item Nodes as you search for the leaf
As opposed to bottom up in a 2-3 tree
Guarantees we insert into a leaf node with an empty slot
Works because a node has 3 items (consider root case) CPSC 223 ‐‐ Fall 2010 50 20 30 40 60 70 30 50 20 40 60 70 Insert 25! split
2-3-4 Tree Insertion
The root is a special case …
For the root case, we can’t move the middle element up CPSC 223 ‐‐ Fall 2010 Split into 3 nodes (add 2) Leave middle‐element in root f k n a g insert h m p q root k a g insert h m p q root f n
2-3-4 Insertion
Example:
Insert the following nodes into a 2-3-4 tree 10, 30, 60, 20, 40, 50, 70, 80, 15, 90, 100, 85 CPSC 223 ‐‐ Fall 2010
2-3-4 Removal
Removal refresher …
Find node to remove
Find inorder successor (in a leaf node)
Copy and remove inorder successor Also more efficient removal
Transform nodes with only 1 item into nodes with 2 or 3 items
during top-down traversal
Guarantees that the leaf node has more than one item
This means that removal does not require restructuring the tree
bottom up
A number of cases to consider … CPSC 223 ‐‐ Fall 2010
Case 0
If the only node in the tree is the root …
Remove the item from the root (adjust remaining items)
If the root node has no more items, remove the root node CPSC 223 ‐‐ Fall 2010 a b c remove b root (^) a c a c remove a root (^) c c remove c root root root root NULL
Removal – Case 3 (the root case)
When remove encounters a 1 -item node (not the root)
If the parent is the root and the root has 1 item
And the sibling only has 1 item (how many siblings possible?)
Then the node, parent, and sibling are fused CPSC 223 ‐‐ Fall 2010 f a g N 1 N 2 N 3 N 4 a f g N 1 N 2 N 3 N 4 AKer fusion
Nearest Sibling (Textbook)
Instead of “ adjacent ” sibling
Which could be the sibling to the immediate left or immediate right
The textbook uses “ nearest ” sibling such that:
A node’s nearest sibling is its immediate left sibling
Unless the node is a left child … in which case its nearest sibling is its immediate right sibling CPSC 223 ‐‐ Fall 2010
2-3-4 Removal
Example:
From the 2-3-4 tree of the previous example, remove 70, 30, 80, 50 CPSC 223 ‐‐ Fall 2010
Encoding Red Black Trees as 2-3-4 Trees
Red Black trees can be used to encode 2-3-4 Trees
Can lead to more efficient storage … since nodes in a Red Black
tree are binary (have two children)
This can also simplify (and explain) rotations and color changes
The basic idea (see textbook for details):
All parent-child edges in a 2-3-4 tree remain black
3-Nodes and 4-Nodes converted to binary nodes with red edges CPSC 223 ‐‐ Fall 2010 S L a b d M c S M a b L c d S L a b c S L a b c