Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.

Slides:



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

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.
Binary Search Trees Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 19 (continued) © 2002 Addison Wesley.
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Trees Types and Operations
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
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.
CS202 - Fundamental Structures of Computer Science II
Tree Balancing: AVL Trees Dr. Yingwu Zhu. Recall in BST The insertion order of items determine the shape of BST Balanced: search T(n)=O(logN) Unbalanced:
CSE332: Data Abstractions Lecture 7: AVL Trees Dan Grossman Spring 2010.
Data Structures Lecture 11 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
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:
TCSS 342 AVL Trees v1.01 AVL Trees Motivation: we want to guarantee O(log n) running time on the find/insert/remove operations. Idea: keep the tree balanced.
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
Chapter 6: Transform and Conquer Trees, Red-Black Trees The Design and Analysis of Algorithms.
AVL-Trees (Part 1: Single Rotations) Lecture COMP171 Fall 2006.
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.
CSE 326: Data Structures AVL Trees
Trees and Red-Black Trees Gordon College Prof. Brinton.
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
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.
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.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
Properties of BST delete We first do the normal BST deletion: – 0 children: just delete it – 1 child: delete it, connect child to parent – 2 children:
CSE373: Data Structures & Algorithms Optional Slides: AVL Delete Dan Grossman Fall 2013.
INTRODUCTION TO AVL TREES P. 839 – 854. INTRO  Review of Binary Trees: –Binary Trees are useful for quick retrieval of items stored in the tree –order.
1 AVL-Trees: Motivation Recall our discussion on BSTs –The height of a BST depends on the order of insertion E.g., Insert keys 1, 2, 3, 4, 5, 6, 7 into.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
Binary SearchTrees [CLRS] – Chap 12. What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following.
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.
CS 253: Algorithms Chapter 13 Balanced Binary Search Trees (Balanced BST) AVL Trees.
Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.
AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.
Data Structures AVL 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.
AVL trees1 AVL Trees Height of a node : The height of a leaf is 1. The height of a null pointer is zero. The height of an internal node is the maximum.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
AVL Trees 1. Balancing a BST Goal – Keep the height small – For any node, left and right sub-tree have approximately the same height Ensures fast (O(lgn))
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Search Trees.
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Balancing Binary Search Trees
UNIT III TREES.
CSIT 402 Data Structures II
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
AVL Search Trees Introduction What is an AVL Tree?
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
AVL Tree 27th Mar 2007.
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
AVL Tree A Balanced Binary Search Tree
AVL Trees CENG 213 Data Structures.
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
AVL Trees CSE 373 Data Structures.
CS202 - Fundamental Structures of Computer Science II
Binary SearchTrees [CLRS] – Chap 12.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
ITCS6114 Algorithms and Data Structures
Richard Anderson Spring 2016
Red-black tree properties
CSE 373 Data Structures Lecture 8
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

Balancing Binary Search Trees

Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes in its left subtree and the number of nodes in its right subtree is at most one Example: Balanced tree vs Not balanced tree

Balancing Binary Search Trees Inserting or deleting a node from a (balanced) binary search tree can lead to an unbalance In this case, we perform some operations to rearrange the binary search tree in a balanced form –These operations must be easy to perform and must require only a minimum number of links to be reassigned –Such kind of operations are Rotations

Tree Rotations The basic tree-restructuring operation There are left rotation and right rotation. They are inverses of each other [CLRS Fig. 13.1]

Tree Rotations Changes the local pointer structure. (Only pointers are changed.) A rotation operation preserves the binary- search-tree property: the keys in α precede x.key, which precedes the keys in β, which precede y.key, which precedes the keys in γ.

Implementing Rotations

[CLRS Fig. 13.3]

Balancing Binary Search Trees Balancing a BST is done by applying simple transformations such as rotations to fix up after an insertion or a deletion Perfectly balanced BST are very difficult to maintain Different approximations are used for more relaxed definitions of “balanced”, for example: –AVL trees –Red-black trees

AVL trees Adelson Velskii and Landes An AVL tree is a binary search tree that is height balanced: for each node x, the heights of the left and right subtrees of x differ by at most 1. AVL Tree vs Non-AVL Tree

AVL Trees AVL trees are height-balanced binary search trees Balance factor of a node –height(left subtree) - height(right subtree) An AVL tree has balance factor calculated at every node –For every node, heights of left and right subtree can differ by no more than 1

Height of an AVL Tree How many nodes are there in an AVL tree of height h ? N(h) = minimum number of nodes in an AVL tree of height h. Base Case: –N(0) = 1, N(1) = 2 Induction Step: –N(h) = N(h-1) + N(h-2) + 1 Solution: –N(h) =  h (   1.62) h-1 h-2 h

Height of an AVL Tree N(h)=  h (   1.62) What is the height of an AVL Tree with n nodes ? Suppose we have n nodes in an AVL tree of height h. –n > N(h) (because N(h) was the minimum) –n >  h hence log  n > h –h < 1.44 log 2 n ( h is O(logn))

Insertion into an AVL trees 1.place a node into the appropriate place in binary search tree order 2.examine height balancing on insertion path: 1.Tree was balanced (balance=0) => increasing the height of a subtree will be in the tolerated interval +/-1 2.Tree was not balanced, with a factor +/-1, and the node is inserted in the smaller subtree leading to its height increase => the tree will be balanced after insertion 3.Tree was balanced, with a factor +/-1, and the node is inserted in the taller subtree leading to its height increase => the tree is no longer height balanced (the heights of the left and right children of some node x might differ by 2) we have to balance the subtree rooted at x using rotations How to rotate ? => see 4 cases according to the path to the new node

Example – AVL insertions Case 1: Node’s Left – Left grandchild is too tall RIGHT-ROTATE

AVL insertions – Right Rotation h h h x y h h h x y Balance: 1 Balance: 2 Balance: 0 Case 1: Node’s Left – Left grandchild is too tall h+2 Height of tree after balancing is the same as before insertion !

Example – AVL insertions Case 2: Node’s Left-Right grandchild is too tall Solution: do a Double Rotation: LEFT-ROTATE and RIGHT-ROTATE

Double Rotation – Case Left-Right x z y h h-1 Balance: -1 Balance: 2 Case 2: Node’s Left-Right grandchild is too tall h+2

Double Rotation – Case Left-Right h z x y h h-1 Balance: 0 or 1 Balance: 0 or -1 Balance: 0 h+2 Height of tree after balancing is the same as before insertion ! => there are NO upward propagations of the unbalance !

Example – AVL insertions Case 3: Node’s Right – Right grandchild is too tall LEFT-ROTATE

AVL insertions – Left Rotation h h h x y h h h x y Balance: -1 Balance: -2 Balance: 0 Case 3: Node’s Right – Right grandchild is too tall

Example – AVL insertions Case 4: Node’s Right – Left grandchild is too tall Solution: do a Double Rotation: RIGHT-ROTATE and LEFT-ROTATE

Double Rotation – Case Right-Left h z x y h h-1 Balance: 1 or -1 Balance: 1 Balance: -2 Case 4: Node’s Right – Left grandchild is too tall

Double Rotation – Case Right-Left h z x y h h-1 Balance: 0 or 1 Balance: -1 or 0 Balance: 0

Implementing AVL Trees Insertion needs information about the height of each node It would be highly inefficient to calculate the height of a node every time this information is needed => the tree structure is augmented with height information that is maintained during all operations An AVL Node contains the attributes: –Key –Left, right, p –Height

Case 2 – Left-Right Case 1 – Left-Left Case 4 – Right-Left Case 3 – Right-Right <

Analysis of AVL-INSERT Insertion makes O(h) steps, h is O(log n), thus Insertion makes O(log n) steps At every insertion step, there is a call to Balance, but rotations will be performed only once for the insertion of a key. It is not possible that after doing a balancing, unbalances are propagated, because the BALANCE operation restores the height of the subtree before insertion. => number of rotations for one insertion is O(1) AVL-INSERT is O(log n)

AVL Delete The procedure of BST deletion of a node z: –1 child: delete it, connect child to parent –2 children: put successor in place of z, delete successor Which nodes’ heights may have changed: –1 child: path from deleted node to root –2 children: path from deleted successor leaf to root AVL Tree may need rebalancing as we return along the deletion path back to the root

Exercise Insert following keys into an initially empty AVL tree. Indicate the rotation cases: 14, 17, 11, 7,, 3, 14, 12, 9

AVL delete – Right Rotation h-1 y x Balance: 1 Balance: 2 Case 1: Node’s Left-Left grandchild is too tall h-1 h y x Balance: 0 h-1 Delete node in right child, the height of the right child decreases The height of tree after balancing decreases !=> Unbalance may propagate h h+2 h+1

AVL delete – Double Rotation Case 2: Node’s Left-Right grandchild is too tall Delete node in right child, the height of the right child decreases x y Balance: 0 h-1 z The height of tree after balancing decreases !=> Unbalance may propagate h+2 h+1 z x Balance: 1 Balance: 2 h-1 y

AVL delete – Left Rotation h-1 h x y Balance: -1 Balance: -2 Case 3: Node’s Right – Right grandchild is too tall h-1 h x y Balance: 0 h-1 Delete node in left child, the height of the left child decreases The height of tree after balancing decreases !=> Unbalance may propagate h+2 h+1

AVL delete – Double Rotation h-1 x z Balance: 1 Balance: -2 Case 4: Node’s Right – Left grandchild is too tall h-1 Delete node in left child, the height of the left child decreases y h-1 x y Balance: 0 h-1 z The height of tree after balancing decreases !=> Unbalance may propagate h+2 h+1

Analysis of AVL-DELETE Deletion makes O(h) steps, h is O(log n), thus deletion makes O(log n) steps At the deletion of a node, rotations may be performed for all the nodes of the deletion path which is O(h)=O(log n) ! In the worst case, it is possible that after doing a balancing, unbalances are propagated on the whole path to the root !

Exercise What happens if key 12 is deleted ?

AVL Trees - Summary AVL definition of balance: for each node x, the heights of the left and right subtrees of x differ by at most 1. Maximum height of an AVL tree with n nodes is h < 1.44 log 2 n AVL-Insert: O(log n), Rotations: O(1) (For Insert, unbalances are not propagated after they are solved once) AVL-Delete: O(log n), Rotations: O(log n) (For Delete, unbalances may be propagated up to the root)

AVL vs Simple BST AVLSimple BST Max Height1.44 log n n INSERTO(log n)O(log(n) Rotations at InsertO(1) DELETEO(log n) Rotations at Delete O(log n)

Conclusions - Binary Search Trees BST are well suited to implement Dictionary and Dynamic Sets structures (Insert, Delete, Search) In order to keep their height small, balancing techniques can be applied –AVL is one of these techniques