Height Balanced Trees CPSC 335 Dr. Marina Gavrilova Computer Science

Slides:



Advertisements
Similar presentations
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.
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
CS202 - Fundamental Structures of Computer Science II
Balanced Search Trees. 2-3 Trees Trees Red-Black Trees AVL Trees.
CPSC 335 Height Balanced Trees Dr. Marina Gavrilova Computer Science University of Calgary Canada.
CS Data Structures Chapter 10 Search Structures (Selected Topics)
Tirgul 5 AVL trees.
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.
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!
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.
CS Data Structures Chapter 10 Search Structures.
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.
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.
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.
CSE332: Data Abstractions Lecture 7: AVL Trees
Lecture 23 Red Black Tree Chapter 10 of textbook
AVL Trees CSE, POSTECH.
AA Trees.
Balanced Binary Search Trees
Data Structures – LECTURE Balanced trees
Balancing Binary Search Trees
Red Black Trees
CS202 - Fundamental Structures of Computer Science II
Lecture 17 Red-Black Trees
CS202 - Fundamental Structures of Computer Science II
Balanced Binary Search Trees
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.
CS 201 Data Structures and Algorithms
Chapter 29 AVL Trees.
Balanced Trees AVL : Adelson-Velskii and Landis(1962)
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
Red-Black Trees Motivations
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
AVL Trees 11/10/2018 AVL Trees v z AVL Trees.
AVL Tree A Balanced Binary Search Tree
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
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.
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
Copyright © Aiman Hanna All rights reserved
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 Trees CSE 373 Data Structures.
CSE 332: Data Abstractions AVL Trees
AVL Search Tree put(9)
Advanced Implementation of Tables
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.
CSE 373 Data Structures Lecture 8
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

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

General Height Balanced Trees AVL Trees IPR trees Summary Outline General Height Balanced Trees AVL Trees IPR trees Summary

Height Balanced Trees 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.

AVL 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.

Balance Factor 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.

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

Inserting a New Node 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.

Pivot 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. The mechanism of restoring balance to an AVL tree is called rotation.

Restoring Balance – Tree 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

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.

LL (RR) Imbalance 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 (RL) 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.

AVL Trees and Golden ratio The golden ratio is an irrational number φ =(1+√5)/2= 2cos(Pi/5) ∼= 1.618 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

AVL Trees and Golden ratio 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). Image Pattern Recognition, M. Gavrilova World Scietific cover (left) Leonardo Da Vinci's illustration from De Divina Proportione (right)

Efficiency of AVL Trees 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(log2 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).

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)

IPR Tree rotation

IPR vs AVL comparison 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

Rotations are used as a typical mechanism to restore balance 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