Dynamic Dictionaries Primary Operations:  Get(key) => search  Insert(key, element) => insert  Delete(key) => delete Additional operations:  Ascend()

Slides:



Advertisements
Similar presentations
Splay Trees Binary search trees.
Advertisements

Interval Trees Store intervals of the form [li,ri], li <= ri.
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.
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
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 28 Vladimir Kulyukin Computer Science Department Utah State University.
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 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 ),
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 
Data Structures Chapter 10: Efficient Binary Search Trees 10-1.
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.
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.
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.
© 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.
Dynamic Dictionaries Primary Operations:  get(key) => search  put(key, element) => insert  remove(key) => delete Additional operations:  ascend()
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.
Splay Trees 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.
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
AVL Tree 27th Mar 2007.
Dynamic Dictionaries Primary Operations: Additional operations:
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.
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
CS223 Advanced Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Balanced Binary Search Trees
AVL Trees CSE 373 Data Structures.
AVL Search Tree put(9)
Dynamic Dictionaries Primary Operations: Additional operations:
CS223 Advanced Data Structures and Algorithms
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
INSERT THE TITLE OF YOUR PRESENTATION HERE AVL TREE.
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z 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:

Dynamic Dictionaries Primary Operations:  Get(key) => search  Insert(key, element) => insert  Delete(key) => delete Additional operations:  Ascend()  Get(index)  Delete(index)

Complexity Of Dictionary Operations Get(), Insert() and Delete() n is number of elements in dictionary

Complexity Of Other Operations Ascend(), Get(index), Delete(index) D is number of buckets

AVL Tree 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

Balance Factors This is an AVL tree.

Height Of An AVL Tree The height of an AVL tree that has n nodes is at most 1.44 log 2 (n+2). The height of every n node binary tree is at least log 2 (n+1). log 2 (n+1) <= height <= 1.44 log 2 (n+2)

Proof Of Upper Bound On Height Let N h = min # of nodes in an AVL tree whose height is h. N 0 = 0. N 1 = 1.

N h, h > 1 Both L and R are AVL trees. The height of one is h-1. The height of the other is h-2. The subtree whose height is h-1 has N h-1 nodes. The subtree whose height is h-2 has N h-2 nodes. So, N h = N h-1 + N h LR

Fibonacci Numbers F 0 = 0, F 1 = 1. F i = F i-1 + F i-2, i > 1. N 0 = 0, N 1 = 1. N h = N h-1 + N h-2 + 1, i > 1. N h = F h+2 – 1. F i ~  i /sqrt(5).   sqrt 

AVL Search Tree

Insert(9)

Insert(29) RR imbalance => new node is in right subtree of right subtree of white node (node with bf = – 2)

RR rotation Insert(29)

Insert Following insert, 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. No further adjustments to be done. Before insertion. 1 0 A B BLBL BRBR ARAR hh h A B B’ L BRBR ARAR After insertion. h+1h h BA After rotation. BRBR h ARAR h B’ L h

LR Rotation (case 1) Subtree height is unchanged. No further adjustments to be done. Before insertion. 1 0 A B A B After insertion. C CA After rotation. B

LR Rotation (case 2) Subtree height is unchanged. No further adjustments to be done. C A CRCR h-1 ARAR h 1 0 A B BLBL CRCR ARAR h h 0 CLCL C A B BLBL CRCR ARAR h h C’ L h C B BLBL h h

LR Rotation (case 3) Subtree height is unchanged. No further adjustments to be done. 1 0 A B BLBL CRCR ARAR h h-1 h 0 CLCL C A B BLBL C’ R ARAR h h h CLCL h-1 C 2 C A C’ R h ARAR h B BLBL h CLCL h

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

LR Is RR + LL A B BLBL C’ R ARAR h h h CLCL h-1 C 2 After insertion. A C CLCL C’ R ARAR h h BLBL h B 2 After RR rotation. h-1 C A C’ R h ARAR h B BLBL h CLCL h After LL rotation.

Delete An Element Delete 8.

Delete An Element Let q be parent of deleted node. Retrace path from q towards root. q

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. q

Imbalance Classification Let A be the nearest ancestor 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. No further adjustments to be done. Similar to LL rotation. Before deletion. 1 0 A B BLBL BRBR ARAR hh h B A After rotation. BRBR h A’ R h-1 BLBL h 1 A B BLBL BRBR A’ R After deletion. hh h-1 0 2

R1 Rotation Subtree height is reduced by 1. Must continue on path to root. Similar to LL and R0 rotations. Before deletion. 1 1 A B BLBL BRBR ARAR hh-1 h B A After rotation. BRBR h-1 A’ R h-1 BLBL h 0 0 A B BLBL BRBR A’ R After deletion. hh-1 1 2

R-1 Rotation 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. 1 A B BLBL CRCR ARAR h-1 h b CLCL C A B BLBL CRCR A’ R h-1 CLCL C b 2 C A CRCR A’ R h-1 B BLBL CLCL 0

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).