Balanced Trees AVL : Adelson-Velskii and Landis(1962)

Slides:



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

AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
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.
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:
AVL Trees Balanced Trees. AVL Tree Property A Binary search tree is an AVL tree if : –the height of the left subtree and the height of the right subtree.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Splay trees CS 202 – Fundamental Structures of Computer Science II.
Splay Trees Splay trees are binary search trees (BSTs) that:
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
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.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
Oct 26, 2001CSE 373, Autumn A Forest of Trees Binary search trees: simple. –good on average: O(log n) –bad in the worst case: O(n) AVL trees: more.
Data Structures: A Pseudocode Approach with C, Second Edition1 Objectives Upon completion you will be able to: Explain the differences between a BST and.
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.
AVL Tree.
CS 5243: Algorithms Balanced Trees AVL : Adelson-Velskii and Landis(1962)
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 Trees CSE, POSTECH.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AA Trees.
Balanced Binary Search Trees
Balanced Search Trees Modified from authors’ slides.
Lecture Efficient Binary Trees Chapter 10 of textbook
Red-Black Tree Neil Tang 02/04/2010
Week 7 - Friday CS221.
Balancing Binary Search Trees
CSIT 402 Data Structures II
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Balanced Binary Search Trees
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.
Splay Trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Balanced Trees (AVL and RedBlack)
Red Black Trees.
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
Lecture 25 Splay Tree Chapter 10 of textbook
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
AVL Trees 11/10/2018 AVL Trees v z AVL Trees.
TCSS 342, Winter 2006 Lecture Notes
AVL Trees CENG 213 Data Structures.
CS202 - Fundamental Structures of Computer Science II
Trees Chapter 15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Instructor: Lilian de Greef Quarter: Summer 2017
CS202 - Fundamental Structures of Computer Science II
Tree Rotations & Splay Trees
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
SPLAY TREES.
Data Structures & Algorithms
Balanced BSTs "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust CLRS, pages 333, 337.
Binary Search Trees < > =
CMSC 341 Splay Trees.
AVL Trees CSE 373 Data Structures.
Lecture No.20 Data Structures Dr. Sohail Aslam
CMSC 341 Splay Trees.
Self-Balancing Search Trees
CS202 - Fundamental Structures of Computer Science II
AVL-Trees (Part 1).
CMSC 341 Splay Trees.
CSE 373 Data Structures Lecture 8
Tree Balancing: AVL Trees
CMSC 341 Splay Trees.
326 Lecture 9 Henry Kautz Winter Quarter 2002
CS202 - Fundamental Structures of Computer Science II
Splay Trees Binary search trees.
CS202 - Fundamental Structures of Computer Science II
Self-Balancing Search Trees
Presentation transcript:

Balanced Trees AVL : Adelson-Velskii and Landis(1962) CS 3013: Algorithms Balanced Trees AVL : Adelson-Velskii and Landis(1962)

Balanced Trees Balanced trees are those that are basically kept in a shape that allows search time to be log N either worst case or amortized over many searches. They may be weight balanced, height balanced , IPL influenced etc. They come in many flavors AVL trees (height) Red Black trees (height) Splay trees (amortized)

STL The implementation of choice for Set and Map in the standard template library is often the Red-Black tree. These are trees that have nodes defined as either red or black. These colored nodes are used to keep balance via an interesting algorithm. There is considerable discussion on AVL vrs RB see https://benpfaff.org/papers/libavl.pdf

Lets look at AVL Trees An AVL tree is a binary tree that is height-balanced where the difference in height between the left and right subtrees at any point in the tree is restricted. Define the balance factor of node x to be the height of x’s left subtree minus the height of it right subtree An AVL tree is a BST in which the balance factor of each node is 0, -1, or 1

Sample Trees -1 1 1 1 1 -1 2 2 -2 1 Not AVL trees 1

Keeping AVL trees balanced When a new item inserted into a balanced binary tree resulting tree may become unbalanced Tree can be rebalanced transform subtree rooted at the node that is the nearest ancestor of the new node unacceptable balance factor This transformation carried out by rotation, the relative order of nodes in a subtree is shifted, changing the root, and the number of nodes in the left and right subtrees Four standard types of rotations are performed on AVL trees:

AVL Inserting Do a normal BST insert If the tree is balanced, you are finished. Otherwise do 3-4 Starting at the newly inserted node, work your way up the tree until you find the first node whose subtrees’ heights differ by 2. This node is the pivot, the root of its taller subtree is the rotator. It is necessarily the case that the new value was inserted in one of the rotator’s subtrees. If it was inserted in the rotators’s outside subtree, then this subtree will be taller than inside, and a single rotation(of the rotator) will correct the imbalance. Otherwise you must first rotate the root of the rotator’s inside subtree up into the rotator’s position and then rotate the node that replaced the rotator up into the pivot’s position. This is known as a double rotation.

Keeping AVL trees balanced Simple right rotation: When new item inserted in the left subtree of the left child B of the nearest ancestor A with balance factor +2 Simple left rotation: When new item inserted in the right subtree of the right child B of the nearest ancestor A with balance factor –2: 2 P 1 D D R P D 2 P 1 D R R

Keeping AVL trees balanced 2 P 3. Left-right rotation:When new item inserted in the right subtree of the left child B of the nearest ancestor A with balance factor of +2 4. Right-left rotation: When new item inserted in the left subtree of the right child B of the nearest ancestor A with balance factor -2 -1 G R D H 1 M Left-right H G P D M R

Keeping AVL trees balanced Each of these rotations performed by simply resetting some links For example, consider a simple right rotation, used when item inserted in the left subtree of the left child B of the nearest ancestor A with balance factor +2 A simple right rotation involves resetting three links. Reset the link from the parent of A to B Set the left link of A equal to the right link of B Set the right link of B to point to A

Insert Example Insert a 2 9 9 pivot 5 10 5 10 rotator 6 11 6 11 -1 1 6 11 -1 1 -1 1 2

AVL Left Rotate 9 9 pivot pivot 5 10 5 10 6 11 6 11 1 rotator -1 1 2 2 6 11 1 rotator -1 1 2 2 -1

AVL Right Rotate 9 9 Right rotate 1 10 5 10 5 11 6 11 1 -1 2 6 2 -1

Single Rotation Left /** * Rotate binary tree node with right child. i.e. Left rotate */ void SplayTree::rotateWithRightChild( BinaryNode * & k1 ) const { BinaryNode *k2 = k1->right; k1->right = k2->left; k2->left = k1; k1 = k2; } 2 k1 1 k2 1 A 2 C C B B A

Another look at Double Rotation Left-right double 3 2 1 D 1 3 2 A C A B D C B

AVL Rebalancing cost/benefit Balanced binary trees with n nodes will have a depth O(log2n) AVL trees thus guarantee search time will be O(log2n) Overhead involved in rebalancing as the AVL tree justified when search operations exceed insertions. Faster searches compensate for slower insertions. Empirical studies indicate that on the average, rebalancing is required for approximately 45 percent of the insertions. Roughly one-half of these rotations require a double rotation.

Splay Trees A Splay Tree is also a BST Tree that is modified via a set of special rotations. This structure guarantees that any M consecutive tree operations starting from an empty tree take at most O(M log N) time. We call this an amortized running time. Any single operation may take O(N) time although these occur infrequently. The basic idea of the splay tree is that after a node is accessed, it is pushed to the root by a series of AVL type rotations. Frequently accessed nodes are always near the top of the tree. "Self-adjusting Binary Search Trees", Sleator and Tarjan, JACM Volume 32, No 3, July 1985, pp 652-686.

Zig Rotation P X P X C A A B B C Same as AVL single rotation

Zig-Zag Rotation G X P D P G X A C A B D C B Same as AVL double rotation

Zig-Zig Rotation G X P D P A X G C B A B C D

Splaying at node 1 7 7 7 1 6 6 6 6 5 5 1 4 7 4 2 5 4 4 2 5 3 3 1 3 2 2 3 1