1 Lecture 13 CS2013.

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia AVL Trees v z.
Advertisements

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.
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.
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)
CSC401 – Analysis of Algorithms Lecture Notes 6 Dictionaries and Search Trees Objectives: Introduce dictionaries and its diverse implementations Introduce.
CSCE 3110 Data Structures & Algorithm Analysis AVL Trees Reading: Chap. 4, Weiss.
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.
© 2004 Goodrich, Tamassia Binary Search Trees1 CSC 212 Lecture 18: Binary and AVL Trees.
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.
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.
© 2004 Goodrich, Tamassia BINARY SEARCH TREES Binary Search Trees   
Part-D1 Binary Search Trees
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Binary Search Trees < > =
AA Trees.
COMP9024: Data Structures and Algorithms
Red-Black 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.
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.
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
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.
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
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 < > =
CS202 - Fundamental Structures of Computer Science II
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
1 Lecture 13 CS2013.
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 < > =
Binary Search Trees < > = Binary Search Trees
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
Binary Search Trees < > = Dictionaries
Dictionaries 二○一九年九月二十四日 ADT for Map:
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
Presentation transcript:

1 Lecture 13 CS2013

Binary Search Trees 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 6 9 2 4 1 8 © 2014 Goodrich, Tamassia, Goldwasser Binary Search Trees 2

Binary Search Trees 6/10/2019 Search To search for a key k, we trace a downward path starting at the root V is the root for the current search; necessary because this implementation is recursive The next node visited depends on the comparison of k with the key of the current node If we reach a leaf, the key is not found Example: get(4): Call TreeSearch(4,root) Algorithm TreeSearch(k, v) if T.isExternal (v) return v if k < key(v) return TreeSearch(k, left(v)) else if k = key(v) else { k > key(v) } return TreeSearch(k, right(v)) < 6 2 > 9 1 4 = 8 © 2014 Goodrich, Tamassia, Goldwasser 3 Binary Search Trees 3

Insertion < 6 To perform operation put(k, o), we search for key k (using TreeSearch) Assume k is not already in the tree, and 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 2 9 > 1 4 8 > w 6 2 9 1 4 8 w 5 © 2014 Goodrich, Tamassia, Goldwasser Binary Search Trees 4

Deletion To perform operation remove(k), we search for key k < 6 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 < 2 9 > v 1 4 8 w 5 6 2 9 1 5 8 © 2014 Goodrich, Tamassia, Goldwasser Binary Search Trees 5

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 1 v 3 2 8 6 9 w 5 z 1 v 5 2 8 6 9 © 2014 Goodrich, Tamassia, Goldwasser Binary Search Trees 6

BST Time Complexity If there are at least two elements to be placed in a BST, the same data can generate different BSTs. If there are at least three nodes, it can generate BSTs with different heights. 1 Unlike the diagrams from the book, this one does not show null leaves 2 2 1 3 3 7

BST Time Complexity Consider what happens if a BST is created from already-sorted data without any attempts at optimization: 1 Already-sorted data is not unusual! 2 3 4 Unlike the diagrams from the book, this one does not show null leaves 8

We want our trees to be balanced, so that we get the O(log n) behavior BST Time Complexity If a BST is perfectly balanced, i.e., a complete binary tree, its height (number of levels minus 1) is the floor of log n. If it is completely unbalanced, its height is n-1 The time complexity for search, insertion and deletion is the height of the tree. In the worst case, it is O(n). In the best case it is O(log n). We want our trees to be balanced, so that we get the O(log n) behavior 9

Important Warning ! The discussion that follows refers to height (measured from the bottom), not depth (measured from the top). The textbook does not count null children in BSTs as nodes. However, its discussion of height counts levels that consist only of null leaves. For example, in a BST that contains only the root and its null children, the root is at height 1, not 0, and the tree has height 1, not 0. AVL Trees 10

Performance Consider an ordered map with n items implemented by means of a binary search tree of height h the space used is O(n) methods get, put and remove take O(h) time The height h is O(n) in the worst case and O(log n) in the best case © 2014 Goodrich, Tamassia, Goldwasser Binary Search Trees 11

BST Time Complexity A complete binary tree is one in which every level other than the lowest level is completely filled, and all nodes are as far to the left as possible: Unlike the diagrams from the book, this one does not show null leaves 12

Balancing Binary Search Trees We can keep trees in balance by rotating nodes in various ways after insertions and deletions. Rebalancing trees has a cost. In general, however, the more often we search the tree relative to the frequency with which we insert or delete nodes, the more advantageous it is to keep the tree balanced. It is possible to maintain a BST so that it is always perfectly balanced, but in most cases we can be a little bit lax about this in order to reduce the cost of rebalancing. The compromise is to maintain a reasonably well-balanced tree, i.e., one in which the heights of the two subtrees for every node are about the same. One way to accomplish this is by using an AVL Tree. 13

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 heights of the children of v can differ by at most 1 An example of an AVL tree where the heights are shown next to the nodes AVL Trees 14

Height of an AVL Tree The height of an AVL tree storing n keys is O(log n). Let n(h) be the minimum number of internal nodes for an AVL tree of height h. The growth of n(h) is opposite to the growth of h; the larger n(h) for some h, the slower h grows. The larger the minimum number of nodes for some value of h, the smaller the h for n(h) -1. A lower bound on the growth of n(h) is an upper bound on the growth of h. Note that the following discussion does not require that the AVL tree be complete 3 4 n(1) n(2) AVL Trees 15

3 4 n(1) n(2) Height of an AVL Tree The height of an AVL tree storing n keys is O(log n). Proof (by induction): Let us bound n(h): the minimum number of internal nodes of an AVL tree of height h. We easily see that n(1) = 1 and n(2) = 2 For n > 2, an AVL tree with the minimum number of nodes for height h contains the root node, one AVL subtree of height n(h-1) and another of height n(h-2) That is, n(h) = 1 + n(h-1) + n(h-2) Knowing n(h-1) > n(h-2), we get n(h) > 2n(h-2) for n > 2. For each increase of h by 2, n(h) at least doubles. Therefore, for each doubling of n(h), h increases at most by 2 (multiplicative constant). Therefore, h is O(log n). It is also possible to be more exact; see the proof by induction in the textbook. AVL Trees 16

Insertion Insertion is as in a binary search tree Always done by expanding an external node. Example: 44 17 78 32 50 88 48 62 44 17 78 32 50 88 48 62 54 c=z a=y b=x w before insertion after insertion AVL Trees 17

Trinode Restructuring Let (a,b,c) be the inorder listing of x, y, z Perform the rotations needed to make b the topmost node of the three Single rotation around b Double rotation around c and a c=y b=x a=z T0 T1 T2 T3 b=y a=z c=x T0 T1 T2 T3 b=y a=z c=x T0 T1 T2 T3 b=x c=y a=z T0 T1 T2 T3 AVL Trees 18

Insertion Example, continued unbalanced... 4 T 1 44 x 2 3 17 62 z y 1 2 2 32 50 78 1 1 1 ...balanced 48 54 88 T 2 T T 1 T AVL Trees 19 3

Restructuring (as Single Rotations) AVL Trees 20

Restructuring (as Double Rotations) AVL Trees 21

Removal before deletion of 32 after deletion 44 17 78 32 50 88 48 62 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. Example: 44 17 78 32 50 88 48 62 54 44 17 62 50 78 48 54 88 before deletion of 32 after deletion AVL Trees 22

Rebalancing after a Removal Let z be the first unbalanced node encountered while travelling up the tree from w. Also, let y be the child of z with the larger height, and let x be the child of y with the larger height We perform a trinode restructuring to restore balance at z As this restructuring may upset the balance of another node higher in the tree, we must continue checking for balance until the root of T is reached 62 a=z 44 44 78 w 17 62 b=y 17 50 88 c=x 50 78 48 54 48 54 88 AVL Trees 23

AVL Tree Performance AVL tree storing n items The data structure uses O(n) space A single restructuring takes O(1) time using a linked-structure binary tree Searching takes O(log n) time height of tree is O(log n), no restructures needed Insertion takes O(log n) time initial find is O(log n) restructuring up the tree, maintaining heights is O(log n) Removal takes O(log n) time AVL Trees 24