COEN 352 Data Structures and Algorithms
Total Page:16
File Type:pdf, Size:1020Kb
COEN 352 Data Structures and Algorithms Stuart Thiel Advanced Trees COEN 352 Data Structures and Algorithms Tries Balanced Trees Stuart Thiel Concordia University Department of Electrical Computer Engineering Summer, 2014 1/12 COEN 352 Data Outline Structures and Algorithms Stuart Thiel Advanced Trees Advanced Trees Tries Tries Balanced Trees Balanced Trees 2/12 COEN 352 Data Tries Structures and Algorithms Stuart Thiel I pronounced 'try' Advanced Trees I object space decomposition: BST Tries I key space decomposition: Trie Balanced Trees I data usually just in leaves I branching factor based on alphabet I Huffman Coding Tree: binary trie 3/12 COEN 352 Data Searching for Words Structures and Algorithms Stuart Thiel I depth bound by word length Advanced Trees I clumping on common prefix: meh Tries I Unlike Huffman Coding Tree, need terminating Balanced Trees character I show page 432 for example 4/12 COEN 352 Data PAT Tries Structures and Algorithms Stuart Thiel I compressing leaves worked above Advanced Trees I we can also compress internal nodes Tries I Practical Algorithm To Retrieve Information Coded In Balanced Trees Alphanumeric I aka PATRICIA aka PAT trie I nodes point to the next relevant part of the input I full comparison at leaves I show page 433 5/12 COEN 352 Data Balanced Trees Structures and Algorithms Stuart Thiel I BSTs can become unbalanced Advanced Trees I makes search expensive Tries I adjust access, improve performance Balanced Trees I ensuring complete binary tree too expensive I relax requirement, good enough 6/12 COEN 352 Data AVL Tree vs. Splay Tree Structures and Algorithms Stuart Thiel I AVL constrain difference between depth of subtrees Advanced Trees I SPLAY improves balance on each access Tries I both have advantages Balanced Trees 7/12 COEN 352 Data AVL Tree Structures and Algorithms Stuart Thiel I named for its inventors Adelson-Velskii and Landis) Advanced Trees I left and right subtrees differ in depth by at most 1 Tries I ALL NODES Balanced Trees I max depth is O (logn) I search is therefore O (logn) I updates can be done in time proportional to search 8/12 COEN 352 Data AVL Tree Balance Structures and Algorithms Stuart Thiel I Each node knows its level of unbalance Advanced Trees I −1, 0 or 1 Tries I tree starts balanced according to this Balanced Trees I changes propagate up to root I when can we stop early? I propagation stops logically I show page 435 9/12 COEN 352 Data AVL Tree Rotations Structures and Algorithms Stuart Thiel I what are these changes? Advanced Trees I we call them rotations Tries I AVL needs a single rotation or double rotation Balanced Trees I rotation about node with bad balance I if sign of imbalance is the same, single I if sign of imbalance differs, double I double rotation starts by making sign of imbalance the same I rotate on node with awkward balance 10/12 COEN 352 Data AVL Tree Deletion Structures and Algorithms Stuart Thiel I addition looks easy. it is! Advanced Trees I deletion is only slightly trickier Tries I can we optimize? Balanced Trees I if balance is 1 grab smallest in right subtree? I if balance is −1 grab biggest in left subtree? I at least it cannot hurt I work up from secondary deletion 11/12 COEN 352 Data AVL Tree Examples Structures and Algorithms Stuart Thiel I Lets try some trees on the chalkboard! Advanced Trees Tries Balanced Trees 12/12.