Dynamic Dictionaries Primary Operations: Additional operations:

Slides:



Advertisements
Similar presentations
Splay Trees Binary search trees.
Advertisements

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.
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
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.
CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties and maintenance.
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.
Binary Search Trees1 ADT for Map: Map stores elements (entries) so that they can be located quickly using keys. Each element (entry) is a key-value pair.
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 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.
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 
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 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.
Binary Search Trees1 Chapter 3, Sections 1 and 2: Binary Search Trees 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()
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
AVL Trees CSE, POSTECH.
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
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.
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 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
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 11/10/2018 AVL Trees v z AVL Trees.
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
CSE 373: Data Structures and Algorithms
Balanced Binary Search Trees
AVL Trees CSE 373 Data Structures.
AVL Search Tree put(9)
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
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
CSE 373 Data Structures Lecture 8
Red Black Trees Colored Nodes Definition Binary search tree.
Dictionaries 二○一九年九月二十四日 ADT for Map:
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
Presentation transcript:

Dynamic Dictionaries Primary Operations: Additional 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 Hash table get and remove using O(n) select would take O(D + n) time.

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 -1 1 1 -1 1 -1 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 log2 (n+2). The height of every n node binary tree is at least log2 (n+1). log2 (n+1) <= height <= 1.44 log2 (n+2)

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

Nh, h > 1 L R 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 Nh-1 nodes. The subtree whose height is h-2 has Nh-2 nodes. So, Nh = Nh-1 + Nh-2 + 1.

Fibonacci Numbers F0 = 0, F1 = 1. Fi = Fi-1 + Fi-2 , i > 1. N0 = 0, N1 = 1. Nh = Nh-1 + Nh-2 + 1, i > 1. Nh = Fh+2 – 1. Fi ~ fi/sqrt(5). f = (1 + sqrt(5))/2. Note that for Fibonacci heaps Ni = Fi+2. Also, for Fibonacci heaps, i is the degree (not height).

AVL Search Tree 1 -1 10 7 8 3 5 30 40 20 25 35 45 60 AVL search tree is often simply referred to as an AVL tree.

Insert(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

Insert(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

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

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

Delete An Element Delete 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. Delete 8.

Delete 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 has just one node. As q moves up towards the root, neither subtree of q is empty.

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