CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -

Slides:



Advertisements
Similar presentations
AVL Trees binary tree for every node x, define its balance factor
Advertisements

1 AVL-Trees (Adelson-Velskii & Landis, 1962) In normal search trees, the complexity of find, insert and delete operations in search trees is in the worst.
CPSC 252 AVL Trees Page 1 AVL Trees Motivation: We have seen that when data is inserted into a BST in sorted order, the BST contains only one branch (it.
CS261 Data Structures AVL Trees. Goals Pros/Cons of a BST AVL Solution – Height-Balanced Trees.
1 CSE 373 AVL trees, continued read: Weiss Ch. 4, section slides created by Marty Stepp
CS202 - Fundamental Structures of Computer Science II
CSE332: Data Abstractions Lecture 7: AVL Trees Dan Grossman Spring 2010.
Dynamic Dictionaries Primary Operations:  Get(key) => search  Insert(key, element) => insert  Delete(key) => delete Additional operations:  Ascend()
1 AVL-Trees: Motivation Recall our discussion on BSTs –The height of a BST depends on the order of insertion E.g., Insert keys 1, 2, 3, 4, 5, 6, 7 into.
1 Joe Meehean.  BST efficiency relies on height lookup, insert, delete: O(height) a balanced tree has the smallest height  We can balance an unbalanced.
Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.
Copyright Curt Hill Balance in Binary Trees Impact on Performance.
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 20 AVL Trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each node is 0, 1, of -1. Basic Operations Construction,
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
CSE332: Data Abstractions Lecture 7: AVL Trees
AVL Trees CSE, POSTECH.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
Search Trees.
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Balancing Binary Search Trees
CSIT 402 Data Structures II
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
AVL Trees binary tree for every node x, define its balance factor
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
Chapter 26 AVL Trees Jung Soo (Sue) Lim Cal State LA.
AVL Trees A BST in which, for any node, the number of levels in its two subtrees differ by at most 1 The height of an empty tree is -1. If this relationship.
CS 201 Data Structures and Algorithms
Chapter 29 AVL Trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
AVL Tree Mohammad Asad Abbasi Lecture 12
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
AVL Tree A Balanced Binary Search Tree
TCSS 342, Winter 2006 Lecture Notes
CSE 373 AVL trees read: Weiss Ch. 4, section
AVL Trees CENG 213 Data Structures.
CS202 - Fundamental Structures of Computer Science II
AVL Trees: AVL Trees: Balanced binary search tree
CS202 - Fundamental Structures of Computer Science II
CSE 373: Data Structures and Algorithms
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
Copyright © Aiman Hanna All rights reserved
Data Structures & Algorithms
CSE 373: Data Structures and Algorithms
AVL Trees CSE 373 Data Structures.
CSE 332: Data Abstractions AVL Trees
AVL Search Tree put(9)
CSE 373 Data Structures and Algorithms
Lecture No.20 Data Structures Dr. Sohail Aslam
Dynamic Dictionaries Primary Operations: Additional operations:
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
CE 221 Data Structures and Algorithms
CS202 - Fundamental Structures of Computer Science II
Lecture 9: Self Balancing Trees
AVL Tree By Rajanikanth B.
AVL-Trees (Part 1).
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Richard Anderson Spring 2016
CSE 373 Data Structures Lecture 8
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees - Binary Trees with additional structure. BST ADT Performance AVL Trees CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -

(recall:) The Dictionary ADT put(x, k) : Insert data element x with key k. get(k) : find and return the data element having key k. CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -

Binary Search Trees: Motivation We wish to implement the Dictionary ADT with the additional method “getAll” (return all data elements in ascending order of their keys). Keys are now assumed to be ordered (“comparable”). Hashing with linear open addressing requires (D + n log n) for getAll, whereas BSTs can do it in O(n). D = size of hash table. n = number of data elements. CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -

Binary Search Trees: Definition A Binary Search Tree (or BST, for short) is a binary tree with the following added requirements: Each node stores a data element e and a key k. The keys come from a totally ordered set (e.g., integers, or strings with lexicographic ordering). For each node, any and all keys in its left subtree are less than its own key, and any and all keys in its right subtree are greater than its own. (We typically assume that all the keys in a BST are distinct. If not, we should call the structure a BST with duplicates.) CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -

CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees - BST Example 9.4 Sue Dow 3.7 John Smith 20.2 Chi Wang 3.2 Zoe Zebow 7.0 Mary Ives 21.0 Amy Berg CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -

CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees - Performance Put: Worst case O(n), expected case if tree is random O(log n) Get: same as Put. getAll: O(n). CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -

AVL Trees invented by Adelson-Velskii and Landis. An AVL tree is a binary tree such that: if it’s non-empty then its subtrees TL and TR satisfy | height(TL) - height(TR) |  1 and TL and TR are also AVL trees. An AVL search tree is an AVL tree that is also a BST CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -

CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees - Example AVL Tree 1 1 CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -

Negative Example AVL Tree 1 1 CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -

Example AVL Search Tree F B 1 D G 1 E CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -

CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees - AVL Tree: Lookup cost Time to perform FIND(k) in AVL tree T is proportional to the depth of the node containing key k. Depth(k)  height(T). height(T)  c log2 n where c  1.44 (see Sahni, p.605). Therefore, the time for FIND(k) is O(log n) in both the worst case and expected case. The worst case time for hashing is (D + n) although the expected case time when the load factor is low is O(1). CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -

AVL (Search) Tree (Easy) Insertion Example F B 1 D G CAT 1 E CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -

AVL (Search) Tree (Harder) Insertion Example F Adding “BAD” causes A to go out of balance B 1 D G BAD CAT 1 E CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -

AVL Tree Single Rotation Left C B F A BAD 1 D G CAT 1 E CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -

CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees - Now insert EGG C B F A BAD 1 D G CAT E 1 EGG 1 CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -

Prepare for Left rotation C B F A BAD 1 D G CAT E 1 EGG 1 CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -

CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees - Left Rotation Done C B F A BAD 1 1 E G D EGG 1 CAT CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -

Prepare for Right Rotation C B F A BAD 1 1 E G D EGG 1 CAT CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -

Right Rotation Done; Balance Restored 1 A BAD 1 D F CAT EGG 1 G CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -

CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees - Use of Balance Factors -1 C B E 1 +1 A BAD 1 D F CAT EGG 1 G 0 = balanced +1 = left heavy -1 = right heavy CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -

CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees - Insert FUN -2 C -1 B E 1 +1 -1 A BAD 1 D F CAT EGG 1 G +1 0 = balanced +1 = left heavy -1 = right heavy FUN CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -

Prepare to Rotate left at C -2 C -1 B E 1 +1 -1 A BAD 1 D F CAT EGG 1 G +1 0 = balanced +1 = left heavy -1 = right heavy FUN CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -

CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees - Rebalanced E -1 C F 1 +1 +1 B D EGG 1 G A BAD 1 CAT FUN CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -

General AVL Insertion Method Find the place to insert the new key. (While descending the tree, remember the last node having balance factor = -1 or +1. Call it A.) If the key is already there, return. Otherwise insert the new key. If there is no A, go down the path from the root to the new key again, updating balance factors, and then return. Go back to A. If it had bf = 1 and the key went into its right subtree, or if it had bf = -1 and the key went into its left subtree, then set bf(A)=0, fix the bf values on the path from A to the new key, and return. Otherwise, if A had bf = 1 and the new key went into its left subtree, then perform either a LL rotation or a LR rotation depending upon whether the key went into the left or right subtree of A’s left subtree. If it had bf = -1 and the new key went into its right subtree, then perform either a RR rotation or a RL rotation depending upon whether the key went into the right or left subtree of A’s right subtree. Fix the bf values of the nodes involved in the rotation. Correct the bf values of the nodes on the path from A to the new key. CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -

Single rotations classified according to which grandchild is too heavy LL = single rotation right RR = single rotation left CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -

Double rotations classified according to which grandchild is too heavy LR = double rotation right RL = double rotation left CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -

LL Rotation: Schematic Z Y X Z Y T4 X T4 T3 T1 T2 T3 T1 T2 CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -

LR Rotation: Schematic Z Y X Z X T4 Y T4 T1 T2 T3 T1 T2 T3 CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -

Deletion from an AVL Tree (Assume that we have used the key to find the node q to be deleted.) Delete the key from the node q it’s in. Let p be the parent of q. If q had only one child, make it the new child of p and delete q. If q had two children, move the key of the left child into q and delete the left child node of q. Since the deletion may have caused the balance factors to change on the nodes on the path from the root to p, we ascend back along this path from p to the root, fixing the balance factors. At each node, we determine the new balance factor and if the node is unbalanced, perform one of the 4 rotations. If we reach a node whose height ends up the same as it was before the deletion, then the balance factors of its ancestors will also keep their old values, and the deletion operation is complete. If we reach the root and rebalance it, then the deletion operation is also complete. CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -