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.

Slides:



Advertisements
Similar presentations
Rizwan Rehman Centre for Computer Studies Dibrugarh University
Advertisements

Lecture 9 : Balanced Search Trees Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
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.
Rotating Nodes How to balance a node that has become unbalanced after the addition of one new node.
1 AVL Trees (10.2) CSE 2011 Winter April 2015.
AVL Tree Rotations Daniel Box. Binary Search Trees A binary search tree is a tree created so that all of the items in the left subtree of a node are less.
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 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.
Trees Types and Operations
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
1 CSE 373 AVL trees, continued read: Weiss Ch. 4, section slides created by Marty Stepp
One more definition: A binary tree, T, is balanced if T is empty, or if abs ( height (leftsubtree of T) - height ( right subtree of T) )
Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees.
CS202 - Fundamental Structures of Computer Science II
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
CPSC 335 Height Balanced Trees Dr. Marina Gavrilova Computer Science University of Calgary Canada.
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
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.
Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a binary search tree abstract.
CSC 2300 Data Structures & Algorithms February 13, 2007 Chapter 4. Trees.
Balanced Trees. Binary Search tree with a balance condition Why? For every node in the tree, the height of its left and right subtrees must differ by.
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 Optional Slides: AVL Delete Dan Grossman Fall 2013.
Balanced Search Trees Chapter 27 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower.
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.
Chapter 7 Trees_Part3 1 SEARCH TREE. Search Trees 2  Two standard search trees:  Binary Search Trees (non-balanced) All items in left sub-tree are less.
CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search.
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.
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.
Keeping Binary Trees Sorted. Search trees Searching a binary tree is easy; it’s just a preorder traversal public BinaryTree findNode(BinaryTree node,
AVL Trees. AVL Tree In computer science, an AVL tree is the first-invented self-balancing binary search tree. In an AVL tree the heights of the two child.
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.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
AA Trees.
CS202 - Fundamental Structures of Computer Science II
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.
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
AVL Tree 27th Mar 2007.
Tonga Institute of Higher Education
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
AVL Tree A Balanced Binary Search Tree
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
TCSS 342, Winter 2006 Lecture Notes
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
Red-Black Trees 2018年11月26日3时46分 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.
CSE 373: Data Structures and Algorithms
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
Lecture 9: Self Balancing Trees
Data Structures Lecture 21 Sohail Aslam.
ITCS6114 Algorithms and Data Structures
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Self-Balancing Search Trees
Presentation transcript:

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 in the tree, the height of its children differs by no more than 1. The white number below each node represents the height of that node in the tree. The tree below is currently balanced. The height is computed in the following manner: A NULL child has a height of -1 A leaf node has a height of 0 The height of each node is equal to the maximum of the heights of its children plus

Correcting Imbalances Adding to and removing from an AVL tree can create imbalances between nodes. Correcting these imbalances is what makes an AVL tree more efficient. These corrections are often called rotations in that they appear to rotate a parent downward and a child upward. In the example below, node 1 is unbalanced. By “rotating” nodes 1 and 3 counter-clockwise, or by bringing node 1 downward and node 3 upward, the tree becomes balanced in all nodes. Before rebalancingAfter rebalancing

Unbalanced Trees Adding to and removing from an AVL tree can result in four kinds of unbalanced trees. You can determine the kind of rebalancing you must use by following along the path of the heavier child. Consider the first case. Node 0’s two children have heights -1 and 1, so we move right to node 1 (height 1). Node 1’s children have heights -1 and 0, so we move right to node 2 (height 0), giving us a right – right imbalance. Right – Right Right – Left Left – Left Left – Right Each of the four imbalances will rebalance to the tree

Right – Right Imbalance In the example below, node 5 has been added to the tree. This results in a Right – Right imbalance in node 1 (its children have heights 0 and 2, which differ by more than 1; its right child, 3, is heavier on the right). Note that the only connections that change are highlighted in red. Also note that node 3 is the new root of the subtree. A Right – Right imbalance and a Left – Left imbalance are resolved in a similar manner. BeforeAfter adding 5After rebalancing

Right – Left Imbalance A Right – Left imbalance is resolved in two rebalancings. The first rebalance shifts the tree into a Right – Right imbalance, and the second rebalance resolves the Right – Right imbalance as demonstrated before. In the example below, node 0 is removed from the tree. The first rebalance is shown in green lines; the second rebalance is shown in blue lines. BeforeRebalance 1 Rebalance 2 3 After removing

Default to Right – Right In some instances, removing a node can result in an imbalance that could be resolved using either Right – Right or Right – Left rebalancing. In this case, you should default to Right – Right. In the example below, node 3 is not balanced after removing node 2. Since node 7’s children both have a height of 1, neither node is heavier than the other. Both Right – Right rebalancing and Right – Left rebalancing are valid options, but the test driver will only accept a Right – Right rebalance. Note that Left – Left rebalance should be used over Left – Right rebalance. BeforeAfter removing 2After rebalancing

Multiple Imbalances Sometimes, solving an imbalance can result in another imbalance further up the tree. In the example below, node 10 is no longer balanced after removing node 5. After rebalancing node 10, node 20 is unbalanced. Thus, it is important to check for imbalances all the way back to the root after each add and remove. BeforeAfter balancing 10 After balancing 20After removing