AVL Trees: AVL Trees: Balanced binary search tree

Slides:



Advertisements
Similar presentations
COSC 2007 Data Structures II Chapter 12 Advanced Implementation of Tables II.
Advertisements

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.
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
AVL Trees CS II – Fall /8/2010. Announcements HW#2 is posted – Uses AVL Trees, so you have to implement an AVL Tree class. Most of the code is provided.
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
CSC 213 Lecture 7: Binary, AVL, and Splay Trees. Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries):
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.
Analysis of Red-Black Tree Because of the rules of the Red-Black tree, its height is at most 2log(N + 1). Meaning that it is a balanced tree Time Analysis:
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.
CS 367 – Introduction to Data Structures
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 and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
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 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.
Binary Search Trees Chapter 7 Objectives
AVL Trees CSE, POSTECH.
Part-D1 Binary Search Trees
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
AA Trees.
BCA-II Data Structure Using C
Search Trees.
Binary Search Tree (BST)
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
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.
AVL Search Trees Introduction What is an AVL Tree?
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
AVL Tree Mohammad Asad Abbasi Lecture 12
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
AVL Tree 27th Mar 2007.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
AVL Trees 11/10/2018 AVL Trees v z AVL Trees.
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
CSE 373 AVL trees read: Weiss Ch. 4, section
AVL Trees CENG 213 Data Structures.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
AVL Trees: AVL Trees: Balanced binary search tree
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
singleRightRotation 
CSE 373: Data Structures and Algorithms
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
Copyright © Aiman Hanna All rights reserved
CSE 373: Data Structures and Algorithms
Tree Rotations and AVL Trees
CSE 332: Data Abstractions AVL Trees
CSE 373 Data Structures and Algorithms
Lecture No.20 Data Structures Dr. Sohail Aslam
Binary Trees, Binary Search Trees
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
short illustrative repetition
Lecture 9: Self Balancing Trees
AVL Tree By Rajanikanth B.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
Binary Trees, Binary Search Trees
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

AVL Trees: AVL Trees: Balanced binary search tree Ensures that every node can be reached in O(log n) or less Named after inventors: Adelson-Velskii and Landis To stay balanced, the AVL tree maintains the following properties: For every node, the height of the left child and the height of the right child differ by no more than 1 Every subtree is an AVL tree <- left right ->

AVL Trees: Searching: done like a binary tree Traversal: done like a binary tree The sticky ones: Insertion and Deletion of nodes At each node n, we keep track of its “balance” n->leftchild->height - n->rightchild->height 0 (3 – 3) 0 (2 – 2) 1(2 – 1) 0 (0 – 0) 0 (1 – 1) 1 (1 – 0) -1 (0 – 1) 0(0-0) 0 (0 – 0) 0 ( 0 – 0) 0 (0 – 0) <- left right ->

A: What is the height of each node? B: What is the balance of each node? C: Is this an AVL tree? D: What changes if you insert -9 H:4 B:-1 B:0 H:2 H:3 H:3 B:-1 B:1 B:-2 H:1 H:2 H:2 H:1 B:1 B:0 B:1 B:0 H:1 -9 B:0 H:1 B:0

Insertion and maintaining balance: Inserting may cause tree to become unbalanced At least one node becomes 2 or –2 Only direct ancestors of inserted node back up to root node could have possibly changed in height After the insert, travel up parents to the root, updating heights if necessary If a node’s balance (hleftchild – hrightchild) is 2 or –2, adjust tree by rotation around the node -1 2 1 -2 -1 68 <- left right ->

Rules for insertion: Assume we are inserting node w into a tree: 1. Perform standard BST insert with node w. 2. Starting from w, travel up ancestors and find first unbalanced node (2 or -2). 4 cases: z (the unbalanced node) has a balance of 2 We check z’s left child (why left child)? For it’s unbalance Case 1: z’s left child has a balance of 1 Do one rotation around z Case 2: z’s left child has a balance of -1 Rotate first around z’s left child to make it heavier on the left side as well Then rotate around z so that its right side becomes heavier Z (the unbalanced node) has a balance of -2 We check z’s right child (why right?) for its unbalance Case 3: z’s right child has a balance of -1 Case 4: z’s right child has a balance of 1 Rotate first around z’s right child to make it heavier on the right side Then rotate around z to make it heavier on the left side. <- left right ->

Insertion: rotations (the fun part) Left-left rotation: The unbalanced node and its right child (the heavier side) both have less height on the left side Only need to do one rotation (to the left, around the unbalanced node) if we have a left- left unbalance -2 12 5 18 3 12 -1 5 7 18 3 7 15 15 inserted Single left rotation: 5’s right child becomes 12’s left child 12’s left chld becomes 5 12 becomes new root How many steps? <- left right ->

Insertion: rotations Right-right rotation: pretty much the same thing The unbalanced node and its left child are both unbalanced on the right side (have a greater left height than a right height): Again, only one rotation needed (to the right, around the unbalanced node) 18 14 21 17 7 5 inserted 1 2 Single right rotation: 18’s left child becomes 14’s right child 14’s right child becomes 18 14 becomes new root <- left right ->

Left-Right rotation (LR) If we have a negative balance at the root, and a positive balance at the right child -2 16 2 13 -1 13 8 16 1 22 8 14 14 22 15 15 inserted Simple left rotation (rotating to the left around the unbalanced node) doesn’t work : <- left right ->

Instead: Do a double-rotation Do a right rotation on the right subtree (even though its balance is only off by 1) And then do our typical left rotation on the root: -2 -2 14 13 13 -1 13 16 8 16 1 8 14 16 8 15 22 14 22 15 22 15 <- left right ->

Try: (You just inserted what?) 17 17 20 12 22 12 14 22 7 14 20 7 19 19 <- left right ->

Try: (You just inserted what?) 16 16 42 12 30 12 14 30 60 7 14 7 24 42 28 36 60 24 36 28 <- left right ->

Try: (What did you just insert?) 20 20 20 7 35 7 32 7 32 3 3 26 35 32 48 26 48 3 26 34 34 48 51 35 51 Stopped here 51 34 <- left right ->

Pseudocode: Single Left rotation Also check if oldroot->parent->right == oldroot, then oldroot->parent->right = tmp otherwise oldroot->parent->left = tmp tmp = oldroot->right oldroot->right = tmp->left tmp->left = oldroot //remember parents… oldroot->parent = tmp oldroot->right->parent = oldroot 12 5 18 3 12 5 7 18 3 7 15 15 <- left right ->

class node { int key; node. left; node. right; int height; }; node class node { int key; node *left; node *right; int height; }; node *rightRotate(node *y){ // this is partial code – must worry about NULL children //and about attaching rotated nodes to new parents node *x = y->left; node *tmp = x->right; // Perform rotation x->right = y; y->left = tmp; // Update heights if (y->left->height > y->right->height) { //why did we look at y first? y->height = y->left->height + 1; } else { y->height =y->right->height + 1; if (x->left->height > x->right->height) { x->height = x->left->height + 1; x->height = x->right->height+1; return x; // Return new root Code for rotations:

Try: Make an AVL Tree: 17, 12, 8, 16,13,14 13 12 17 8 12 13 12 16 16 17 8 16 13 16 17 17 8 14 17 14 14 13 <- left right ->

Try: 44 44 50 50 44 78 17 17 78 48 78 17 48 62 88 32 50 88 62 88 48 62 Remove 32 <- left right ->

Remove a node: (Remember?) Use BST remove No children: delete One child: replace with child 2 children: find the left-most descendent of the right child And now we rebalance the tree Starting from the deleted node, going up, rebalance the first unbalanced node <- left right ->

With Delete, we have to check nodes ABOVE for rebalancing: 50 50 25 25 75 25 60 10 50 60 30 60 80 10 30 55 75 5 15 30 10 1 27 55 75 5 15 27 55 5 15 27 1 1 Delete 80 (For height, follow path up from relocated node and recalculate height of each node (max (height of left , height of right)) stop when hit an unbalanced node, rebalance, recalculate height of that node, and then continue up path One rotation may not be enough! (125) <- left right ->