Cinda Heeren / Geoffrey Tien

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
1 CSE 373 AVL trees, continued read: Weiss Ch. 4, section slides created by Marty Stepp
CS202 - Fundamental Structures of Computer Science II
ITEC200 Week 11 Self-Balancing Search Trees. 2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.
TCSS 342 AVL Trees v1.01 AVL Trees Motivation: we want to guarantee O(log n) running time on the find/insert/remove operations. Idea: keep the tree balanced.
CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties and maintenance.
Fall 2007CS 2251 Self-Balancing Search Trees Chapter 9.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
AVL Trees / Slide 1 Balanced Binary Search Tree  Worst case height of binary search tree: N-1  Insertion, deletion can be O(N) in the worst case  We.
CSC 2300 Data Structures & Algorithms February 13, 2007 Chapter 4. Trees.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 45 AVL Trees and Splay.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower.
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.
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.
Chapter 10: Search Trees Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data.
Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.
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.
Data Structures AVL Trees.
AVL Trees An AVL tree is a binary search tree with a balance condition. AVL is named for its inventors: Adel’son-Vel’skii and Landis AVL tree approximates.
AVL Trees / Slide 1 Height-balanced trees AVL trees height is no more than 2 log 2 n (n is the number of nodes) Proof based on a recurrence formula for.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 20 AVL Trees.
AVL Trees AVL (Adel`son-Vel`skii and Landis) tree = – A BST – With the property: For every node, the heights of the left and right subtrees differ at most.
CE 221 Data Structures and Algorithms Chapter 4: Trees (AVL Trees) Text: Read Weiss, §4.4 1Izmir University of Economics.
CSE332: Data Abstractions Lecture 7: AVL Trees
AVL Trees CSE, POSTECH.
Part-D1 Binary Search Trees
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
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.
Cinda Heeren / Geoffrey Tien
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
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.
AVL Tree Mohammad Asad Abbasi Lecture 12
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
Red-Black Trees Motivations
Binary Search 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.
AVL Trees CENG 213 Data Structures.
Friday, April 13, 2018 Announcements… For Today…
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
CSE 373: Data Structures and Algorithms
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
CSE 373: Data Structures and Algorithms
AVL Search Tree put(9)
CE 221 Data Structures and Algorithms
Self-Balancing Search Trees
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
Data Structures Lecture 21 Sohail Aslam.
AVL-Trees (Part 1).
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
1 Lecture 13 CS2013.
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
Presentation transcript:

Cinda Heeren / Geoffrey Tien AVL Trees Properties Insertion October 17, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien BST rotation g g e h b h b f i a e i a c c f d d Rotations preserve the in-order BST property, while altering the tree structure we can use rotations to bring imbalanced trees back into balance October 12, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien AVL trees An AVL tree is a balanced BST Each node's left and right subtrees differ in height by at most 1 Rebalancing via rotations occurs when an insertion or removal causes excessive height difference AVL tree nodes contain extra information to support this height information 43 19 63 57 60 50 78 38 4 21 October 17, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien AVL nodes enum balance_type {LEFT_HEAVY = -1, BALANCED = 0, RIGHT_HEAVY = +1}; class AVLNode { public: int data; // or template type AVLNode left; AVLNode right; balance_type balance; AVLNode(int value) { ... } AVLNode(int val, AVLNode left1, AVLNode right1) { ... } } AVLNode is almost the same as a binary tree node additional balance field indicates that state of subtree balance at that node October 17, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien AVL imbalance Balanced trees: 3 3 7 12 16 3 7 12 19 3 16 27 31 44 Imbalanced trees: 12 19 3 16 27 31 44 21 24 12 16 3 7 9 3 7 5 October 17, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien AVL imbalance 4 cases of imbalance C B A C A B A B C A C B LL imbalance LR imbalance RR imbalance RL imbalance Solve with a right rotation around C Left rotation around A, becomes LL case Symmetric to left imbalance cases October 17, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien AVL insertion Maintaining balance The best way to keep a tree balanced, is to never let it become imbalanced! AVL insertion begins with ordinary BST insertion (i.e. a leaf node) followed by rotations to maintain balance i.e. AVL properties are satisfied before and after insertion if the balance attribute of a subtree's root node becomes critical (-2 or +2) as a result of inserting the new leaf, rebalance it! October 17, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien AVL insertion Pseudocode if root is NULL Create new node containing item, assign root to it, and return true else if item is equal to root->data item exists already, return false else if item < root->data Recursively insert the item into the left subtree if height of left subtree has increased (increase variable is true) balance--; if balance == 0, reset increase variable to false if balance < -1 reset increase variable to false perform rebalanceLeft else if item > root->data (symmetric to left subtree case, incrementing balance) rebalanceLeft and rebalanceRight are the rotations to correct the 4 imbalance cases October 17, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien AVL insertion example Insert(65) 47 32 71 65 93 October 17, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien AVL insertion example Insert(65) Insert(82) 47 RR imbalance 32 71 OK 65 93 OK 82 OK October 17, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien AVL insertion example Insert(65) Insert(82) 71 Insert(87) 47 93 LR imbalance 32 65 82 OK 87 OK October 17, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien AVL insertion example Insert(65) Insert(82) 71 OK Insert(87) 47 87 OK 32 65 82 93 October 17, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien AVL tree height The height of an AVL tree containing 𝑛 key values is 𝑂 log 𝑛 Intuition: For a fixed height ℎ, a tree containing fewer nodes has a larger height-to-node ratio 𝑛=15 𝑛=4 ℎ=3= log 𝑛 =𝑂( log 𝑛 ) ℎ=3=𝑛−1=𝑂 𝑛 Attempt to achieve the worst ratio by making an AVL tree of height ℎ with the minimum number of nodes October 17, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien AVL tree height Theorem: The height of an AVL tree with 𝑛 nodes is 𝑂 log 𝑛 Proof: Let 𝑁 ℎ represent the minimum number of nodes in an AVL tree of height ℎ Since the AVL property must be satisfied at every node, the children of such a tree must also be minimal, and the height difference between the children must be 1 Thus 𝑁 ℎ =1+ 𝑁 ℎ−1 + 𝑁 ℎ−2 1 Nh-2 Nh-1 October 17, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien AVL tree height 𝑁 ℎ =1+ 𝑁 ℎ−1 + 𝑁 ℎ−2 𝑁 ℎ−1 =1+ 𝑁 ℎ−2 + 𝑁 ℎ−3 𝑁 ℎ =1+ 1+ 𝑁 ℎ−2 + 𝑁 ℎ−3 + 𝑁 ℎ−2 =2+2 𝑁 ℎ−2 + 𝑁 ℎ−3 >2 𝑁 ℎ−2 𝑁 ℎ−2 =1+ 𝑁 ℎ−3 + 𝑁 ℎ−4 =2+2 𝑁 ℎ−4 + 𝑁 ℎ−5 >2 𝑁 ℎ−4 𝑁 ℎ >2∙2 𝑁 ℎ−4 𝑁 ℎ >2∙2∙2 𝑁 ℎ−6 How many times can we subtract 2 from ℎ before we reach 0? ℎ 2 times. 𝑁 ℎ >2∙2∙2∙2 𝑁 ℎ−8 … 𝑁 ℎ > 2 ℎ 2 ℎ< log 𝑁 ℎ ℎ∈𝑂 log 𝑛 October 17, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Exercise Draw the AVL tree resulting from the following sequence of insertions: 71, 13, 65, 22, 49, 37, 45 Note the height recorded/updated at each node What would an ordinary BST look like with the same sequence of insertions? October 17, 2017 Cinda Heeren / Geoffrey Tien

Readings for this lesson Koffman Chapter 11.2 (AVL trees) AVL visualisations: http://visualgo.net/en/bst (AVL tree) http://www.cs.usfca.edu/~galles/visualization/AVLtree.html Next class: AVL removal October 17, 2017 Cinda Heeren / Geoffrey Tien