Advanced Tree Data Structures Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park Binary trees Traversal order Balance Rotation Multi-way trees Search Insert Overview
Tree Traversal Goal Visit every node in binary tree Approaches Depth first Preorder parent before children Inorder left child, parent, right child Postorder children before parent Breadth first closer nodes first Tree Traversal Methods Pre-order. Visit node // first. Recursively visit left subtree. Recursively visit right subtree In-order. Recursively visit left subtree. Visit node // second. Recursively right subtree Post-order. Recursively visit left subtree. Recursively visit right subtree. Visit node // last
Tree Traversal Methods Breadth-first BFS(Node n) { Queue Q = new Queue(); Q.enqueue(n); // insert node into Q while (!Q.empty()) { n = Q.dequeue(); // remove next node if (!n.isempty()) { visit(n); // visit node Q.enqueue(n.Left()); // insert left subtree in Q Q.enqueue(n.Right());// insert right subtree in Q } } Tree Traversal Examples Pre-order (prefix) + / 8 4 In-order (infix) + 8 / 4 Post-order (postfix) 8 4 / + Breadth-first + / 8 4 + / 8 4 Expression tree
Tree Traversal Examples Pre-order 44, 7,, 78, 50, 48, 6, 88 In-order 7,, 44, 48, 50, 6, 78, 88 Post-order, 7, 48, 6, 50, 88, 78, 44 Breadth-first 44, 7, 78,, 50, 88, 48, 6 Sorted order! 44 7 78 50 48 6 Binary search tree 88 Tree Balance Degenerate Worst case Search in O(n) time Balanced Average case Search in O( log(n) ) time Degenerate binary tree Balanced binary tree 4
Question Tree Balance Can we keep tree (mostly) balanced? Self-balancing binary search trees AVL trees Red-black trees Approach Select invariant (that keeps tree balanced) Fix tree after each insertion / deletion Maintain invariant using rotations Provides operations with O( log(n) ) worst case Properties AVL Trees Binary search tree Heights of children for node differ by at most Example Heights of children shown in red 4 44 7 78 50 48 6 88 5
History AVL Trees Discovered in 96 by two Russian mathematicians, Adelson-Velskii & Landis Algorithm. Find / insert / delete as a binary search tree. After each insertion / deletion a) If height of children differ by more than b) Rotate children until subtrees are balanced c) Repeat check for parent (until root reached) Properties Red-black Trees Binary search tree Every node is red or black The root is black Every leaf is black All children of red nodes are black For each leaf, same # of black nodes on path to root Characteristics Properties ensures no leaf is twice as far from root as another leaf 6
Red-black Trees Example History Red-black Trees Discovered in 97 by Rudolf Bayer Algorithm Insert / delete may require complicated bookkeeping & rotations Java collections TreeMap, TreeSet use red-black trees 7
Tree Rotations Changes shape of tree Move nodes Change edges Types Single rotation Left Right Double rotation Left-right Right-left Tree Rotation Example Single right rotation 8
Tree Rotation Example Single right rotation 5 6 5 4 4 6 Node 4 attached to new parent Example Single Rotations single left rotation T 0 T T T 0 T T T T single right rotation T T 0 T 0 T T T T T 9
Example Double Rotations right-left double rotation T 0 T T T 0 T left-right double rotation T T T T T T 0 T T T 0 T T Properties Multi-way Search Trees Generalization of binary search tree Node contains k keys (in sorted order) Node contains k+ children Keys in j th child < j th key < keys in (j+) th child Examples 5 5 8 5 8 7 7 9 9 44 0
Types of Multi-way Search Trees - tree Internal nodes have or children Index search trie Internal nodes have up to 6 children (for strings) B-tree T = minimum degree Non-root internal nodes have T- to T- children All leaves have same depth 5 8 7 a c o T- T- s T Multi-way Search Trees Search algorithm. Compare key x to k keys in node. If x = some key then return node. Else if (x < key j) search child j 4. Else if (x > all keys) search child k+ Example Search(7) 5 5 0 40 8 7 7 6 44
Multi-way Search Trees Insert algorithm. Search key x to find node n. If ( n not full ) insert x in n. Else if ( n is full ) a) Split n into two nodes b) Move middle key from n to n s parent c) Insert x in n d) Recursively split n s parent(s) if necessary Multi-way Search Trees Insert Example (for - tree) Insert( 4 ) 5 5 8 7 4 8 7
Multi-way Search Trees Insert Example (for - tree) Insert( ) 5 5 4 8 7 4 8 7 Split node 5 Split parent 4 8 7 Characteristics B-Trees Height of tree is O( log T (n) ) Reduces number of nodes accessed Wasted space for non-full nodes Popular for large databases node = disk block Reduces number of disk blocks read