Hashing NonBinary Trees Tries SelfBalancing Trees 9 4
• • Hashing Non-Binary Trees Tries Self-Balancing Trees
9. 4 Hashing *
General Trees • • • Root node Ordered subtrees Each node's out degree denotes its children forest is. . . collection of one or more trees Each node has exactly one parent, except the root
General Tree Implementation • • Access root Access children Binary tree was easy. . . what about for n children? o Array based? child count, access via index o Linked-list based? first child, next sibling? Javascript/DOM, anyone? o
General Tree Implementations (2) • • pre-order/post-order pretty much the same what's wrong with inorder traversal?
Operations on General Trees • • find (root) union (merge equivalence classes)
Parent-Pointer Implementation • • • Each node only points at parent Doesn't give us everything. What does it give us? Shared tree? o stored in an array (with parent/value), so find is easy o Union is easy? § Keep depth low § Weighted Union rule (small tree's root, points to big tree's root) § Path Compression. . . flatten that tree o • On FIND, not Union.
Processing h, e equivalence we get
PPI? What did we get? • In general those operations will be fast.
But what if we want all operations? List of Children
Cost of finding a Sibling?
Better options? • The Left-Child/Right-Sibling Implementation Easy to combine trees in same array o Decent space efficiency compared to list of children o • Dynamic node implementations Array-based list of children § better if child count is known/doesn't change o Nice, programatically. . . but can be messy memory management o • Dynamic Left-Child/Right-Sibling? Good space storage, easy to navigate, but? o Turns general tree to binary tree representation o
dynamic left child/right sibling
Tries • • Mentioned this before. Storing/retrieving strings key space decomposition stores values only in leafs
Balanced Trees • • Unbalanced trees can be problematic, with O(logn) becoming O(n) in the worst case. . . what does that mean? Options? always "balanced" (heap) o modified insert/delete operations (AVL) o access-time optimization (SPLAY) o
The AVL Tree • • • When we add a node and it becomes unbalanced, do a rotation (or two). Same with deletion Outsides long, 1 rotation fix Insides long, 2 rotation fix (first to get outside long form) Each rotation gives -1 to a subtree and +1 to the other.
The Avle Tree (2) • • O(logn) search theta(logn) update
The Splay Tree • • Not guaranteed as balanced as AVL tree But guarantees m accesses take O(mlogn) when m > n If we access more than we have data, this is great. 3 types of rotation on access
Splay Tree Rotations • • Single Rotation o when S is a child of the Root node (AVL rotation) Zigzag Rotation (inside rotation) o double rotation from AVL (book mistake? ) l->r or r->l Zigzig Rotation (outside rotation) o l->l or r->r Proof of O(mlogn). . . oh nelly!
- Slides: 37