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

Slides:



Advertisements
Similar presentations
CSE 373 Data Structures and Algorithms
Advertisements

Splay Trees Binary search trees.
AVL Trees binary tree for every node x, define its balance factor
Part II. Delete an Node from an AVL Tree Consider to delete
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.
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
1 CSE 373 AVL trees, continued read: Weiss Ch. 4, section slides created by Marty Stepp
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.
CS2420: Lecture 30 Vladimir Kulyukin Computer Science Department Utah State University.
CS2420: Lecture 28 Vladimir Kulyukin Computer Science Department Utah State University.
Dynamic Dictionaries Primary Operations:  Get(key) => search  Insert(key, element) => insert  Delete(key) => delete Additional operations:  Ascend()
Red Black Trees Colored Nodes Definition Binary search tree.
CS2420: Lecture 29 Vladimir Kulyukin Computer Science Department Utah State 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.
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.
Balanced Binary Search Trees height is O(log n), where n is the number of elements in the tree AVL (Adelson-Velsky and Landis) trees red-black trees get,
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 Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.
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.
Balanced Search Trees Problem: Efficiency of BST is related to tree’s height.  search, insert and remove follow a path from root to desired location 
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.
© 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.
1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each node is 0, 1, of -1. Basic Operations Construction,
AVL Trees CSE, POSTECH.
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
Balanced Binary Search Trees
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
Red Black Trees Colored Nodes Definition Binary search tree.
Search Trees.
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.
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Splay Trees 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.
Introduction Applications Balance Factor Rotations Deletion Example
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.
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
AVL Tree 27th Mar 2007.
Splay Trees Binary search trees.
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
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.
AVL Trees: AVL Trees: Balanced binary search tree
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.
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
Data Structures & Algorithms
CSE 373: Data Structures and Algorithms
Balanced Binary Search Trees
AVL Search Tree put(9)
Dynamic Dictionaries Primary Operations: Additional operations:
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
AVL Tree By Rajanikanth B.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
ITCS6114 Algorithms and Data Structures
INSERT THE TITLE OF YOUR PRESENTATION HERE AVL TREE.
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
Tree Balancing: AVL Trees
Red Black Trees Colored Nodes Definition Binary search tree.
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

AVL Trees binary tree for every node x, define its balance factor balance factor of x = height of left subtree of x – height of right subtree of x balance factor of every node x is – 1, 0, or 1 log2 (n+1) <= height <= 1.44 log2 (n+2)

Example AVL Tree 1 -1 10 7 8 3 5 30 40 20 25 35 45 60

put(9) -1 10 1 1 7 40 -1 -1 1 45 3 8 30 Some balance factors on insert path change. The height of a subtree on this insert path can change by at most 1. So, bf of a node on the insert path can change by at most 1. When a bf changes from 0 to +1 or –1 the subtree height increases and we need to go further up the tree adjusting balance factors. -1 60 35 1 9 20 5 25

put(29) -1 10 1 1 7 40 -1 1 45 3 8 30 -1 -2 60 35 1 20 5 White node is the A node, nearest ancestor of newly inserted node whose bf becomes +2 or –2. RR imbalance => new node is in right subtree of right subtree of white node (node with bf = –2) -1 25 29

put(29) -1 10 1 1 7 40 -1 1 45 3 8 30 60 35 1 25 5 20 29 RR rotation.

Insert/Put Following insert/put, retrace path towards root and adjust balance factors as needed. Stop when you reach a node whose balance factor becomes 0, 2, or –2, or when you reach the root. The new tree is not an AVL tree only if you reach a node whose balance factor is either 2 or –2. In this case, we say the tree has become unbalanced.

A-Node Let A be the nearest ancestor of the newly inserted node whose balance factor becomes +2 or –2 following the insert. Balance factor of nodes between new node and A is 0 before insertion.

Balance Factors Between New Node and A Are 0 1 -1 10 7 8 3 5 30 40 20 25 35 45 60 Insert 6

Imbalance Types RR … newly inserted node is in the right subtree of the right subtree of A. LL … left subtree of left subtree of A. RL… left subtree of right subtree of A. LR… right subtree of left subtree of A.

LL Rotation Subtree height is unchanged. Before insertion. 1 A B BL BR AR h A B B’L BR AR After insertion. h+1 h B 2 B’L h+1 A 1 BR h AR h After rotation. Subtree height is unchanged. No further adjustments to be done.

LR Rotation (case 1) Subtree height is unchanged. Before insertion. 1 A B A B After insertion. C C 2 B A -1 After rotation. B is a leaf prior to the insert. Subtree height is unchanged. No further adjustments to be done.

LR Rotation (case 2) Subtree height is unchanged. BL CR AR h h-1 C’L C 1 A B BL CR AR h h-1 CL C C 2 B A -1 -1 BL h C’L h CR h-1 AR h 1 B is not a leaf prior to the insert, the insert takes place in the left subtree of C. Subtree height is unchanged. No further adjustments to be done.

LR Rotation (case 3) Subtree height is unchanged. BL C’R AR h CL h-1 C -1 2 1 A B BL CR AR h h-1 CL C C A C’R h AR B BL CL h-1 1 Subtree height is unchanged. No further adjustments to be done.

Single & Double Rotations LL and RR Double LR and RL LR is RR followed by LL RL is LL followed by RR

LR Is RR + LL A C CL C’R AR h BL B After RR rotation. h-1 A B BL C’R 2 After RR rotation. h-1 A B BL C’R AR h CL h-1 C -1 2 After insertion. C A C’R h AR B BL CL h-1 1 After LL rotation.

Remove An Element Remove 8. 1 -1 10 7 8 3 5 30 40 20 25 35 45 60 1 -1 10 7 8 3 5 30 40 20 25 35 45 60 Remove 5; no traceback needed. Remove 8.

Remove An Element q Let q be parent of deleted node. 2 -1 1 10 7 3 5 30 40 20 25 35 45 60 q No q => no rebalancing to be done. Let q be parent of deleted node. Retrace path from q towards root.

New Balance Factor Of q Deletion from left subtree of q => bf--. Deletion from right subtree of q => bf++. New balance factor = 1 or –1 => no change in height of subtree rooted at q. New balance factor = 0 => height of subtree rooted at q has decreased by 1. New balance factor = 2 or –2 => tree is unbalanced at q. Initially q is the parent of the deleted node. At this time, one subtree of q is empty and the other is not empty. As q moves up towards the root, neither subtree of q is empty.

Imbalance Classification Let A be an ancestor (q) of the deleted node whose balance factor has become 2 or –2 following a deletion. Deletion from left subtree of A => type L. Deletion from right subtree of A => type R. Type R => new bf(A) = 2. So, old bf(A) = 1. So, A has a left child B. bf(B) = 0 => R0. bf(B) = 1 => R1. bf(B) = –1 => R-1.

R0 Rotation Subtree height is unchanged. Before deletion. 1 A B BL BR AR h A B BL BR A’R After deletion. h h-1 2 B A After rotation. BR h A’R h-1 BL 1 -1 Subtree height is unchanged. No further adjustments to be done. Similar to LL rotation.

R1 Rotation Subtree height is reduced by 1. Before deletion. 1 A B BL BR AR h h-1 A B BL BR A’R After deletion. h h-1 1 2 B A After rotation. BR h-1 A’R BL h Subtree height is reduced by 1. Must continue on path to root. Similar to LL and R0 rotations.

R-1 Rotation New balance factor of A and B depends on b. BL CR A’R h-1 CL C b -1 2 1 -1 A B BL CR AR h-1 h b CL C C A CR A’R h-1 B BL CL New balance factor of A and B depends on b. Subtree height is reduced by 1. Must continue on path to root. Similar to LR.

Number Of Rebalancing Rotations At most 1 for an insert. O(log n) for a delete.

Rotation Frequency Insert random numbers. No rotation … 53.4% (approx). LL/RR … 23.3% (approx). LR/RL … 23.2% (approx). Frequencies do not sum to 100% because of rounding errors in reporting frequencies to one decimal place.