Foundations of Data Structures Practical Session #7 AVL Trees 2.

Slides:



Advertisements
Similar presentations
Mathematical Preliminaries
Advertisements

COSC 2007 Data Structures II Chapter 12 Advanced Implementation of Tables II.
Advanced Piloting Cruise Plot.
1 Vorlesung Informatik 2 Algorithmen und Datenstrukturen (Parallel Algorithms) Robin Pomplun.
© 2008 Pearson Addison Wesley. All rights reserved Chapter Seven Costs.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Chapter 1 The Study of Body Function Image PowerPoint
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
1 Copyright © 2013 Elsevier Inc. All rights reserved. Appendix 01.
Properties Use, share, or modify this drill on mathematic properties. There is too much material for a single class, so you’ll have to select for your.
Introduction to Algorithms
© 2001 by Charles E. Leiserson Introduction to AlgorithmsDay 18 L10.1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 10 Prof. Erik Demaine.
and 6.855J Spanning Tree Algorithms. 2 The Greedy Algorithm in Action
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
Chapter 3: Top-Down Design with Functions Problem Solving & Program Design in C Sixth Edition By Jeri R. Hanly & Elliot B. Koffman.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Addition Facts
Year 6 mental test 5 second questions
Year 6 mental test 10 second questions
REVIEW: Arthropod ID. 1. Name the subphylum. 2. Name the subphylum. 3. Name the order.
CS16: Introduction to Data Structures & Algorithms
Binary Tree Structure a b fe c a rightleft g g NIL c ef b left right pp p pp left key.
Data Structures: A Pseudocode Approach with C
Data Structures ADT List
Data Structures Using C++
ABC Technology Project
1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.
EU market situation for eggs and poultry Management Committee 20 October 2011.
1 Undirected Breadth First Search F A BCG DE H 2 F A BCG DE H Queue: A get Undiscovered Fringe Finished Active 0 distance from A visit(A)
VOORBLAD.
1 Breadth First Search s s Undiscovered Discovered Finished Queue: s Top of queue 2 1 Shortest path from s.
CSE 373 Data Structures and Algorithms
David Luebke 1 8/25/2014 CS 332: Algorithms Red-Black Trees.
Factor P 16 8(8-5ab) 4(d² + 4) 3rs(2r – s) 15cd(1 + 2cd) 8(4a² + 3b²)
Squares and Square Root WALK. Solve each problem REVIEW:
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
CSE 4101/5101 Prof. Andy Mirzaian B-trees trees.
CONTROL VISION Set-up. Step 1 Step 2 Step 3 Step 5 Step 4.
© 2012 National Heart Foundation of Australia. Slide 2.
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 11: Priority Queues and Heaps Java Software Structures: Designing.
Addition 1’s to 20.
Model and Relationships 6 M 1 M M M M M M M M M M M M M M M M
25 seconds left…...
Januar MDMDFSSMDMDFSSS
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
Week 1.
10 -1 Chapter 10 Amortized Analysis A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)
Analyzing Genes and Genomes
Binary Search Trees Ravi Chugh March 28, Review: Linked Lists Goal: Program that keeps track of friends Problem: Arrays have fixed length Solution:
We will resume in: 25 Minutes.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Chapter 12 Binary Search Trees
CSE Lecture 17 – Balanced trees
Rizwan Rehman Centre for Computer Studies Dibrugarh University
Intracellular Compartments and Transport
PSSA Preparation.
AVL Tree Example: Insert 14, 17, 11, 7, 53, 4, 13 into an empty AVL tree
Essential Cell Biology
יסודות מבנה נתונים תרגול 6 Foundations of Data Structures /Spring, Amihai Savir & Ilya Mirsky AVL TREES.
Presentation transcript:

Foundations of Data Structures Practical Session #7 AVL Trees 2

AVL Tree properties Height-Balance Property AVL Tree AVL Interface AVL Height 2

AVL Tree example

Question 1 Insert the following sequence of integers into an empty AVL tree: 14, 17, 11, 7, 53, 4,

right A single right rotation of ’11’ is executed to rebalance the tree: Insert

Now insert The sub-tree of 11 is unbalanced. Double rotation: right and then left.

After right rotation of ’13’ Now left rotate ’11’

After left rotation of ’11’ Now balanced!

Now insert The sub-tree of 7 is unbalanced. Required double rotation: right and then left.

After right rotation of ’12’ Now left rotate ‘7’

Now balanced!

Remove

Unbalanced! Right rotate ’14’

Balanced! Remove

Remove 11 Replace it with the maximum in its left branch

Remove

Unbalanced! Required double rotatation

After right rotation of ‘14’

After left rotation of ‘7’

Question 2 In class we’ve seen an implementation of AVL tree where each node v has an extra field h, the height of the sub-tree rooted at v. The height can be used in order to balance the tree. -How many bits are required to store the height in a node? -Answer: For an AVL tree with n nodes, h=O(logn) thus requires O(loglogn) extra bits. 1.How can we reduce the number of the extra bits necessary for balancing the AVL tree? 2.Suggest an algorithm for computing the height of a given AVL tree given in the representation you suggested in 1. 20

Question 2 solution balance 1.Instead of a height field, which is redundant, each node will store 2 balance bits, calculated as the difference of heights between its right and left sub-trees. Two bits suffice because the difference can be one of the three: -1, 0, 1. (The leftmost bit represents the sign) The balance field should be updated on insert and delete operations, along the path to the root. 21

Question 2 solution balance 2.To compute the height of a tree, follow the path from the root to the deepest leaf by reading the balance field. If a sub tree is balanced to one side, the deepest leaf resides on that side. 22 CalcHeight(T) if T == null return -1 if T.balance == -1 or T.balance == 0 return 1 + CalcHeight( T.left ) else return 1 + CalcHeight( T.right )

Question 3 23

Question 3 solution 24

Question 3 solution 25

Question 3 solution Reminder: 26 TREE-SUCCESSOR(x) If x.right != NULL then return TREE-MINIMUM(x.right) y ← x.parent while y != NULL and x == y.right do x ← y y ← y.parent return y

Question 3 solution 27

Question 4 Suggest an efficient algorithm for sorting an array of numbers. Analyze its running time and required space. 28

Question 4 solution 29

Question 5 Suggest a data structure for storing integers that supports the following operations. 30 Init()Initialize the data structure.O(1) Insert(x)Insert x, if it is not present yet.O(log n) Delete(x)Delete x if it exists.O(log n) DeletePlace(i) Delete the element in the i th place (as determined by the order of insertion). O(log n) GetPlace(x) Return the place (which is determined by the order of insertion) of x. If x does not exist, return -1. O(log n)

Question 5 solution For example, for the following sequence of actions: Insert(3), Insert(5), Insert(11), Insert(4), Insert(7), Delete(5) GetPlace(7) returns 4, and DeletePlace(2) will delete 11. The solution We will use two AVL trees: T1 stores the elements by their key. T2 stores the elements by the order of insertion (using a running counter). There are pointers between the two trees connecting the nodes with the same key. 31

Question 5 solution 32