CPSC 335 Height Balanced Trees Dr. Marina Gavrilova Computer Science University of Calgary Canada.

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

Chapter 4: Trees Part II - AVL Tree
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 Balancing. The AVL Tree An AVL tree is a balanced binary search tree. What does it mean for a tree to be balanced? It means that for every node.
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
Balanced Search Trees. 2-3 Trees Trees Red-Black Trees AVL Trees.
Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.
CS Data Structures Chapter 10 Search Structures (Selected Topics)
CSE332: Data Abstractions Lecture 7: AVL Trees Dan Grossman Spring 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:
Tirgul 5 AVL trees.
Advanced Tree Data Structures Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Dynamic Set ADT Balanced Trees Gerda Kamberova Department of Computer Science Hofstra 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.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
AVL trees. AVL Trees We have seen that all operations depend on the depth of the tree. We don’t want trees with nodes which have large height This can.
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!
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
CPSC 335 BTrees Dr. Marina Gavrilova Computer Science University of Calgary Canada.
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.
Balanced Search Trees Chapter 27 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Balanced Search Trees Chapter Chapter Contents AVL Trees Single Rotations Double Rotations Implementation Details 2-3 Trees Searching Adding Entries.
CS Data Structures Chapter 10 Search Structures.
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.
Red Black Tree Smt Genap Outline Red-Black Trees ◦ Motivation ◦ Definition ◦ Operation Smt Genap
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
Copyright Curt Hill Balance in Binary Trees Impact on Performance.
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.
Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.
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.
Data Structures AVL Trees.
Balanced Search Trees Chapter 19 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 20 AVL Trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
CS 5243: Algorithms Balanced Trees AVL : Adelson-Velskii and Landis(1962)
AVL TREES By Asami Enomoto CS 146 AVL Tree is… named after Adelson-Velskii and Landis the first dynamically balanced trees to be propose Binary search.
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.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
Lecture 23 Red Black Tree Chapter 10 of textbook
AVL Trees CSE, POSTECH.
Data Structures – LECTURE Balanced trees
CS202 - Fundamental Structures of Computer Science II
Lecture 17 Red-Black Trees
CS202 - Fundamental Structures of Computer Science II
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.
AVL Trees A BST in which, for any node, the number of levels in its two subtrees differ by at most 1 The height of an empty tree is -1. If this relationship.
Chapter 29 AVL Trees.
Height Balanced Trees CPSC 335 Dr. Marina Gavrilova Computer Science
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
Advanced Associative Structures
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
CS202 - Fundamental Structures of Computer Science II
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Data Structures & Algorithms
AVL Search Tree put(9)
CS202 - Fundamental Structures of Computer Science II
Red-black tree properties
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

CPSC 335 Height Balanced Trees Dr. Marina Gavrilova Computer Science University of Calgary Canada

2 Outline General Height Balanced Trees AVL Trees IPR trees Summary

 A height balanced tree is one in which the difference in the height of the two subtrees for any node is less than or equal to some specified amount. One type of height balanced tree is the AVL tree named after its originators (Adelson-Velskii & Landis). In this tree the height difference may be no more than 1. Height Balanced Trees

In the AVL trees, searches always stay close to the theoretical minimum of O(log n) and never degenerate to O(n). The tradeoff for search efficiency is increased time and complexity for insertion and deletion from these trees since each time the tree is changed it may go out of balance and have to be rebalanced. AVL Trees

 To implement an AVL tree each node must contain a balance factor which indicates its state of balance relative to its subtrees. If balance is determined by (height left tree) - (height right tree) then the balance factors in a balanced tree can only have values of -1, 0, or 1. Balance Factor

AVL tree with 1 to 4 nodes A larger AVL tree 6 AVL tree examples

 Adding a new node can change the balance factor of any node by at most 1.  If a node currently has a balance factor of 0 it cannot go out of balance.  If a node is left high (left subtree 1 level higher than right) and the insertion is in its left subtree it may go out of balance ( same applies to right/right).  If a node is left high and the insertion takes place in its right subtree then a) it cannot go out of balance b) it cannot affect the balance of nodes above it since the balance factor of higher nodes is determined by the maximum height of this node which will not change. Inserting a New Node

 If the balance factor of each node passed through on the path to an insertion is examined then it should be possible to predict which nodes could go out of balance and determine which of these nodes is closest to the insertion point. This node will be called the pivot node.  Restoring the balance of the pivot node restores the balance of the whole sub-tree and potentially all of the nodes that were affected by the insertion. Pivot Node  The mechanism of restoring balance to an AVL tree is called rotation.

Restoring Balance – Tree Rotation

 When an imbalance occurs, there are two possible situations: 1. The root of the high subtree is out of balance in the same direction as its parent. This is called a Left-Left or Right-Right imbalance since the insertion took place in the left/(right) subtree of a node whose parent (the pivot) was left/(right) high. 2. The root of the high subtree is out of balance in the opposite direction to its parent. This is called a Left-Right or Right-Left imbalance since the insertion took place in the right /(left) subtree of a node whose parent (the pivot) was left/(right) high. Restoring Balance – Tree Rotation

 Correction of the LL or RR imbalance requires only a single rotation about the pivot node but the second case, LR or RL requires a prior rotation about the root of the affected subtree then a rotation about the pivot node. The first of the double rotations does not affect the degree of imbalance but converts it to a simple LL or RR.  A rotation consists of moving the child of the high subtree into the position occupied by the pivot, and moving the pivot down into the low subtree. Tree Rotation

 To correct an LL imbalance the high child replaces the pivot and the pivot moves down to become the root of this child ’ s right subtree.  The original right child of this node becomes the left child of the pivot (for RR exchange left and right in the description). LL (RR) Imbalance

 To correct an LR imbalance a rotation is first performed about the left child of the pivot as if it were the pivot for a RR imbalance.  This rotation effectively converts the imbalance of the original pivot to a LL which can now be corrected as above. LL (RL) Imbalance

The golden ratio is an irrational number φ =(1+√5)/2= 2cos(Pi/5) ∼ = Properties: φ turns up in many geometric figures including pentagrams and dodecahedra φ − 1 = 1/φ It is the ratio, in the limit, of successive members of the Fibonacci sequence 14 AVL Trees and Golden ratio

15 This figure is a regular pentagon with an inscribed pentagram. All the line segments found there are equal in length to one of the five line segments described below: The length of the black line segment is 1 unit. The length of the red line seqment, a, is Ø. The length of the yellow line seqment, b, is 1/Ø. The length of the green line seqment, c, is 1, like the black segment. The length of the blue line seqment, d, is (1/Ø) 2, or equivalently, 1 - (1/Ø), as can be seen from examining the figure. (Note that b + d = 1). AVL Trees and Golden ratio Image Pattern Recognition, M. Gavrilova World Scietific cover (left) Leonardo Da Vinci's illustration from De Divina Proportione (right)

Golden ratio is used to prove that in AVL tree the height will never be greater than 1.44log(n+2).  Time complexity of restoring balance O(log 2 n) {in spite of the overhead of calculating balance factors, determining imbalance and correcting it}  Height balancing for a single node is still O(1). Efficiency of AVL Trees

IPR Trees  Internal path Reduction tree IPR (Gaston Gonnet, 1983) is another type of balanced tree.  It uses as balancing factor not a difference between the left and right subtrees, but the sum of path lengths from the root to the leaves in the subtrees.  Root length is 1, Internal path is sum of depths of all nodes in the subtree

IPR Tree rotation  Four rotation cases are similar to AVL trees:  MR: more nodes to the right and can be restored through single or double rotation (Case a and b on your handout)  ML: more nodes to the left and can be restored through single or double rotation (Case c and d on your handout)

19 IPR Tree rotation

 Rotations to restore balance can be multiple, thus this operation is longer than in AVL tree  Search for a key is faster on average as the tree minimized the combined length to reach the node 20 IPR vs AVL comparison

Summary  Balanced trees demonstrate superior performance to binary search trees due to log (n) tree depth  Rotations are used as a typical mechanism to restore balance  Balanced trees are used in lookup- intensive applications