CS2420: Lecture 29 Vladimir Kulyukin Computer Science Department Utah State University.

Slides:



Advertisements
Similar presentations
CSE 373 Data Structures and Algorithms
Advertisements

Splay Trees Binary search trees.
AVL Trees binary tree for every node x, define its balance factor
AVL-Trees (Part 2: Double Rotations) Lecture 19 COMP171 Fall 2006.
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.
Binary Search Trees Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 19 (continued) © 2002 Addison Wesley.
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.
AVL Trees CS II – Fall /8/2010. Announcements HW#2 is posted – Uses AVL Trees, so you have to implement an AVL Tree class. Most of the code is provided.
1 CSE 373 AVL trees, continued read: Weiss Ch. 4, section slides created by Marty Stepp
CS202 - Fundamental Structures of Computer Science II
CPSC 335 Height Balanced Trees Dr. Marina Gavrilova Computer Science University of Calgary Canada.
4.5 AVL Trees  A tree is said to be balanced if for each node, the number of nodes in the left subtree and the number of nodes in the right subtree differ.
1 Theory I Algorithm Design and Analysis (4 – AVL trees: deletion) Prof. Th. Ottmann.
CS2420: Lecture 24 Vladimir Kulyukin Computer Science Department Utah State University.
CS2420: Lecture 19 Vladimir Kulyukin Computer Science Department Utah State University.
Advanced Tree Data Structures Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS2420: Lecture 30 Vladimir Kulyukin Computer Science Department Utah State University.
AVL-Trees (Part 1: Single Rotations) Lecture COMP171 Fall 2006.
CS2420: Lecture 28 Vladimir Kulyukin Computer Science Department Utah State University.
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
Dynamic Dictionaries Primary Operations:  Get(key) => search  Insert(key, element) => insert  Delete(key) => delete Additional operations:  Ascend()
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
CS2420: Lecture 27 Vladimir Kulyukin Computer Science Department Utah State University.
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
Fall 2007CS 2251 Self-Balancing Search Trees Chapter 9.
CS2420: Lecture 31 Vladimir Kulyukin Computer Science Department Utah State University.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
CS2420: Lecture 15 Vladimir Kulyukin Computer Science Department Utah State University.
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.
AVL Trees ITCS6114 Algorithms and Data Structures.
1 Height-Balanced Binary Search Trees AVL Trees. 2 Background zBinary Search Trees allow dynamic allocation (like linked lists), but O(log 2 (n)) average.
David Kaplan Dept of Computer Science & Engineering Autumn 2001
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.
Balanced Trees (AVL and RedBlack). Binary Search Trees Optimal Behavior ▫ O(log 2 N) – perfectly balanced tree (e.g. complete tree with all levels filled)
Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.
Beyond (2,4) Trees What do we know about (2,4)Trees? Balanced
Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.
AVL Trees. Knowing More How many nodes? – Determine on demand.
COSC 2P03 Week 51 Representation of an AVL Node class AVLNode { AVLnode left; AVLnode right; int height; int height(AVLNode T) { return T == null? -1 :
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.
Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.
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.
Red-Black Tree Insertion Start with binary search insertion, coloring the new node red NIL l Insert 18 NIL l NIL l 1315 NIL l
CSE 3358 NOTE SET 13 Data Structures and Algorithms.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
AVL Trees CSE, POSTECH.
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
Red-Black Tree Neil Tang 02/04/2010
Splay Trees Binary search trees.
AVL Trees binary tree for every node x, define its balance factor
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.
Chapter 29 AVL Trees.
Splay Trees Binary search trees.
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
AVL Trees: AVL Trees: Balanced binary search tree
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
CS223 Advanced Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
AVL Search Tree put(9)
Dynamic Dictionaries Primary Operations: Additional operations:
CS223 Advanced Data Structures and Algorithms
Lecture 9: Self Balancing Trees
AVL Tree By Rajanikanth B.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Red-black tree properties
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

CS2420: Lecture 29 Vladimir Kulyukin Computer Science Department Utah State University

Outline Balanced Binary Trees (AVL Trees) –Section 4.4.

Review: Left-Left (LL) Unbalance at A A B AR Before Insertion A B After Insertion, Before Rotation AR h BRBL BF=0 BF=1 hh BF=2 BF=1 BL BR h h+1 AR h A node is inserted into the left child of the left child of A – hence the name left-left. h+2

Review: Right-Right (RR) Unbalance at A A B BRBL AL hh BF=0 h A AL h B BL h BR h+1 Before Insertion After Insertion BF=-1 BF=-2 BF=-1 A node is inserted into the right child of the right child of A – hence right-right.

LL Fix: Single Right Rotation at A A B After Insertion, Before Rotation h BF=2 BF=1 BR h h+1 AR B A BR BL h+1h h BF=0 After Insertion and Rotation h+2

RR Fix : Single Left Rotation at A After Insertion, Before Rotation A BF= -2 AL h B BF=-1 BL h BR h+1 B BF=0 A ALBL hh BR h+1 BF=0 After Insertion and Rotation

Left-Right Unbalance (LR) h Before Insertion After Insertion AR A BF=1 BF=0 B BLBR hh A BF=2 BF= -1 B BL h CL C CR BF=X AR h A node is inserted into the right child of the left child of A – hence left-right.

LR Fix: Double LR Rotation at A After Insertion, Before Rotation A BF=2 BF= -1 B BL h CL C CR BF=X AR h A C B BLCLCRAR h h After Insertion and Rotation BF=0 BF=? h+1 Double LR Rotation consists of a single left rotation at B and a single right rotation at A.

LR Fix: Case 1 (C’s BF = 0) After Insertion, Before Rotation A BF=2 BF= -1 B BL h CL C CR BF=0 AR h A C B BLCLCRAR h h After Insertion and Rotation BF=0 h+1 hh If C’s BF=0, CL and CR are of the same height h. So, B’s BF = A’s BF = 0.

LR Fix: Case 2 (C’s BF = 1) After Insertion, Before Rotation A BF=2 BF= -1 B BL h CL C CR BF=1 AR h A C B BLCLCRAR h h After Insertion and Rotation BF=0 BF= -1 h+1 h-1 h If C’s BF=1, CL’s height is h and CR’s height is h-1. So, B’s BF = 0, A’s BF = -1.

LR Fix: Case 3 (C’s BF = -1) After Insertion, Before Rotation A BF=2 BF= -1 B BL h CL C CR BF= -1 AR h A C B BLCLCRAR h h After Insertion and Rotation BF=0 BF=1BF= 0 h+1 h h-1 h If C’s BF = -1, CL’s height is h-1 and CR’s height is h. So, B’s BF = 1, A’s BF = 0.

Double LR Rotation: Height Preservation The height of the tree rooted in A before the insertion is h+2. The height of the tree rooted in C after the insertion and double LR rotation is also h+2. So, no further rotations are needed up the insertion path.

Right Unbalance (RL) and Double RL Rotation RL Unbalance is symmetric to LR unbalance. Double RL rotation is symmetric to double LR rotation: whenever we rotate left in LR, we rotate right in RL; whenever we have to rotate right in LR, we have to rotate left in RL. Like the double LR rotation, the double RL rotation preserves the height.

Right-Left (RL) Unbalance at A A B BRBL AL hh BF=0 h A AL h B BL h h+1 Before Insertion After Insertion BF=-1 BF=-2 BF=1 BR h

RL Fix: Double RL Rotation at A A AL h B h+1 After Insertion, Before Rotation BF=-2 BF=1 BR h C CLCR BF=X C B BRCR A ALCL After Insertion and Rotation h h BF=0 BF=?

RL Fix: Computing BF’s The computation of BF’s in the RL rotation follows the same three cases as with the LR rotation summarized in the table below. Before RotationAfter Rotation C’s BF = 0; CL’s height = h; CR’s height = h. A’s BF = 0; B’s BF = 0. C’s BF = 1; CL’s height = h; CR’s height = h-1. A’s BF = 0; B’s BF = -1. C’s BF = -1, CL’s height = h-1; CR’s height = h. A’s BF = 1; B’s BF = 0.

AVL Search Tree: Implementation The AVL Tree Node data type should have the following data members: –key –data –parent –left child –right child –balance factor –height

AVL Search Tree: Implementation The insertion algorithm should insert the node into the tree in the same way as was done with the binary search tree. The balance factor and the height of the newly inserted node are set to 0. After the insertion is done, the algorithm starts climbing up the tree from the parent of the newly inserted node.

Fixing the AVL Search Tree After Insertion At each node up the insertion path beginning from the parent of the newly inserted node, execute the following steps: –Update the node’s height; –Update the node’s balance factor; –If the node’s balance factor is 2 or -2, determine which unbalance it is (LL, LR, RR, RL), execute the appropriate rotation and return; –If the node’s balance factor is 1, 0, or -1, get its parent and repeat the above steps; The algorithm terminates either after a rotation is executed or the null node is reached (the null node is the root’s parent).

Computing the Heights of the AVL Tree Nodes The height of the AVL tree node can be computed in constant time by looking up the heights of its children. The balance factor can also be computed in constant time as the difference b/w the left height and the right height.