Copyright 2004-2006 Curt Hill Balance in Binary Trees Impact on Performance.

Slides:



Advertisements
Similar presentations
AVL Trees binary tree for every node x, define its balance factor
Advertisements

1 Lecture 12 AVL Trees. 2 trees static dynamic game treessearch trees priority queues and heaps graphs binary search trees AVL trees 2-3 treestries Huffman.
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 (Adelson-Velskii & Landis, 1962) In normal search trees, the complexity of find, insert and delete operations in search trees is in the worst.
QuickSort Average Case Analysis An Incompressibility Approach Brendan Lucier August 2, 2005.
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 COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
CS261 Data Structures AVL Trees. Goals Pros/Cons of a BST AVL Solution – Height-Balanced Trees.
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture27.
Time Complexity of Basic BST Operations Search, Insert, Delete – These operations visit the nodes along a root-to- leaf path – The number of nodes encountered.
CPSC 335 Height Balanced Trees Dr. Marina Gavrilova Computer Science University of Calgary Canada.
CS Data Structures Chapter 10 Search Structures (Selected Topics)
C++ Programming:. Program Design Including
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 45 AVL Trees and Splay.
Balanced Search Trees CS 3110 Fall Some Search Structures Sorted Arrays –Advantages Search in O(log n) time (binary search) –Disadvantages Need.
1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003.
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
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.
AVL Trees CSCI 2720 Fall 2005 Kraemer. Binary Tree Issue  One major problem with the binary trees we have discussed thus far: they can become extremely.
CS Data Structures Chapter 10 Search Structures.
Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.
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.
CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.
11/7/20151 Balanced Trees Data Structures Ananda Gunawardena.
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.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 20 AVL Trees.
1 CSC TREES AVL & BALANCED TREES. 2 Balanced Trees The advantage of balanced trees is that we can perform most operation in time proportional to.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
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.
AVL Balanced Trees Introduction Data structure AVL tree balance condition AVL node level AVL rotations Choosing the rotation Performing a balanced insertion.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
CSE332: Data Abstractions Lecture 7: AVL Trees
3.1 Height-Balanced Trees 3.2 Weight-Balanced Trees
AVL Trees CSE, POSTECH.
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Data Structures – LECTURE Balanced trees
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Balanced 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.
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.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Height Balanced Trees CPSC 335 Dr. Marina Gavrilova Computer Science
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 Trees 11/10/2018 AVL Trees v z AVL Trees.
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.
A Kind of Binary Tree Usually Stored in an Array
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
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)
Lecture No.20 Data Structures Dr. Sohail Aslam
Dynamic Dictionaries Primary Operations: Additional operations:
AVL Tree By Rajanikanth B.
Richard Anderson Spring 2016
Self-Balancing Search Trees
Presentation transcript:

Copyright Curt Hill Balance in Binary Trees Impact on Performance

Copyright Curt Hill Tree Shape and Performance A tree that is balanced has excellent performance O(log 2 N) for: –Searches –Insertions –Deletions Only a hash table can beat this performance –But it has its own issues

Copyright Curt Hill What is balance? The notion is that the two sub-trees are of about the same size Thus a search eliminates half the tree in each examination Perfect balance: –For each node in the tree, the size of the two sub-trees are off by at most one

Copyright Curt Hill Probabilities What is the likelihood that a randomly built tree will have good performance characteristics? This is a difficult question The shape of a tree is dependent on the entry order of the nodes to be inserted Example: –Consider the integers 1-7 as the items to put in a tree –There are 7! = 5040 ways to order their input 7 ways to choose first 6 ways to choose second etc.

Copyright Curt Hill What do we want? A search must look at no more than 3 nodes

Copyright Curt Hill Example Continued There are two really bad ways to choose the tree: –In ascending order or descending order –There are only two of these but there are several others that are just as bad –Consider or Bad in this case means that every node has zero or one descendents

Copyright Curt Hill What do we not want? 1 A search must look at no more than 7 nodes Arrival in ascending order Equally bad

Copyright Curt Hill Negative Combinatorics There are two ways to choose the first item –Each subsequent item provides two ways: –The next item in ascending order –The last item –Therefore 2 * 2 * 2 * 2 * 2 * 2 * 1 –Looks like 64 ways to choose a list –This is 1.27% chance of a list A search would look at no more than 7 nodes

Copyright Curt Hill Positive Combinatorics There is only one way to choose the root, it must be the 4 There are two ways to choose the second: 2 or 6 There are three ways to choose the third –If 2 was picked the 6 or any descendent of 2 –If 6 was picked the 2 or any descendent of 6 It gets exciting after that

Copyright Curt Hill Positive Combinatorics Sub-cases need to be examined of the three last choices These do not work well in this kind of presentation I believe that there are 80 out of 5040 (1.5%) permutations that yield a perfectly balance tree However, most possibilities fall somewhere in between maximum pathes of 7 and 3

Copyright Curt Hill Summary The worst case is a linked list which is bad –The worst case is not very likely The best case is perfectly balanced –The best case is more likely, but still unlikely Empirical studies indicate that the average path length of a unbalanced tree to be only 39% longer than a perfectly balanced tree Balancing is hard and slows insertions and deletions

Copyright Curt Hill When to Balance In most cases an unbalanced tree will perform quite adequately If the application fulfills the following two criteria then balancing could be considered –The data is large and the search performance impacts the program –The number of searches is large compared to insertion and deletion

Copyright Curt Hill Perfectly balanced trees Definition: –For each node the number of nodes of the left and right sub-trees differ by only 1 Balancing a tree is a recursive process that involves nodes from the leaves to root It is usually the case that control information is placed in node that measures the balance

Copyright Curt Hill Balance Again Balancing occurs in insertion and deletion, but not searches It is somewhat intricate so perfect balance is seldom used The ratio of searches to inserts and deletes must be very high Is there another definition of balance that gives good performance with less rebalancing

Copyright Curt Hill Height Balanced Also known as AVL balance –Adelson, Velski and Landis –Developed it and proved its desirability Definition: –The tree is balanced if for each node the heights of the two sub-trees differ at most by one It is the height of the tree that determines the worst case search

Copyright Curt Hill Digression on Search Consider searching an array On average the search requires ½N comparisons The worst case is N searchs to find last one or to show not found The average and worst case are quite different This is not the case for trees

Copyright Curt Hill Searching Trees More than half the nodes are leaves at maximum depth. Worst case is three probes, but average case is only slightly less than three probes.

Copyright Curt Hill AVL Trees Again Adelson, Velski and Landis proved: –Worst case of an AVL tree is only 45% worse than perfectly balanced –Average case: Insignificantly different than perfectly balanced Every perfectly balanced is also AVL balanced Far fewer rebalance, thus cheaper to construct –For the most part rebalancing occurs when really needed

Copyright Curt Hill Construction Consider the construction of the following tree Four types of rebalancing operation –RR single –LL single –LR double –RL double Add:

Copyright Curt Hill After 2 inserts 4 5 Still perfectly balanced

Copyright Curt Hill Insert Neither perfect nor AVL, rebalance is needed

Copyright Curt Hill Rotate Right Rebalance is needed – RR Single

Copyright Curt Hill After Rotate 5 7 After rebalance 4

Copyright Curt Hill Insert No problem 4 2

Copyright Curt Hill Insert Unbalanced in other way – Do a LL single 4 2 1

Copyright Curt Hill Rebalance 5 7 Rebalance complete – not perfect but AVL 2 14

Copyright Curt Hill Insert A rebalance is again needed, but different

Copyright Curt Hill After Rotatation 4 5 This requires LR double

Copyright Curt Hill Insert This requires RL double

Copyright Curt Hill Rotate This requires RL double

Copyright Curt Hill Rotate Now complete

Copyright Curt Hill The problem of balancing To implement requires extra stuff in the nodes Measures the height of the descendents Even with an AVL tree there is substantial work to be done at insertion and deletion time Thus the search to insert and delete ratio needs to be high –Just not as high as perfect balance

Copyright Curt Hill Synonyms Another name for an AVL trees is Fibonacci tree The fact that heights may disagree by one leads to as strangely asymmetric tree

Copyright Curt Hill Is this balanced?