Rizwan Rehman Centre for Computer Studies Dibrugarh University

Slides:



Advertisements
Similar presentations
Introduction to Algorithms
Advertisements

Chapter 13. Red-Black Trees
COSC 2007 Data Structures II Chapter 12 Advanced Implementation of Tables II.
© 2001 by Charles E. Leiserson Introduction to AlgorithmsDay 18 L10.1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 10 Prof. Erik Demaine.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
CS16: Introduction to Data Structures & Algorithms
CSE 373 Data Structures and Algorithms
David Luebke 1 8/25/2014 CS 332: Algorithms Red-Black Trees.
Definitions and Bottom-Up Insertion
Introduction to Algorithms Red-Black Trees
25 seconds left…...
Chapter 12 Binary Search Trees
CSE Lecture 17 – Balanced trees
Foundations of Data Structures Practical Session #7 AVL Trees 2.
Part II. Delete an Node from an AVL Tree Consider to delete
AVL-Trees (Part 2: Double Rotations) Lecture 19 COMP171 Fall 2006.
AVL-Trees (Part 2) COMP171. AVL Trees / Slide 2 A warm-up exercise … * Create a BST from a sequence, n A, B, C, D, E, F, G, H * Create a AVL tree for.
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 (10.2) CSE 2011 Winter April 2015.
Chapter 4: Trees Part II - AVL Tree
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.
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.
Balanced Binary Search Trees
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:
AVL Trees / Slide 1 Delete Single rotation Deletion.
Chapter 6: Transform and Conquer Trees, Red-Black Trees The Design and Analysis of Algorithms.
AVL-Trees (Part 1: Single Rotations) Lecture COMP171 Fall 2006.
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 /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.
AVL Trees / Slide 1 Balanced Binary Search Tree  Worst case height of binary search tree: N-1  Insertion, deletion can be O(N) in the worst case  We.
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.
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.
B + -Trees COMP171 Fall AVL Trees / Slide 2 Dictionary for Secondary storage * The AVL tree is an excellent dictionary structure when the entire.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
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.
Chapter 10: Search Trees Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data.
AVL Trees / Slide 1 Height-balanced trees AVL trees height is no more than 2 log 2 n (n is the number of nodes) Proof based on a recurrence formula for.
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.
Red-Black Trees an 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.
CSC317 1 x y γ β α x y γ β x β What did we leave untouched? α y x β.
Lecture 23 Red Black Tree Chapter 10 of textbook
Part-D1 Binary Search Trees
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
BCA-II Data Structure Using C
G64ADS Advanced Data Structures
AVL Trees 6/25/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Balancing Binary Search Trees
Red Black 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.
AVL Tree Mohammad Asad Abbasi Lecture 12
AVL Tree.
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
Lecture 25 Splay Tree Chapter 10 of textbook
Red-Black Trees Motivations
AVL Trees 4/29/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
Red-Black Trees 2018年11月26日3时46分 AVL Trees v z AVL Trees.
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
Red-Black Trees.
Data Structures & Algorithms
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
AVL-Trees.
AVL-Trees (Part 1).
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Red-black tree properties
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
Presentation transcript:

Rizwan Rehman Centre for Computer Studies Dibrugarh University AVL-Trees Rizwan Rehman Centre for Computer Studies Dibrugarh University

Balanced binary tree The disadvantage of a binary search tree is that its height can be as large as N-1 This means that the time needed to perform insertion and deletion and many other operations can be O(N) in the worst case We want a tree with small height A binary tree with N node has height at least (log N) Thus, our goal is to keep the height of a binary search tree O(log N) Such trees are called balanced binary search trees. Examples are AVL tree, red-black tree.

AVL tree 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 height of its children plus 1 Note that this definition of height is different from the one we defined previously (we defined the height of a leaf as zero previously).

AVL tree An AVL tree is a binary search tree in which for every node in the tree, the height of the left and right subtrees differ by at most 1. AVL property violated here

AVL tree Let x be the root of an AVL tree of height h Let Nh denote the minimum number of nodes in an AVL tree of height h Clearly, Ni ≥ Ni-1 by definition We have By repeated substitution, we obtain the general form The boundary conditions are: N1=1 and N2 =2. This implies that h = O(log Nh). Thus, many operations (searching, insertion, deletion) on an AVL tree will take O(log N) time.

Rotations When the tree structure changes (e.g., insertion or deletion), we need to transform the tree to restore the AVL tree property. This is done using single rotations or double rotations. e.g. Single Rotation y x x A y B C C B A After Rotation Before Rotation

Rotations Since an insertion/deletion involves adding/deleting a single node, this can only increase/decrease the height of some subtree by 1 Thus, if the AVL tree property is violated at a node x, it means that the heights of left(x) ad right(x) differ by exactly 2. Rotations will be applied to x to restore the AVL tree property.

Insertion First, insert the new key as a new leaf just as in ordinary binary search tree Then trace the path from the new leaf towards the root. For each node x encountered, check if heights of left(x) and right(x) differ by at most 1. If yes, proceed to parent(x). If not, restructure by doing either a single rotation or a double rotation [next slide]. For insertion, once we perform a rotation at a node x, we won’t need to perform any rotation at any ancestor of x.

Insertion Let x be the node at which left(x) and right(x) differ by more than 1 Assume that the height of x is h+3 There are 4 cases Height of left(x) is h+2 (i.e. height of right(x) is h) Height of left(left(x)) is h+1  single rotate with left child Height of right(left(x)) is h+1  double rotate with left child Height of right(x) is h+2 (i.e. height of left(x) is h) Height of right(right(x)) is h+1  single rotate with right child Height of left(right(x)) is h+1  double rotate with right child Note: Our test conditions for the 4 cases are different from the code shown in the textbook. These conditions allow a uniform treatment between insertion and deletion.

Single rotation The new key is inserted in the subtree A. The AVL-property is violated at x height of left(x) is h+2 height of right(x) is h.

Single rotation The new key is inserted in the subtree C. The AVL-property is violated at x. Single rotation takes O(1) time. Insertion takes O(log N) time.

x AVL Tree C y 8 B A Insert 0.8 3 5 After rotation 5 5 8 3 3 4 1 4 1

Double rotation The new key is inserted in the subtree B1 or B2. The AVL-property is violated at x. x-y-z forms a zig-zag shape also called left-right rotate

Double rotation The new key is inserted in the subtree B1 or B2. The AVL-property is violated at x. also called right-left rotate

x y A z B C AVL Tree 8 Insert 3.5 4 5 After Rotation 5 5 8 3 3 4 1 4 1

An Extended Example Insert 3,2,1,4,5,6,7, 16,15,14 3 3 3 5 Single rotation 3 2 1 Fig 3 2 1 3 Fig 4 3 2 Fig 2 3 Fig 1 2 1 3 4 Fig 5 2 1 3 4 5 Fig 6 Single rotation

6 4 4 5 5 6 Single rotation Single rotation 2 1 5 3 Fig 8 2 1 5 3 7 Fig 10 Single rotation 4 2 6 7 1 3 5 Fig 11

4 2 6 7 1 3 5 16 Fig 12 4 2 6 7 1 3 5 16 15 Fig 13 Double rotation 4 2 6 15 1 3 5 16 7 Fig 14

5 4 2 7 15 1 3 6 16 14 Fig 16 4 2 6 15 1 3 5 16 7 14 Fig 15 Double rotation

Deletion Delete a node x as in ordinary binary search tree. Note that the last node deleted is a leaf. Then trace the path from the new leaf towards the root. For each node x encountered, check if heights of left(x) and right(x) differ by at most 1. If yes, proceed to parent(x). If not, perform an appropriate rotation at x. There are 4 cases as in the case of insertion. For deletion, after we perform a rotation at x, we may have to perform a rotation at some ancestor of x. Thus, we must continue to trace the path until we reach the root.

Deletion On closer examination: the single rotations for deletion can be divided into 4 cases (instead of 2 cases) Two cases for rotate with left child Two cases for rotate with right child

Single rotations in deletion In both figures, a node is deleted in subtree C, causing the height to drop to h. The height of y is h+2. When the height of subtree A is h+1, the height of B can be h or h+1. Fortunately, the same single rotation can correct both cases. rotate with left child

Single rotations in deletion In both figures, a node is deleted in subtree A, causing the height to drop to h. The height of y is h+2. When the height of subtree C is h+1, the height of B can be h or h+1. A single rotation can correct both cases. rotate with right child

Rotations in deletion There are 4 cases for single rotations, but we do not need to distinguish among them. There are exactly two cases for double rotations (as in the case of insertion) Therefore, we can reuse exactly the same procedure for insertion to determine which rotation to perform