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.
Interval Trees Store intervals of the form [li,ri], li <= ri.
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.
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.
Solution of Assignment 3 and Midterm CSC2100B. AVL Tree A binary search tree is a binary tree in which every node has larger key than the nodes in its.
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()
CSE 326: Data Structures AVL Trees
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.
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.
1 Height-Balanced Binary Search Trees AVL Trees. 2 Background zBinary Search Trees allow dynamic allocation (like linked lists), but O(log 2 (n)) average.
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,
CSE373: Data Structures & Algorithms Optional Slides: AVL Delete Dan Grossman Fall 2013.
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.
§4 AVL Trees Target : Speed up searching (with insertion and deletion) Tool : Binary search trees root smalllarge Problem : Although T p = O( height ),
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.
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.
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.
Data Structures AVL Trees.
AVL Trees It’s a balancing act. Binary Tree Problems If you get either sorted or reverse-sorted input, you essentially get a linked list (always following.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 20 AVL Trees.
Copyright © 2005 Pearson Addison-Wesley. All rights reserved Balancing Binary Trees There are many approaches to balancing binary trees One method.
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 CSE, POSTECH.
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
Red Black Trees Colored Nodes Definition Binary search tree.
Splay Trees Binary search trees.
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.
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.
Draft for an AVL tree insertion visualization with multiple levels of engagement T Special Course in Software Techniques: Directions for Future.
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
Splay Trees Binary search trees.
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
Balanced Binary Search Trees
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
Data Structures & Algorithms
CSE 373: Data Structures and Algorithms
AVL Trees CSE 373 Data Structures.
AVL Search Tree put(9)
CSE 373 Data Structures and Algorithms
Dynamic Dictionaries Primary Operations: Additional operations:
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
AVL Tree By Rajanikanth B.
Data Structures Lecture 21 Sohail Aslam.
ITCS6114 Algorithms and Data Structures
INSERT THE TITLE OF YOUR PRESENTATION HERE:
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
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. 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.

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 => tree is empty following deletion. 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.