1 AVL Trees (10.2) CSE 2011 Winter 2011 22 April 2015.

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia AVL Trees v z.
Advertisements

Rizwan Rehman Centre for Computer Studies Dibrugarh University
Splay Tree Algorithm Mingda Zhao CSC 252 Algorithms Smith College Fall, 2000.
AVL-Trees (Part 2) COMP171. AVL Trees / Slide 2 A warm-up exercise … * Create a BST from a sequence, n A, B, C, D, E, F, G, H * Create a AVL tree for.
The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
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.
1 AVL Trees. 2 AVL Tree AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children.
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.
Chapter 4: Trees Part II - AVL Tree
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Red-Black Trees 二○一七年四月十二日 AVL Trees v z AVL Trees.
AVL Trees Balanced Trees. AVL Tree Property A Binary search tree is an AVL tree if : –the height of the left subtree and the height of the right subtree.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
Data Structures Lecture 11 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
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:
CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties and maintenance.
CSC 213 Lecture 7: Binary, AVL, and Splay Trees. Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries):
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Search.
Binary Search Trees1 ADT for Map: Map stores elements (entries) so that they can be located quickly using keys. Each element (entry) is a key-value pair.
AVL Trees v z. 2 AVL Tree Definition AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the.
CSCE 3110 Data Structures & Algorithm Analysis
AVL Trees Amanuel Lemma CS252 Algoithms Dec. 14, 2000.
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.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
CSCE 3110 Data Structures & Algorithm Analysis AVL Trees Reading: Chap. 4, Weiss.
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.
© 2004 Goodrich, Tamassia Binary Search Trees1 CSC 212 Lecture 18: Binary and AVL Trees.
Chapter 10: Search Trees Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data.
CS 253: Algorithms Chapter 13 Balanced Binary Search Trees (Balanced BST) AVL Trees.
Fall 2006 CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties.
CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Binary Search Trees1 Chapter 3, Sections 1 and 2: Binary Search Trees AVL Trees   
CHAPTER 10 SEARCH TREES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND.
AVL Trees. AVL Tree In computer science, an AVL tree is the first-invented self-balancing binary search tree. In an AVL tree the heights of the two child.
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.
Balanced Search Trees 2-3 Trees AVL Trees Red-Black Trees
AVL Trees CSE, POSTECH.
Part-D1 Binary Search Trees
AA Trees.
BCA-II Data Structure Using C
AVL Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
AVL Trees AVL Trees.
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.
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
Binary Search Tree Chapter 10.
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
Chapter 2: Basic Data Structures
AVL Trees 11/10/2018 AVL Trees v z AVL 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.
TCSS 342, Winter 2006 Lecture Notes
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
Red-Black Trees 2018年11月26日3时46分 AVL Trees v z AVL Trees.
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
Copyright © Aiman Hanna All rights reserved
Binary Search Trees < > =
Lecture No.20 Data Structures Dr. Sohail Aslam
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
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
Dictionaries 二○一九年九月二十四日 ADT for Map:
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
Presentation transcript:

1 AVL Trees (10.2) CSE 2011 Winter April 2015

2 AVL Trees AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children of v can differ by at most 1. An example of an AVL tree where the heights are shown next to the nodes

3 Height of an AVL Tree Proposition: The height of an AVL tree T storing n keys is O(log n). Proof: Find n(h): the minimum number of internal nodes of an AVL tree of height h We see that n(1) = 1 and n(2) = 2 For h ≥ 3, an AVL tree of height h contains the root node, one AVL subtree of height h  1 and the other AVL subtree of height h  2. i.e. n(h) = 1 + n(h  1) + n(h  2)

4 Height of an AVL Tree (2) Knowing n(h-1) > n(h-2), we get n(h) > 2n(h-2) n(h) > 2n(h-2) n(h) > 4n(h-4) … n(h) > 2 i n(h-2i) Solving the base case we get: n(h) ≥ 2 h/2-1 Taking logarithms: h < 2log n(h) +2 Thus the height of an AVL tree is O(log n)

5 Insertion in an AVL Tree Insertion is as in a binary search tree. Always done by expanding an external node. Example: w b=x a=y c=z before insertionafter insertion

6 Insertion: rebalancing A binary search tree T is called balanced if for every node v, the height of v’s children differ by at most 1. Inserting a node into an AVL tree involves performing insertAtExternal(w, e) on T, which changes the heights of some of the nodes in T. If an insertion causes T to become unbalanced, we travel up the tree from the newly created node w until we find the first node z that is unbalanced. y = child of z with higher height (Note: y = ancestor of w) x = child of y with higher height (Note: x = ancestor of w or x = w) Since z became unbalanced by an insertion in the subtree rooted at its child y, height(y) = height(sibling(y)) + 2

Insertion: restructuring Now to rebalance... To rebalance the subtree rooted at z, we must perform a restructuring. Two methods: 1.cut/link restructuring (not in textbook) –Given 7 integer keys 1, 2, 3, 4, 5, 6, and 7, how do we build a balanced BST? 2.tri-node restructuring (textbook) 7

8 Cut/Link Restructure Algorithm Any tree that needs to be balanced can be grouped into 7 parts: x, y, z, and the 4 subtrees anchored at the children of those nodes (T 0, T 1, T 2, T 3 ). Any of the 4 subtrees can be empty (one dummy leaf).

9 Cut/Link Restructure Algorithm Number the 7 parts by doing an inorder traversal. x,y, and z are now renamed based upon their order within the inorder traversal.

10 Cut/Link Restructure Algorithm Now we can re-link these subtrees to the main tree. Link in node 4 (b) where the subtree’s root was.

11 Cut/Link Restructure Algorithm Link in nodes 2 (a) and 6 (c) as children of node 4.

12 Cut/Link Restructure Algorithm Finally, link in subtrees 1 and 3 as the children of node 2, and subtrees 5 and 7 as the children of 6. 1

13 Tri-node Restructuring To rebalance the subtree rooted at z, we must perform a restructuring. We rename x, y, and z to a, b, and c based on the order of the nodes in an in-order traversal (see the next slides for 4 possible mappings). z is replaced by b, whose children are now a and c whose children, in turn, consist of the 4 other subtrees formerly children of x, y, and z.

14 Tri-node Restructuring (2) Single rotations T0T0 T1T1 T2T2 T3T3

15 Tri-node Restructuring (3) Double rotations

16 Single/Double Rotations let (a,b,c) be an inorder listing of x, y, z perform the rotations needed to make b the topmost node of the three b=y a=z c=x T0T0 T1T1 T2T2 T3T3 b=y a=z c=x T0T0 T1T1 T2T2 T3T3 c=y b=x a=z T0T0 T1T1 T2T2 T3T3 b=x c=ya=z T0T0 T1T1 T2T2 T3T3 case 1: single rotation (a left rotation about a) case 2: double rotation (a right rotation about c, then a left rotation about a) (other two cases are symmetrical)

17 Restructuring Example unbalanced......balanced

18 Restructure Algorithm Algorithm restructure(x): Input: A node x of a binary search tree T that has both a parent y and a grandparent z Output: Tree T restructured by a rotation (either single or double) involving nodes x, y, and z. 1.Let (a, b, c) be an inorder listing of the nodes x, y, and z, and let (T0, T1, T2, T3) be an inorder listing of the the four subtrees of x, y, and z, not rooted at x, y, or z. 2.Replace the subtree rooted at z with a new subtree rooted at b 3.Let a be the left child of b and let T0, T1 be the left and right subtrees of a, respectively. 4.Let c be the right child of b and let T2, T3 be the left and right subtrees of c, respectively.

19 Cut/Link vs. Tri-node Restructuring Both methods give the same results. Both methods have the same time complexity. Time complexity = ? Cut/link method: –Advantage: no case analysis; more elegant. –Disadvantage: can be more code to write.

20 Removal First remove the node as in a BST. Performing a removeExternal(w) can cause T to become unbalanced. Let z be the first unbalanced node encountered while travelling up the tree from w. y = child of z with higher height (y  ancestor of w) x = child of y with higher height, or either child if two children of y have the same height. Perform operation restructure(x) to restore balance at the subtree rooted at z. As this restructuring may upset the balance of another node higher in the tree, we must continue checking for balance until the root of T is reached.

21 Removal Example Whew, balanced! Choose either 78 or 50 as node x.

22 Removal Example (2) Whew, balanced! Oh no, unbalanced! Choose 50 as x.

Next time … Heaps (8.3) 23