© 2004 Goodrich, Tamassia Binary Search Trees1 CSC 212 Lecture 18: Binary and AVL Trees.

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Binary Search Trees
Advertisements

© 2004 Goodrich, Tamassia AVL Trees v z.
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
1 AVL Trees (10.2) CSE 2011 Winter April 2015.
1 prepared from lecture material © 2004 Goodrich & Tamassia COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material.
Data Structures Lecture 11 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
© 2004 Goodrich, Tamassia Binary Search Trees   
Goodrich, Tamassia Search Trees1 Binary Search Trees   
CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties and maintenance.
CSC 213 Lecture 7: Binary, AVL, and Splay Trees. Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries):
Binary Search Trees1 Part-F1 Binary Search Trees   
Binary Search Trees   . 2 Ordered Dictionaries Keys are assumed to come from a total order. New operations: closestKeyBefore(k) closestElemBefore(k)
Binary Search Trees1 ADT for Map: Map stores elements (entries) so that they can be located quickly using keys. Each element (entry) is a key-value pair.
AVL Trees v z. 2 AVL Tree Definition AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the.
Lecture12: Tree II Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
COMP20010: Algorithms and Imperative Programming Lecture 4 Ordered Dictionaries and Binary Search Trees AVL Trees.
CSC401 – Analysis of Algorithms Lecture Notes 6 Dictionaries and Search Trees Objectives: Introduce dictionaries and its diverse implementations Introduce.
CS 221 Analysis of Algorithms Ordered Dictionaries and Search Trees.
Search Trees Last Update: Nov 5, 2014 EECS2011: Search Trees1 “Grey Tree”, Piet Mondrian, 1912.
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
Search Trees Chapter   . Outline  Binary Search Trees  AVL Trees  Splay Trees.
Binary Search Trees   . 2 Binary Search Tree Properties A binary search tree is a binary tree storing keys (or key-element pairs) at its.
Chapter 10: Search Trees Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data.
1 Searching the dictionary ADT binary search binary search trees.
Binary Search Trees1 Chapter 3, Sections 1 and 2: Binary Search Trees AVL Trees   
CHAPTER 10 SEARCH TREES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND.
1 Binary Search Trees   . 2 Ordered Dictionaries Keys are assumed to come from a total order. New operations: closestKeyBefore(k) closestElemBefore(k)
1 COMP9024: Data Structures and Algorithms Week Six: Search Trees Hui Wu Session 1, 2014
© 2004 Goodrich, Tamassia BINARY SEARCH TREES Binary Search Trees   
Algorithms Design Fall 2016 Week 6 Hash Collusion Algorithms and Binary Search Trees.
Part-D1 Binary Search Trees
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Binary Search Trees < > =
COMP9024: Data Structures and Algorithms
AVL Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Binary Search Trees < > =
Search Trees.
Binary Search Trees < > =
AVL Trees 6/25/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Binary Search Trees (10.1) CSE 2011 Winter August 2018.
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Chapter 10.1 Binary Search Trees
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
Binary Search Trees < > = Binary Search Trees
Chapter 2: Basic Data Structures
AVL Trees 11/10/2018 AVL Trees v z AVL Trees.
AVL Trees 4/29/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
Binary Search Trees (10.1) CSE 2011 Winter November 2018.
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Copyright © Aiman Hanna All rights reserved
Binary Search Trees < > =
Binary Search Trees < > = Binary Search Trees
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
Red-Black Trees 2018年11月26日3时46分 AVL Trees v z AVL Trees.
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
Dictionaries < > = /9/2018 3:06 AM Dictionaries
Copyright © Aiman Hanna All rights reserved
Binary Search Trees < > =
Dictionaries < > = /17/2019 4:20 PM Dictionaries
Binary Search Trees < > =
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
Binary Search Trees < > =
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
1 Lecture 13 CS2013.
Binary Search Trees < > = Dictionaries
Dictionaries 二○一九年九月二十四日 ADT for Map:
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
Presentation transcript:

© 2004 Goodrich, Tamassia Binary Search Trees1 CSC 212 Lecture 18: Binary and AVL Trees

© 2004 Goodrich, Tamassia Binary Search Trees2 Ordered Dictionaries Keys are assumed to come from a total order. New operations: first(): first entry in the dictionary ordering last(): last entry in the dictionary ordering successor(k): entry with smallest key greater than or equal to k; increasing order predecessor(k): entry with largest key less than or equal to k; decreasing order

© 2004 Goodrich, Tamassia Binary Search Trees3 Binary Search (§ 8.3.3) Binary search can perform operation find(k) on a dictionary implemented by means of an array-based sequence, sorted by key similar to the high-low game at each step, the number of candidate items is halved terminates after O(log n) steps Example: find(7) m l h m l h m l h l  m  h

© 2004 Goodrich, Tamassia Binary Search Trees4 Search Tables A search table is a dictionary implemented by means of a sorted sequence Store the items of the dictionary in an array-based sequence, sorted by key Performance: find takes O(log n) time, using binary search insert takes O(n) time since in the worst case we have to shift n  2 items to make room for the new item remove take O(n) time since in the worst case we have to shift n  2 items to compact the items after the removal Lookup table effective only for dictionaries of small size or for dictionaries on which searches are the most common operations

© 2004 Goodrich, Tamassia Binary Search Trees5 Binary Search Trees (§ 9.1) A binary search tree is a binary tree storing keys (or key-value entries) at its internal nodes and satisfying the following property: Let u, v, and w be three nodes such that u is in the left subtree of v and w is in the right subtree of v. We have key(u)  key(v)  key(w) External nodes do not store items An inorder traversal of a binary search trees visits the keys in increasing order

© 2004 Goodrich, Tamassia Binary Search Trees6 Search (§ 9.1.1) To search for a key k, we trace a downward path starting at the root The next node visited depends on the outcome of the comparison of k with the key of the current node If we reach a leaf, the key is not found and we return null Example: find(4): Call TreeSearch(4,root) Algorithm TreeSearch(k, v) if T.isExternal (v) /* throw exception */ if k  key(v) return TreeSearch(k, T.left(v)) else if k  key(v) return v else { k  key(v) } return TreeSearch(k, T.right(v))   

© 2004 Goodrich, Tamassia Binary Search Trees7 Search (§ 9.1.1) Example: find(5): Call TreeSearch(5,root) Algorithm TreeSearch(k, v) if T.isExternal (v) /* throw exception */ if k  key(v) return TreeSearch(k, T.left(v)) else if k  key(v) return v else { k  key(v) } return TreeSearch(k, T.right(v))   

© 2004 Goodrich, Tamassia Binary Search Trees8 Insertion To perform operation insert(k, o), we search for key k (using TreeSearch) Assume k is not already in the tree, and let let w be the leaf reached by the search We insert k at node w and expand w into an internal node Example: insert(5, o)    w w

© 2004 Goodrich, Tamassia Binary Search Trees9 Deletion To perform operation remove( k ), we search for key k Assume key k is in the tree, and let let v be the node storing k If node v has a leaf child w, we remove v and w from the tree with operation removeExternal( w ), which removes w and its parent Example: remove(4) v w  

© 2004 Goodrich, Tamassia Binary Search Trees10 Deletion (cont.) We consider the case where the key k to be removed is stored at a node v whose children are both internal we find the internal node w that follows v in an inorder traversal we copy key(w) into node v we remove node w and its left child z (which must be a leaf) by means of operation removeExternal( z ) Example: remove(3) v w z v 2

© 2004 Goodrich, Tamassia Binary Search Trees11 Performance Consider a dictionary with n items implemented by means of a binary search tree of height h the space used is O(n) methods find, insert and remove take O(h) time The height h is O(n) in the worst case and O(log n) in the best case

© 2004 Goodrich, Tamassia Binary Search Trees12 Your Turn Insert 4, 2, 6, 1, 3, 5, 7 into a tree Then remove nodes in this order: 4, 2, 6, 1, 3, 5, 7

© 2004 Goodrich, Tamassia Binary Search Trees13 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children of v can differ by at most 1. Example AVL tree where the heights are shown next to the nodes

© 2004 Goodrich, Tamassia Binary Search Trees14 Insertion in an AVL Tree Insertion is as in a binary search tree Always done by expanding an external node. Consider insert(54, 0): before insertionafter insertion

© 2004 Goodrich, Tamassia Binary Search Trees15 Trinode Restructuring let (a,b,c) be an inorder listing of x, y, z perform the rotations needed to make b the topmost node of the three b=y a=z c=x T0T0 T1T1 T2T2 T3T3 b=y a=z c=x T0T0 T1T1 T2T2 T3T3 c=y b=x a=z T0T0 T1T1 T2T2 T3T3 b=x c=ya=z T0T0 T1T1 T2T2 T3T3 case 1: single rotation (a left rotation about a) case 2: double rotation (a right rotation about c, then a left rotation about a) (other two cases are symmetrical)

© 2004 Goodrich, Tamassia Binary Search Trees16 Insertion Example, continued T 0 T 1 T 2 T 3 x y z unbalanced......balanced T 1

© 2004 Goodrich, Tamassia Binary Search Trees17 Restructuring (as Single Rotations) Single Rotations:

© 2004 Goodrich, Tamassia Binary Search Trees18 Restructuring (as Double Rotations) double rotations:

© 2004 Goodrich, Tamassia Binary Search Trees19 Removal in an AVL Tree Removal begins as in a binary search tree, which means the node removed will become an empty external node. Its parent, w, may cause an imbalance. Remove(32): before deletion of 32after deletion

© 2004 Goodrich, Tamassia Binary Search Trees20 Rebalancing after a Removal Let z be the first unbalanced node encountered while travelling up tree after removing a node. Let y be the child of z with the larger height, and x be the child of y with the larger height. restructure(x) restores balance at z. Restructuring may unbalance node higher in the tree, so must continue checking for balance (and restructuring where needed) until root is reached w c=x b=y a=z

© 2004 Goodrich, Tamassia Binary Search Trees21 Running Times for AVL Trees Single restructure is O(1) Work is constant for height of tree find is O(log n) height of tree is O(log n) insert is _____________ initial find is O(log n) Restructuring up the tree – could do at most _______________________ work remove is _______________ initial find is O(log n) Restructuring up the tree – could do at most _______________________ work

© 2004 Goodrich, Tamassia Binary Search Trees22 Your Turn Insert 1, 2, 3, 4, 5, 6, 7, 8 into an AVL tree Then remove 1, 2, 3, 4, 5, 6, 7, 8 into AVL tree

© 2004 Goodrich, Tamassia Binary Search Trees23 Daily Quiz I have a “friend” who claims that the order nodes are entered into an AVL tree does not matter, the end tree is always the same Provide a small example proving my “friend” wrong