Augmenting AVL trees.

Slides:



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

CS261 Data Structures AVL Trees. Goals Pros/Cons of a BST AVL Solution – Height-Balanced Trees.
Trees Types and Operations
Time Complexity of Basic BST Operations Search, Insert, Delete – These operations visit the nodes along a root-to- leaf path – The number of nodes encountered.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Augmenting AVL trees. How we’ve thought about trees so far Good for determining ancestry Can be good for quickly finding an element Good for determining.
4a-Searching-More1 More Searching Dan Barrish-Flood.
Tirgul 5 This tirgul is about AVL trees. You will implement this in prog-ex2, so pay attention... BTW - prog-ex2 is on the web. Start working on it!
AVL Trees ITCS6114 Algorithms and Data Structures.
Tirgul 6 B-Trees – Another kind of balanced trees.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Lecture 11COMPSCI.220.FS.T Balancing an AVLTree Two mirror-symmetric pairs of cases to rebalance the tree if after the insertion of a new key to.
Week 8 - Wednesday.  What did we talk about last time?  Level order traversal  BST delete  2-3 trees.
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.
Augmenting AVL trees. How we’ve thought about trees so far Good for determining ancestry Can be good for quickly finding an element Good for determining.
CSE332: Data Abstractions Lecture 7: AVL Trees
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
BCA-II Data Structure Using C
Week 7 - Friday CS221.
Multiway Search Trees Data may not fit into main memory
Binary Search Trees A binary search tree is a binary tree
Binary Search Trees Rem Collier Room A1.02
BST Trees
Augmenting AVL trees.
UNIT III TREES.
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
CSIT 402 Data Structures II
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
CO4301 – Advanced Games Development Week 10 Red-Black Trees continued
Lecture 22 Binary Search Trees Chapter 10 of textbook
MA/CSSE 473 Day 21 AVL Tree Maximum height 2-3 Trees
Hashing Exercises.
COMP 103 Binary Search Trees.
ITEC 2620M Introduction to Data Structures
O(lg n) Search Tree Tree T is a search tree made up of n elements: x0 x1 x2 x3 … xn-1 No function (except transverse) takes more than O(lg n) in the.
Lecture 25 Splay Tree Chapter 10 of textbook
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
AVL Trees: AVL Trees: Balanced binary search tree
Search Sorted Array: Binary Search Linked List: Linear Search
A Kind of Binary Tree Usually Stored in an Array
B- Trees D. Frey with apologies to Tom Anastasio
B- Trees D. Frey with apologies to Tom Anastasio
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
A Robust Data Structure
B- Trees D. Frey with apologies to Tom Anastasio
Binary Trees, Binary Search Trees
CSE 373: Data Structures and Algorithms
CSC 143 Binary Search Trees.
BST Insert To insert an element, we essentially do a find( ). When we reach a NULL pointer, we create a new node there. void BST::insert(const Comp &
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
ITCS6114 Algorithms and Data Structures
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Richard Anderson Spring 2016
การวิเคราะห์และออกแบบขั้นตอนวิธี
Lecture 9: Intro to Trees
CS 106B Lecture 20: Binary Search Trees Wednesday, May 17, 2017
Binary Trees, Binary Search Trees
CS 261 – Data Structures AVL Trees.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

Augmenting AVL trees

How we’ve thought about trees so far Good for determining ancestry Can be good for quickly finding an element

Enter: idea of augmenting a tree Other kinds of uses? Any thoughts? Finding a minimum/maximum… (heaps are probably just as good or better) Finding an average? More complicated things?!!!11one Enter: idea of augmenting a tree

Augmenting Can quickly compute many global properties that seem to need knowledge of the whole tree! Examples: size of any sub-tree height of any sub-tree averages of keys/values in a sub-tree min+max of keys/values in any sub-tree, … Can quickly compute any function f(x) so long as you only need to know f(x.left) and f(x.right)!

Augmenting an AVL tree Can augment any kind of tree Only balanced trees are guaranteed to be fast After augmenting an AVL tree to compute f(x), we can still do all operations in O(lg n)!

Simple first example We are going to do one simple example Then, you will help with a harder one! Problem: augment an AVL tree so we can do: Insert(key): add key in O(lg n) Delete(key): remove key in O(lg n) Height(node): get height of sub-tree rooted at node in O(1) A regular AVL tree already does this Store some extra data at each node… but what? How do we do this?

Can we compute this function quickly? Function we want to compute: Height(u) = H(u) If someone gives us H(uL) and H(uR), can we compute H(u)? What formula should we use? If u = null then H(u) = 0 Else H(u) = max{H(uL), H(uR)}+1 u uL uR H(uL) H(u)=? H(uR)

Augmenting AVL tree to compute H(u) Each node u contains key: the key left, right: child pointers h: height of sub-tree rooted at u How? The usual stuff… Secret sauce! d, 1 d, 1 d, 0 d, 2 d, 2 d, 2 Insert(d) a, 1 a, 0 e, 0 b, 0 b, 1 e, 0 Insert(a) Insert(e) b, 0 a, 0 a, 1 c, 0 c, 0 Insert(b) Insert(c) c, 0

Algorithm idea: From the last slide, we develop an algorithm Insert(u): 1 BST search for where to put u 2 Insert u into place like in a regular AVL tree 3 Fix balance factors and rotate as you would in AVL insert, but fix heights at the same time. (Remember to fix heights all the way to the root. Don’t stop before reaching the root!) (When you rotate, remember to fix heights of all nodes involved, just like you fix balance factors!)

Harder problem: overpaid employees! You have a record for each employee in your company with salary and age. We want to quickly find overpaid employees of age <= A.

Breaking the problem down You must design a data structure D of employee records to efficiently do: Insert(D; x): If x is a pointer to a new employee record, insert the record pointed to by x into D. Delete(D; x): If x is a pointer to an existing employee record in D, remove that record from D. MaxSal(D; A): Return the largest salary, amongst all employees in D whose age is <= A; if there is no such employee, then return -1. All functions must run in O(lg n)

Part 1 Give a precise and full description of your data structure Illustrate this data structure by giving an example of it on some collection of employee records of your own choice.

Figuring out the data structure Iterative process… hard to get it right the first time! What function do we want to compute? Maximum salary for employees with age <= A What info should we store at each node u? Maybe maximum salary for all employees in the sub-tree rooted at u? Let’s call this value MS(u). How can we compute MS(u) quickly (i.e., by looking at a small number of descendents of u)?

Organizing nodes inside the tree How can we turn maximum salary in a sub-tree into maximum salary for those with age <= A? For sure, need to relate age with sub-trees! Things are organized into sub-trees by key… So, age must be related to key. Let age be the key of each node… May have 2+ nodes with same key, but that’s okay.

Answer to part 1 Age Salary 1100 31000 1247 29000 1590 28000 1100 31000 1247 29000 1590 28000 1590 48000 3801 41000 17000 36000 30000 90000 30000 75000 Answer to part 1 Age=17000, salary=36000, MS= Age=17000, salary=36000, MS=90000 Age=17000, MS= Age=1590, salary=28000, MS=48000 Age=1590, salary=28000, MS= Age=1590, MS= Age=30000, salary=75000, MS=90000 Age=30000, salary=75000, MS= Age=24100, MS= Age=1100, salary=31000, MS=31000 Age=1100, salary=31000, MS= Age=1100, MS= Age=3801, MS= Age=3801, salary=41000, MS=48000 Age=3801, salary=41000, MS= Age=30000, salary=90000, MS=90000 Age=30000, salary=90000, MS= Age=30000, MS= Age=1247, salary=29000, MS= Age=1247, MS= Age=1247, salary=29000, MS=29000 Age=1590, salary=48000, MS=48000 Age=1590, MS= Age=1590, salary=48000, MS=

Part 2 What does “fix MS(u)” mean? What formula do we use? Explain how to implement Insert(D, x), Delete(D, x) and MaxSal(D, A) in O(log n) time Insert: 1 Do regular AVL-insert(D, <x.age, x.salary, MS=0>), except: As you move up the tree fixing balance factors, fix MS(u) for every node on the path to the root After each rotation, fix each MS(u) for each node that was involved in the rotation Remember to fix heights all the way to the root. (Don’t stop early, even if done balance factors/rotations!) Delete: Same as Insert, but “Do regular AVL-delete[…]” How do we do MaxSal??? What does “fix MS(u)” mean? What formula do we use? Set MS(u) = max{u.salary, MS(uL), MS(uR)}

How can we compute MaxSal? Write a recursive function! Recursion is often very natural for trees Big problem: want MaxSal(D, A) = largest salary for age <= A in tree Smaller problem: MaxSal(u, A) = largest salary for age <= A in sub-tree rooted at u Recursive/inductive measure of problem size: size of sub-tree Therefore, smaller problem = smaller sub-tree.

Calling MaxSal on BOTH sub-trees can take O(n) time! Code for MaxSal // u is a node, A is an age MaxSal(u, A) if u = null then return -1 else if A < u.age then return MaxSal(uL, A) else return max{u.salary, MaxSal(uR,A), MaxSal(uR,A)} u: salary, age, MS uL: salary, age, MS uR: salary, age, MS MS(uL) Calling MaxSal on BOTH sub-trees can take O(n) time!

Code for MaxSal // u is a node, A is an age MaxSal(u, A) if u = null then return -1 else if A < u.age then return MaxSal(uL, A) else return max{u.salary, MaxSal(uR,A), MS(uL)} u: salary, age, MS uL: salary, age, MS uR: salary, age, MS MS(uL)

Why O(lg n) time? Insert/Delete: normal AVL operation = O(lg n) MaxSal PLUS: update MS(u) for each u on path to the root Length of this path <= tree height, so O(lg n) in an AVL tree PLUS: update MS(u) for each node involved in a rotation At most O(lg n) rotations (one per node on the path from the root <= tree height) Each rotation involves a constant number of nodes Therefore, constant * O(lg n), which is O(lg n). MaxSal Constant work + recursive call on ONE child Single recursive call means O(tree height) = O(lg n)