4.5 AVL Trees  A tree is said to be balanced if for each node, the number of nodes in the left subtree and the number of nodes in the right subtree differ.

Slides:



Advertisements
Similar presentations
§2 Binary Trees Note: In a tree, the order of children does not matter. But in a binary tree, left child and right child are different. A B A B andare.
Advertisements

Trees Types and Operations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
B+-Trees (PART 1) What is a B+ tree? Why B+ trees? Searching a B+ tree
Binary Trees, Binary Search Trees COMP171 Fall 2006.
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.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
Advanced Tree Data Structures Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
AVL-Trees (Part 1: Single Rotations) Lecture COMP171 Fall 2006.
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
Advanced Tree Data Structures Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
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.
Balanced Trees Abs(depth(leftChild) – depth(rightChild))
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.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 45 AVL Trees and Splay.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Splay Trees and B-Trees
1 B-Trees Section AVL (Adelson-Velskii and Landis) Trees AVL tree is binary search tree with balance condition –To ensure depth of the tree is.
ICS 220 – Data Structures and Algorithms Week 7 Dr. Ken Cosh.
INTRODUCTION TO AVL TREES P. 839 – 854. INTRO  Review of Binary Trees: –Binary Trees are useful for quick retrieval of items stored in the tree –order.
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.
INTRODUCTION TO MULTIWAY TREES P INTRO - Binary Trees are useful for quick retrieval of items stored in the tree (using linked list) - often,
Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.
CSIT 402 Data Structures II
Starting at Binary Trees
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.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
COSC 2P03 Week 51 Representation of an AVL Node class AVLNode { AVLnode left; AVLnode right; int height; int height(AVLNode T) { return T == null? -1 :
Binary trees -2 Chapter Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can.
11/7/20151 Balanced Trees Data Structures Ananda Gunawardena.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Lecture 11COMPSCI.220.FS.T Balancing an AVLTree Two mirror-symmetric pairs of cases to rebalance the tree if after the insertion of a new key to.
Chapter 7 Trees_Part3 1 SEARCH TREE. Search Trees 2  Two standard search trees:  Binary Search Trees (non-balanced) All items in left sub-tree are less.
3.1. Binary Search Trees   . Ordered Dictionaries Keys are assumed to come from a total order. Old operations: insert, delete, find, …
Data Structures AVL Trees.
AVL Trees An AVL tree is a binary search tree with a balance condition. AVL is named for its inventors: Adel’son-Vel’skii and Landis AVL tree approximates.
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.
1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 20 AVL Trees.
Data Structures Using C++ 2E Chapter 11 Binary Trees.
CSE 3358 NOTE SET 13 Data Structures and Algorithms.
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,
CE 221 Data Structures and Algorithms Chapter 4: Trees (AVL Trees) Text: Read Weiss, §4.4 1Izmir University of Economics.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AA Trees.
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
BCA-II Data Structure Using C
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.
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.
CS 201 Data Structures and Algorithms
Chapter 29 AVL Trees.
AVL Tree Mohammad Asad Abbasi Lecture 12
Lecture 22 Binary Search Trees Chapter 10 of textbook
AVL Trees CENG 213 Data Structures.
Trees.
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
CS223 Advanced Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
AVL Search Tree put(9)
CE 221 Data Structures and Algorithms
CS223 Advanced Data Structures and Algorithms
AVL Tree By Rajanikanth B.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Presentation transcript:

4.5 AVL Trees  A tree is said to be balanced if for each node, the number of nodes in the left subtree and the number of nodes in the right subtree differ by at most one.  A tree is said to be height-balanced or AVL if for each node, the height of the left subtree and height of the right subtree differ by at most one.

4.5 AVL Trees  Height of AVL tree is 1.44log(N+2)  logN  searching complexity  O(log N)

4.5 AVL Trees  The height of the left subtree minus the height of the right subtree of a node is called the balance of the node. For an AVL tree, the balances of the nodes are always -1, 0 or 1.  Given an AVL tree, if insertions or deletions are performed, the AVL tree may not remain height balanced.

4.5 AVL Trees  Violation may occur when an insertion into 1. left subtree of left child (LL case) 2. right subtree of left child (RL case) 3. left subtree of right child (LR case) 4. right subtree of right child (RR case)

4.5 AVL Trees n+1 n n From: J Beidler, Data structures and algorithms, Springer1997

4.5 AVL Trees  To maintain the height balanced property of the AVL tree after insertion or deletion, it is necessary to perform a transformation on the tree so that (1) the inorder traversal of the transformed tree is the same as for the original tree (i.e., the new tree remains a binary search tree). (2) the tree after transformation is height balanced.

4.5 AVL Trees  Rotation - to restore the AVL tree after insertion - single rotation - double rotation

4.5 AVL Trees  single rotation

4.5 AVL Trees  Double rotation

4.5.1 AVL Trees: Single Rotation  Fix LL and RR cases

4.5.1 AVL Trees: Single Rotation  Example:  construct binary search tree without height balanced restriction  depth of tree = 4 i.e. LL case

4.5.1 AVL Trees: Single Rotation  Construct AVL tree (height balanced)

4.5.1 AVL Trees: Single Rotation Insert 4, 5 Insert 6

4.5.1 AVL Trees: Single Rotation Insert 7

4.5.2 AVL Trees: Double Rotation Single rotation fails to fix cases 2 and 3 (i.e. LR and RL cases) n n  n+1 n

4.5.2 AVL Trees: Double Rotation Double rotation is used case 2 (LR case) n+1 nn

4.5.2 AVL Trees: Double Rotation case 3 (RL case)

4.5.2 AVL Trees: Double Rotation Example: Double Rotation

4.5.2 AVL Trees: Double Rotation Insert 14 Double Rotation

4.5.2 AVL Trees: Double Rotation Insert 13 Single Rotation

4.5.2 AVL Trees: Double Rotation Insert 12, 11, 10 Single Rotation

4.5.2 AVL Trees: Double Rotation Insert 8 and 9

4.5.2 AVL Trees: Double Rotation Result

4.5.3 AVL Trees: Implementation After insertion, if the height of the node does not change => done Otherwise, if imbalance appears, then determine the appropriate action: single or double rotation

4.5.3 AVL Trees: Implementation struct AvlNode; typedef struct AvlNode *Position; typedef struct AvlNode *AvlTree; AvlTree MakeEmpty( AvlTree T ); Position Find( ElementType X, AvlTree T ); Position FindMin( AvlTree T ); Position FindMax( AvlTree T ); AvlTree Insert( ElementType X, AvlTree T ); AvlTree Delete( ElementType X, AvlTree T ); ElementType Retrieve( Position P );

4.5.3 AVL Trees: Implementation struct AvlNode { ElementType Element; AvlTree Left; AvlTree Right; int Height; };

4.5.3 AVL Trees: Implementation AvlTree MakeEmpty( AvlTree T ) { if( T != NULL ) { MakeEmpty( T->Left ); MakeEmpty( T->Right ); free( T ); } return NULL; }

4.5.3 AVL Trees: Implementation static int Height( Position P ) { if( P == NULL ) return -1; else return P->Height; }

4.5.3 AVL Trees: Implementation static Position SingleRotateWithLeft( Position K2 ) { Position K1; K1 = K2  Left; K2  Left = K1  Right; K1  Right = K2; K2  Height = Max( Height( K2  Left ), Height(K2  Right ) ) + 1; K1  Height = Max( Height( K1  Left ), K2  Height ) + 1; return K1; /* New root */ }

4.5.3 AVL Trees: Implementation static Position SingleRotateWithRight( Position K1 ) { Position K2; K2 = K1  Right; K1  Right = K2  Left; K2  Left = K1; K1  Height = Max( Height( K1  Left ), Height( K1  Right ) ) + 1; K2  Height = Max( Height( K2  Right ), K1  Height ) + 1; return K2; /* New root */ }

4.5.3 AVL Trees: Implementation static Position DoubleRotateWithLeft( Position K3 ) { /* Rotate between K1 and K2 */ K3  Left = SingleRotateWithRight( K3  Left ); /* Rotate between K3 and K2 */ return SingleRotateWithLeft( K3 ); }

4.5.3 AVL Trees: Implementation static Position DoubleRotateWithRight( Position K1) { /* Rotate between K3 and K2 */ K1  Right = SingleRotateWithLeft( K1  Right ); /* Rotate between K1 and K2 */ return SingleRotateWithRight( K1 ); }

4.5.3 AVL Trees: Implementation AvlTree Insert( ElementType X, AvlTree T ) { if ( T == NULL ) { /* Create and return a one-node tree */ T = malloc( sizeof( struct AvlNode ) ); if ( T == NULL ) FatalError( "Out of space!!!" ); else { T  Element = X; T  Height = 0; T  Left = T  Right = NULL; } else

4.5.3 AVL Trees: Implementation if ( X < T  Element ) { T  Left = Insert( X, T  Left ) if ( Height( T  Left ) - Height( T  Right ) == 2 ) if ( X < T  Left  Element ) T = SingleRotateWithLeft( T ) else T = DoubleRotateWithLeft( T ); } else

4.5.3 AVL Trees: Implementation if ( X > T  Element ) { T  Right = Insert( X, T  Right ); if( Height( T  Right ) - Height( T  Left ) == 2 ) if( X > T  Right  Element ) T = SingleRotateWithRight( T ); elseT = DoubleRotateWithRight( T ); } /* Else X is in the tree already; we'll do nothing */ T  Height = Max( Height( T  Left ), Height( T  Right ) ) + 1; return T; }

4.6 B-Trees A B-tree of order M is a multiway tree of order M with the following structural properties. -The root is either a leaf or has at least 2 subtrees. -All nonroot nodes have at least M/2 branches. -All leaves are at the same level.

4.6 B-Trees The first constraint ensures that the tree branches out at the root. The second constraint ensures that each node of the tree is at least half full. The third constraint keeps the tree nearly balanced.

4.6 B-Trees Example: B-tree of order 4

4.6 B-Trees A B-tree of order 3 (known as a 2-3 tree)

4.6 B-Trees Insert 1 node with key = 8

4.6 B-Trees Insert 1 node with key = 1

4.6 B-Trees Insert 1 node with key = 19 Problem

4.6 B-Trees Insert 1 node with key = 19

4.6 B-Trees Insert 1 node with key = 28 Create a leaf with 4 children split into 2 leaves of 2 children Create a leaf with 4 children split into 2 leaves of 2 children

4.6 B-Trees Insert 1 node with key = 28 (cont) 4 nodes under root

4.6 B-Trees Insert 1 node with key = 28 (cont) Split the root into 2 nodes

4.6 B-Trees use of B-tree in database systems number of disk access is O(log M N) memory access time vs disk access time