Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPS 100, Spring 2010 15.1 Interlude for trees l Joyce Kilmer Joyce Kilmer l Balanced Trees  Splay  Red-Black  AVL  B-tree.

Similar presentations


Presentation on theme: "CPS 100, Spring 2010 15.1 Interlude for trees l Joyce Kilmer Joyce Kilmer l Balanced Trees  Splay  Red-Black  AVL  B-tree."— Presentation transcript:

1 CPS 100, Spring 2010 15.1 Interlude for trees l Joyce Kilmer Joyce Kilmer l Balanced Trees  Splay  Red-Black  AVL  B-tree

2 CPS 100, Spring 2010 15.2 Minimax, see TicTac.java l Players alternate, one might be computer, one human (or two computer players) l Simple rules: win scores +10, loss scores –10, tie is zero  X maximizes, O minimizes l Assume opponent plays smart  What happens otherwise? l As game tree is explored is there redundant search?  What can we do about this? X O X X O X X O X X X O X O X O X O X O X O X O X X O X O

3 CPS 100, Spring 2010 15.3 Balanced Trees and Complexity l A tree is height-balanced if  Left and right subtrees are height-balanced  Left and right heights differ by at most one boolean isBalanced(Tree root){ if (root == null) return true; return isBalanced(root.left) && isBalanced(root.right) && Math.abs(height(root.left) – height(root.right)) <= 1; }

4 CPS 100, Spring 2010 15.4 Rotations and balanced trees l Height-balanced trees  For every node, left and right subtree heights differ by at most 1  After insertion/deletion need to rebalance  Every operation leaves tree in a balanced state: invariant property of tree l Find deepest node that’s unbalanced then make sure:  On path from root to inserted/deleted node  Rebalance at this unbalanced point only Are these trees height-balanced?

5 CPS 100, Spring 2010 15.5 What is complexity? l Assume trees are “balanced” in analyzing complexity  Roughly half the nodes in each subtree  Leads to easier analysis l How to develop recurrence relation?  What is T(n)?  What other work is done? l How to solve recurrence relation  Plug, expand, plug, expand, find pattern  A real proof requires induction to verify correctness

6 CPS 100, Spring 2010 15.6 Balanced trees we won't study l B-trees are used when data is both in memory and on disk  File systems, really large data sets  Rebalancing guarantees good performance both asymptotically and in practice. Differences between cache, memory, disk are important l Splay trees rebalance during insertion and during search, nodes accessed often more closer to root  Other nodes can move further from root, consequences? Performance for some nodes gets better, for others …  No guarantee running time for a single operation, but guaranteed good performance for a sequence of operations, this is good amortized cost (ArrayList.add)

7 CPS 100, Spring 2010 15.7 Balanced trees we will study l Both kinds have worst-case O(log n) time for tree operations l AVL (Adel’son-Velskii and Landis), 1962  Nodes are “height-balanced”, subtree heights differ by 1  Rebalancing requires per-node bookkeeping of height  http://people.ksp.sk/~kuko/bak/ http://people.ksp.sk/~kuko/bak/ l Red-black tree uses same rotations, but can rebalance in one pass, contrast to AVL tree  In AVL case, insert, calculate balance factors, rebalance  In Red-black tree can rebalance on the way down, code is more complex, but doable  Standard java.util.TreeMap/TreeSet use red-black

8 CPS 100, Spring 2010 15.8 Rotation doLeft (see AVLSet.java) l Why is this called doLeft?  N will no longer be root, new value in left.left subtree  Left child becomes new root l Unbalanced by two (not one!)  If left, left (or right, right) doLeft (doRight)  Otherwise need two doLeft/doRight  First to get to left, left Or to right, right A B C N A B C N Node doLeft(Node root) { Node newRoot = root.left; root.left = newRoot.right; newRoot.right = root; return newRoot; }

9 CPS 100, Spring 2010 15.9 Rotation to rebalance l Suppose we add a new node in right subtree of left child of root  Single rotation can’t fix  Need to rotate twice l First stage is shown at bottom  Rotate blue node right (its right child takes its place)  This is left child of unbalanced B A C N A B C N AB1B1 B2B2 C ???? ? Node doRight(Node root) { Node newRoot = root.right; root.right = newRoot.left; newRoot.left = root; return newRoot; } B2B2 A B1B1

10 CPS 100, Spring 2010 15.10 Double rotation complete AB1B1 B2B2 C B2B2 A B1B1 C B2B2 A B1B1 C l Calculate where to rotate and what case, do the rotations Node doRight(Node root) { Node newRoot = root.right; root.right = newRoot.left; newRoot.left = root; return newRoot; } Node doLeft(Node root) { Node newRoot = root.left; root.left = newRoot.right; newRoot.right = root; return newRoot; }

11 CPS 100, Spring 2010 15.11 AVL tree practice l Insert into AVL tree:  18 10 16 12 6 3 8 13 14  After adding 16: doLeftRight  After 3, doLeft on 16 18 10 18 16 18 10 16 doRight 18 16 10 doLeft 10 16 18 126 10 16 18 126 3 6 10 16 3 18 12 16 18 10 6 3 8 16 10 18

12 CPS 100, Spring 2010 15.12 AVL practice: continued, and finished l After adding 13, ok l After adding14, not ok  doRight at 12 12 16 18 10 6 3 812 16 18 10 6 3 8 13 14 13 16 18 10 6 3 8 12 14

13 CPS 100, Spring 2010 15.13 Lynn Conway See Wikipedia and lynnconway.com l Joined Xerox Parc in 1973  Revolutionized VLSI design with Carver Mead l Joined U. Michigan 1985  Professor and Dean, retired '98 l NAE '89, IEEE Pioneer '09 l Helped invent dynamic scheduling early '60s IBM l Transgender, fired in '68


Download ppt "CPS 100, Spring 2010 15.1 Interlude for trees l Joyce Kilmer Joyce Kilmer l Balanced Trees  Splay  Red-Black  AVL  B-tree."

Similar presentations


Ads by Google