April 19th – Avl Operations

Slides:



Advertisements
Similar presentations
1 AVL Trees (10.2) CSE 2011 Winter April 2015.
Advertisements

AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
CSE332: Data Abstractions Lecture 7: AVL Trees Tyler Robison Summer
AVL-Trees (Part 1) COMP171. AVL Trees / Slide 2 * Data, a set of elements * Data structure, a structured set of elements, linear, tree, graph, … * Linear:
CSE 326: Data Structures AVL Trees
AVL Trees ITCS6114 Algorithms and Data Structures.
1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
CMSC420: Splay Trees Kinga Dobolyi Based off notes by Dave Mount.
11/7/20151 Balanced Trees Data Structures Ananda Gunawardena.
AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.
Data Structures AVL Trees.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
CSE 3358 NOTE SET 13 Data Structures and Algorithms.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Catie Baker Spring 2015.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Linda Shapiro Spring 2016.
CSE332: Data Abstractions Lecture 7: AVL Trees
Lecture 23 Red Black Tree Chapter 10 of textbook
AVL Trees CSE, POSTECH.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AA Trees.
File Organization and Processing Week 3
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
Data Structures – LECTURE Balanced trees
Week 7 - Friday CS221.
CSE373: Data Structures & Algorithms
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
CSIT 402 Data Structures II
Week 6 - Wednesday CS221.
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.
Introduction Applications Balance Factor Rotations Deletion Example
CSE373: Data Structures & Algorithms
Splay Trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Cse 373 May 15th – Iterators.
Balanced Trees (AVL and RedBlack)
October 30th – Priority QUeues
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Cse 373 April 14th – TreEs pt 2.
Week 11 - Friday CS221.
March 31 – Priority Queues and the heap
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
Cse 373 April 12th – TreEs.
COMP 103 Binary Search Trees.
Cse 373 April 26th – Exam Review.
November 1st – Exam Review
Lecture 25 Splay Tree Chapter 10 of textbook
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Monday, April 16, 2018 Announcements… For Today…
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Wednesday, April 18, 2018 Announcements… For Today…
Data Structures and Algorithms
Instructor: Lilian de Greef Quarter: Summer 2017
David Kaplan Dept of Computer Science & Engineering Autumn 2001
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
AVL Trees CSE 373 Data Structures.
CSE 332: Data Abstractions AVL Trees
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
CS223 Advanced Data Structures and Algorithms
Lecture 9: Self Balancing Trees
AVL-Trees (Part 1).
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
ITCS6114 Algorithms and Data Structures
Richard Anderson Spring 2016
CSE 373 Data Structures Lecture 8
Lecture 9: Intro to Trees
Presentation transcript:

April 19th – Avl Operations Cse 373 April 19th – Avl Operations

Assorted minutiae Exam review Wednesday evening (Canvas announcement) Regrade requests for HW2 by end of day Monday

Today’s lecture Finish AVL Trees Proof Memory analysis Framework and concept

Review AVL Trees BST trees with AVL property Abs(height(left) – height(right)) <= 1 Heights of subtrees can differ by at most one This property must be preserved throughout the tree

Review 1 Add the following into an AVL Tree {1,2,3,5,4}

Review 1 2 Add 2, then verify balance

Review Add three, observe that the balance of ‘1’ is off. 2 2 1 3 Add three, observe that the balance of ‘1’ is off. What case is this?

Review Add three, observe that the balance of ‘1’ is off. 2 2 1 3 Add three, observe that the balance of ‘1’ is off. What case is this? Right-right

Review 1 2 2 1 3 Rotate the tree to preserve balance

Review Rotate the tree to preserve balance What is the new root? 1 2 2 3 Rotate the tree to preserve balance What is the new root?

Review Rotate the tree to preserve balance What is the new root? 2 1 2 3 Rotate the tree to preserve balance What is the new root? 2

Review 2 1 3 Perform the ‘left’ rotation which brings two into the root position

Review 2 1 3 5 Add the 5

Review 2 1 3 5 Add the 5 Verify balance

Review 2 1 3 5 4 Add the 4

Review 2 1 3 5 4 Add the 4 Verify balance

Review Add the 4 Verify balance. Which node(s) are out of balance? 2 1 3 5 4 Add the 4 Verify balance. Which node(s) are out of balance?

Review Add the 4 Verify balance. Which node(s) are out of balance? 2 1 3 5 4 Add the 4 Verify balance. Which node(s) are out of balance?

Review 2 1 3 5 4 Which rotation will fix the tree?

Review Which rotation will fix the tree? 2 1 3 5 4 Which rotation will fix the tree? Select the lowest out-of-balance node

Review Which rotation will fix the tree? 2 1 3 5 4 Which rotation will fix the tree? Select the lowest out-of-balance node (right-left case)

Review 2 1 3 5 4 What does the final tree look like?

Review The grandchild (4) moves up to the unbalanced position 2 1 4 3 5 The grandchild (4) moves up to the unbalanced position

Review The grandchild (4) moves up to the unbalanced position 2 1 4 3 5 The grandchild (4) moves up to the unbalanced position Observe the tree is balanced

Review 2 1 4 3 5 Work among yourselves, create an AVL tree from the input sequence

Review On your own or in small groups, produce the AVL tree from the following sequence of inputs. {10,20,15,5,0,-5}

Review On your own or in small groups, produce the AVL tree from the following sequence of inputs. {10,20,15,5,0,-5} Once you’ve finished this, think about why this balance condition is enough to give us a tree height in O(log n)

Review {10,20,15,5,0,-5} 5

Review {10,20,15,5,0,-5} 5 15

Review {10,20,15,5,0,-5} 5 15 -5 10 20

Review {10,20,15,5,0,-5} 5 15 -5 10 20

AVL Height Do we get O(log n) height from this balance?

AVL Height Do we get O(log n) height from this balance? We can get somewhat unbalanced trees

AVL Height Do we get O(log n) height from this balance? We can get somewhat unbalanced trees Are the balanced enough?

AVL Height (Proof) You do not need to memorize this proof, but it is interesting to think about

AVL Height (Proof) You do not need to memorize this proof, but it is interesting to think about Let’s consider the most “unbalanced” AVL tree, that is: the tree for each height that has the fewest nodes

AVL Height (Proof) For height 1, there is only one possible tree.

AVL Height (Proof) For height 1, there is only one possible tree. For height 2, there are two possible trees, each with two nodes.

AVL Height (Proof) For height 1, there is only one possible tree. For height 2, there are two possible trees, each with two nodes.

AVL Height (Proof) What about for height three? What tree has the fewest number of nodes?

AVL Height (Proof) What about for height three? What tree has the fewest number of nodes? Hint: balance will probably not be zero

AVL Height (Proof) What about for height three? What tree has the fewest number of nodes? Hint: balance will probably not be zero

AVL Height (Proof) What about for height three? What tree has the fewest number of nodes? Hint: balance will probably not be zero There are multiple of these trees, but what’s special about it?

AVL Height (Proof) The smallest tree of size three is a node where one child is the smallest tree of size one and the other one is the smallest tree of size two.

AVL Height (Proof) In general then, if N1 = 1 and N2 = 2 and N3 = 4, what is Nk?

AVL Height (Proof) In general then, if N1 = 1 and N2 = 2 and N3 = 4, what is Nk? Powers of two seems intuitive, but this is a good case of why 3 doesn’t always make the pattern.

AVL Height (Proof) In general then, if N1 = 1 and N2 = 2 and N3 = 4, what is Nk? Powers of two seems intuitive, but this is a good case of why 3 doesn’t always make the pattern. N4 = 7, how do I know?

AVL Height (Proof) In general then, if N1 = 1 and N2 = 2 and N3 = 4, what is Nk? Nk = 1 + Nk-1 + Nk-2 Because the smallest AVL tree is a node (1) with a child that is the smallest AVL tree of height k-1 (Nk-1) and the other child is the smallest AVL tree of height k-2 (Nk-2).

AVL Height (Proof) In general then, if N1 = 1 and N2 = 2 and N3 = 4, what is Nk? Nk = 1 + Nk-1 + Nk-2 Because the smallest AVL tree is a node (1) with a child that is the smallest AVL tree of height k-1 (Nk-1) and the other child is the smallest AVL tree of height k-2 (Nk-2). This means every non-leaf has balance 1

AVL Height (Proof) In general then, if N1 = 1 and N2 = 2 and N3 = 4, what is Nk? Nk = 1 + Nk-1 + Nk-2 Because the smallest AVL tree is a node (1) with a child that is the smallest AVL tree of height k-1 (Nk-1) and the other child is the smallest AVL tree of height k-2 (Nk-2). This means every non-leaf has balance 1 Nothing in the tree is perfectly balanced.

AVL Height (Proof) Nk = 1 + Nk-1 + Nk-2 Nk-1 = 1 + Nk-2 + Nk-3

AVL Height (Proof) Nk = 1 + Nk-1 + Nk-2 Nk-1 = 1 + Nk-2 + Nk-3

AVL Height (Proof) Nk = 1 + Nk-1 + Nk-2 Nk-1 = 1 + Nk-2 + Nk-3 Substitute the k-1 into the original equation Nk = 1 + Nk-1 + Nk-2 Nk-1 = 1 + Nk-2 + Nk-3

AVL Height (Proof) Nk = 1 + Nk-1 + Nk-2 Nk-1 = 1 + Nk-2 + Nk-3 1 + Nk-3 must be greater than zero Nk = 1 + Nk-1 + Nk-2 Nk-1 = 1 + Nk-2 + Nk-3 Nk = 1 + (1 + Nk-2 + Nk-3) + Nk-2 Nk = 1 + 2Nk-2 + Nk-3 Nk > 2Nk-2

AVL Height (Proof) Nk = 1 + Nk-1 + Nk-2 Nk-1 = 1 + Nk-2 + Nk-3 1 + Nk-3 must be greater than zero Nk = 1 + Nk-1 + Nk-2 Nk-1 = 1 + Nk-2 + Nk-3 Nk = 1 + (1 + Nk-2 + Nk-3) + Nk-2 Nk = 1 + 2Nk-2 + Nk-3 Nk > 2Nk-2 This means the tree doubles in size after every two height (compared to a perfect tree which doubles with every added height)

AVL Conclusion If AVL rotation can enforce O(log n) height, what are the asymptotic runtimes for our functions?

AVL Conclusion If AVL rotation can enforce O(log n) height, what are the asymptotic runtimes for our functions? Insert(key k, value v) Find(key k)

AVL Conclusion If AVL rotation can enforce O(log n) height, what are the asymptotic runtimes for our functions? Insert(key k, value v) Find(key k) Delete(key k): not covered in this class

AVL Conclusion If AVL rotation can enforce O(log n) height, what are the asymptotic runtimes for our functions? Insert(key k, value v) Find(key k) : O(height) = O(log n) Delete(key k): not covered in this class

AVL Conclusion If AVL rotation can enforce O(log n) height, what are the asymptotic runtimes for our functions? Insert(key k, value v) = O(log n) + balancing Find(key k) : O(height) = O(log n) Delete(key k): not covered in this class

AVL Conclusion If AVL rotation can enforce O(log n) height, what are the asymptotic runtimes for our functions? Insert(key k, value v) = O(log n) + balancing Find(key k) : O(height) = O(log n) Delete(key k): not covered in this class How long does it take to perform a balance?

AVL Conclusion If AVL rotation can enforce O(log n) height, what are the asymptotic runtimes for our functions? Insert(key k, value v) = O(log n) + balancing Find(key k) : O(height) = O(log n) Delete(key k): not covered in this class How long does it take to perform a balance? There are at most three nodes and four subtrees to move around.

AVL Conclusion If AVL rotation can enforce O(log n) height, what are the asymptotic runtimes for our functions? Insert(key k, value v) = O(log n) + balancing Find(key k) : O(height) = O(log n) Delete(key k): not covered in this class How long does it take to perform a balance? There are at most three nodes and four subtrees to move around. O(1)

AVL Conclusion By using AVL rotations, we can keep the tree balanced

AVL Conclusion By using AVL rotations, we can keep the tree balanced An AVL tree has O(log n) height

AVL Conclusion By using AVL rotations, we can keep the tree balanced An AVL tree has O(log n) height This does not come at an increased asymptotic runtime for insert.

AVL Conclusion By using AVL rotations, we can keep the tree balanced An AVL tree has O(log n) height This does not come at an increased asymptotic runtime for insert. Rotations take a constant time.

Memory Analysis Similar to runtime analysis

Memory Analysis Similar to runtime analysis Consider the worst case

Memory Analysis Similar to runtime analysis Rather than counting the number of operations, we count the amount of memory needed

Memory Analysis Similar to runtime analysis Rather than counting the number of operations, we count the amount of memory needed During the operation, when does the algorithm need to “keep track” of the most number of things?

Memory Analysis Breadth first search

Memory Analysis Breadth first search The Queue keeps track of the elements that need to be analyzed next.

Memory Analysis Breadth first search The Queue keeps track of the elements that need to be analyzed next. This is the memory we need to consider

Memory Analysis Breadth first search The Queue keeps track of the elements that need to be analyzed next. This is the memory we need to consider At what point does the Queue have the most amount stored in it?

Memory Analysis Breadth first search The Queue keeps track of the elements that need to be analyzed next. This is the memory we need to consider At what point does the Queue have the most amount stored in it? When the tree is at its widest – how many nodes is that?

Memory Analysis Breadth first search The Queue keeps track of the elements that need to be analyzed next. This is the memory we need to consider At what point does the Queue have the most amount stored in it? When the tree is at its widest – how many nodes is that? N/2: half the nodes of a tree are leaves

Memory Analysis Consider finding an element in a sorted linked list

Memory Analysis Consider finding an element in a sorted linked list How much memory does this take?

Memory Analysis Consider finding an element in a sorted linked list How much memory does this take? Don’t count the data structure, only count the amount of memory that the actual algorithm uses.

Memory Analysis Consider finding an element in a sorted linked list How much memory does this take? Don’t count the data structure, only count the amount of memory that the actual algorithm uses. What does it need to “keep track” of?

Memory Analysis Consider finding an element in a sorted linked list How much memory does this take? Don’t count the data structure, only count the amount of memory that the actual algorithm uses. What does it need to “keep track” of? Just the think we’re looking for!

Memory Analysis Consider finding an element in a sorted linked list How much memory does this take? Don’t count the data structure, only count the amount of memory that the actual algorithm uses. What does it need to “keep track” of? Just the think we’re looking for! O(1)

Memory Analysis We care about the asymptotic memory usage

Memory Analysis We care about the asymptotic memory usage That is, as the input size of the data structures increases, does the amount of extra memory increase?

Memory Analysis We care about the asymptotic memory usage That is, as the input size of the data structures increases, does the amount of extra memory increase? AVL Insert?

Memory Analysis We care about the asymptotic memory usage That is, as the input size of the data structures increases, does the amount of extra memory increase? AVL Insert? No, we only need to keep track of the parent and grandparent.

Memory Analysis We care about the asymptotic memory usage That is, as the input size of the data structures increases, does the amount of extra memory increase? AVL Insert? No, we only need to keep track of the parent and grandparent. DFS?

Memory Analysis We care about the asymptotic memory usage That is, as the input size of the data structures increases, does the amount of extra memory increase? AVL Insert? No, we only need to keep track of the parent and grandparent. DFS? Yes, we need to keep track of all the elements leading back up to the root

Next Week Hashtables The O(1) holy grail!

Next Week Hashtables The O(1) holy grail! Exam review on Wednesday

Next Week Hashtables The O(1) holy grail! Exam review on Wednesday Exam on Friday!