Data Structures Midterm 2 review outline
For each topic the key questions are what?, how?
Readings: See latest info page (Sakai home page) for specific chapter/section links to ODS textbook, notes, Sedgewick slides.
- USet: multiplicative hash functions.
- How does multiplicative hashing work?
user data --> int --> random index
x --> y = hashCode(x) --> (z*y) >> (w-d)
- Why does it use the high order bits instead of the low order bits?
example: y = 47, z = z1*10 + z0, yz = (4*z0 + 7*z1)*10 + 7*z0.
Notice that the 10's digit of z*y is a random combination of both digits of y,
whereas the units digit of z*y just involves the units digit of y.
- Priority Queue: add(x), x = extractMin()
- Heap property: at each node parent item less than child items.
- Binary Heap of n items
- [What:] 2 views: (1) left complete binary tree with heap property. (2) array, i's left child at 2i+1.
- [What:] log(n) time for add, extractMin
- [How:] add: put at bottom, bubble up. extractMin: put bottom item at root , trickle down.
- Meldable Heap
- [What:] Merge is the key function (and also the new user feature)
- [How:] add: make a one node 2nd tree, merge. extractMin: merge left and right subtrees. merge: preserve heap prop. flip coin to decide left or right.
- [What:] log(n) expected time for add, extractMin, merge
- [How:] expected random walk length in any binary tree is proportional to log(n).
- SSet: add(x), remove(x), find(x), x = first(), x = next()
- Binary Search Tree
- The BST property is left to right sortedness: at all u, u->x is greater than all items in u->left and less than all items in u->right.
- the SSet functions run in worst case time proportional to the tree height.
- If the tree is built by additing items in random order then the tree height is expected to be proportional to log(n).
- a sequence of m SSet ops on a BST runs in average time O(log(m)) per op. (averaging over all possible op sequences).
- Balancing schemes
- Treap: combine bst(user items) and heap(internal random priorities)
- [What:] A seq of m ops runs in expected time O(log(m)) per op.
(averaging over randomly set -- artificial -- priorities).
- [How:] add: add ordinary bst style. Then fix priorities (heap property) via rotations.
- [Why:] root is random (whichever item happened to get lowest priority number). Therefore Treap's expected behaviour is unbalanced BST's average behaviour.
- 2-3-4 tree
- [What:] Nodes have 1,2 or 3 keys (2,3, or 4 children).
- [What:] All leaves are at the same depth.
- [How:] split 4 nodes, putting center key in node above...
- [What:] Worst case search path length is O(log(n)).
- [Why:] Tree height between log4(n) and log2(n)
- [What:] Device for understanding RBT (red-black trees)
- [What:] 2-3 tree: never leave any 4 nodes at the end of an add or remove.
- Red-Black tree
- [What:] Best implementation strategy for 2-3-4 trees
- [What:] Nodes have "red" or "black" link to parent
- [What:] All leaves are at the same black depth.
- [What:] No 2 reds in a row.
- [Why:] then no path longer than twice the 2-3-4 tree height.
- [How:] 3 node = 2 nodes with red link between. Left leaning: red link to left child.
- [How:] split 4 nodes = color flip
- [What:] Worst case search path length is O(log(n)).
- [How:] add: can do BST add followed by fixup on the way up.
- [How:] Use rotations to (a) make 3-nodes left leaning, (b) fix malformed 4-nodes (may require 2 rotations).