{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

balanced

# balanced - Symbol Table Review 4.4 Balanced Trees Symbol...

This preview shows pages 1–4. Sign up to view the full content.

Robert Sedgewick and Kevin Wayne • Copyright © 2006 • http://www.Princeton.EDU/~cos226 4.4 Balanced Trees Reference: Chapter 13, Algorithms in Java, 3 rd Edition, Robert Sedgewick. 2 Symbol Table Review Symbol table: key-value pair abstraction. ! Insert a value with specified key. ! Search for value given key. ! Delete value with given key. Randomized BST. ! O(log N) time per op. [unless you get ridiculously unlucky] ! Store subtree count in each node. ! Generate random numbers for each insert/delete op. This lecture. 2-3-4 trees, red-black trees, B-trees. Robert Sedgewick and Kevin Wayne • Copyright © 2006 • http://www.Princeton.EDU/~cos226 2-3-4 Trees 4 2-3-4 Tree 2-3-4 tree. Generalize node to allow multiple keys; keep tree balanced. Perfect balance. Every path from root to leaf has same length. Allow 1, 2, or 3 keys per node. ! 2-node: one key, two children. ! 3-node: two keys, three children. ! 4-node: three keys, four children. M O K R W C E < K K R > R D F G J A S V Y Z Q N L

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
5 2-3-4 Tree: Search Search. ! Compare search key against keys in node. ! Find interval containing search key. ! Follow associated link (recursively). M O K R W C E < K K R > R D F G J A S V Y Z Q N L 6 2-3-4 Tree: Insert Insert. ! Search to bottom for key. ! 2-node at bottom: convert to 3-node. ! 3-node at bottom: convert to 4-node. ! 4-node at bottom: ?? M O K R W C E < K K R > R D F G J A S V Y Z Q N L 7 2-3-4 Tree: Splitting Four Nodes Transform tree on the way down. ! Ensures last node is not a 4-node. ! Local transformation to split 4-nodes: Invariant. Current node is not a 4-node. Consequence. Insertion at bottom is easy since it's not a 4-node. 8 2-3-4 Tree: Splitting a Four Node Ex. To split a four node, move middle key up. A-C K Q W D E-J L-P R-V X-Z A-C K D Q E-J L-P R-V X-Z W
9 2-3-4 Tree Tree grows up from the bottom. E A P E X M L 10 2-3-4 Tree: Balance Property. All paths from root to leaf have same length. Tree height. ! Worst case: lg N [all 2-nodes] ! Best case: log 4 N = 1/2 lg N [all 4-nodes] ! Between 10 and 20 for a million nodes. ! Between 15 and 30 for a billion nodes. 11 2-3-4 Tree: Implementation? Direct implementation. Complicated because of: !

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 11

balanced - Symbol Table Review 4.4 Balanced Trees Symbol...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online