Part II. Delete an Node from an AVL Tree Consider to delete 4 10 2 1 8 1 0 9 5 0 3 1 6 4 0 7 0 11 12 0.

Slides:



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

CS16: Introduction to Data Structures & Algorithms
Binary Tree Structure a b fe c a rightleft g g NIL c ef b left right pp p pp left key.
CSE 373 Data Structures and Algorithms
Definitions and Bottom-Up Insertion
Introduction to Algorithms Red-Black Trees
Splay Trees Binary search trees.
AVL Trees CSE 373 Data Structures Lecture 8. 12/26/03AVL Trees - Lecture 82 Readings Reading ›Section 4.4,
CSE Lecture 17 – Balanced trees
Rizwan Rehman Centre for Computer Studies Dibrugarh University
AVL Trees binary tree for every node x, define its balance factor
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.
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
CS202 - Fundamental Structures of Computer Science II
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.
Dynamic Dictionaries Primary Operations:  Get(key) => search  Insert(key, element) => insert  Delete(key) => delete Additional operations:  Ascend()
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
CSE 326: Data Structures AVL Trees
Red Black Trees Colored Nodes Definition Binary search tree.
Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Dynamic Set ADT Balanced Trees Gerda Kamberova Department of Computer Science Hofstra 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.
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,
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 ),
CS Data Structures Chapter 10 Search Structures.
Balanced Trees (AVL and RedBlack). Binary Search Trees Optimal Behavior ▫ O(log 2 N) – perfectly balanced tree (e.g. complete tree with all levels filled)
Binary Tree Representation Of Trees Problems with trees. 2- and 3-nodes waste space. Overhead of moving pairs and pointers when changing among.
CSIT 402 Data Structures II
Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.
1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower.
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.
Data Structures Chapter 10: Efficient Binary Search Trees 10-1.
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.
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.
Bottom-Up Red-Black Trees Top-down red-black trees require O(log n) rotations per insert/delete. Color flips cheaper than rotations. Priority search trees.
Red-Black Tree Insertion Start with binary search insertion, coloring the new node red NIL l Insert 18 NIL l NIL l 1315 NIL l
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AA 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.
Red Black Trees Colored Nodes Definition Binary search tree.
Red Black Trees
Splay Trees Binary search trees.
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
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.
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 Tree.
Splay Trees Binary search trees.
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
TCSS 342, Winter 2006 Lecture Notes
Balanced Binary Search Trees
CS202 - Fundamental Structures of Computer Science II
Data Structures & Algorithms
CSE 373: Data Structures and Algorithms
AVL Search Tree put(9)
Dynamic Dictionaries Primary Operations: Additional operations:
CS202 - Fundamental Structures of Computer Science II
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
CS202 - Fundamental Structures of Computer Science II
Red Black Trees Colored Nodes Definition Binary search tree.
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

Part II

Delete an Node from an AVL Tree Consider to delete

Delete an Node from an AVL Tree 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++ Case1: New bf = 1 or –1 no change in height of subtree rooted at q. Case 2: New bf = 0 height of subtree rooted at q has decreased by 1. Retrace path back to the root, update the bf, and do rebalancing if necessary. Case 3: New balance factor = 2 or –2 tree is unbalanced at q. Perform rebalancing. q

Example of Case 2 Consider to delete If new bf is 0, retrace the path back to the root and update the bf.

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: type L. Deletion from right subtree of A: type R. Take type R for instance: new bf(A) = 2. Therefore, old bf(A) = 1. Suppose 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. 0 A B BLBL BRBR ARAR hh h 1 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 Similar to LL and R0 rotations. Subtree height is reduced by 1. Therefore, we must continue on path to root. Before deletion. 1 A B BLBL BRBR ARAR hh-1 h 1 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 Similar to LR. New balance factor of A and B depends on b. Subtree height is reduced by 1. Therefore, we must continue on path to root. 1 A B BLBL CRCR ARAR h-1 h CLCL C b A B BLBL CRCR A’ R h-1 CLCL C 2 b C A CRCR A’ R h-1 B BLBL CLCL 0

L0 Rotation Subtree height is unchanged. No further adjustments to be done. Similar to RR rotation. Before deletion. A B BLBL BRBR ALAL hh h B A After rotation. A’ L h-1 BLBL h BRBR h 1 1 A B BLBL BRBR A’ L After deletion. hh h

L1 Rotation Similar to RL. New balance factor of A and B depends on b. Subtree height is reduced by 1. Therefore, we must continue on path to root. A B BRBR CRCR ALAL h-1 h CLCL C 1 b C B CRCR BRBR A A’ L h-1 CLCL 0 -2 A B BRBR CRCR A’ L h-1 CLCL C 1 b

L-1 Rotation Similar to RR rotations. Subtree height is reduced by 1. Therefore, we must continue on path to root. Before deletion. A B BLBL BRBR ALAL h-1h h B A After rotation. ALAL h-1 BLBL BRBR h 0 0 A A’ L After deletion. h-1 -2 B BLBL BRBR h-1h

Number Of Rebalancing Rotations At most O(log n) for one insertion. At most O(log n) to update bf. O(log n) for one deletion.

Red-Black Tree A red-black tree is an extended binary search tree that satisfies the following two definitions: Each node/pointer is colored red or black. Colored nodes definition Colored edges definition External nodes Extended binary search tree Internal nodes

Introduction Colored Nodes Definition RB1: The root and all external nodes are black. RB2: No root-to-external-node path has two consecutive red nodes. RB3: All root-to-external-node paths have the same number of black nodes

Introduction Colored Edges Definition RB1’: Pointer to an external node is black. RB2’: No root to external node path has two consecutive red pointers. RB3’: Every root to external node path has the same number of black pointers.

Example Red-Black Tree

Properties Let the rank of a node be the number of black pointers on any path from the node to any external node. Suppose that the rank of the root is r. From RB2’, each red pointer is followed by a black pointer. Therefore, each root-to-external-node path has between r and 2r.

Properties Lemma 10.2 Let h be the height of a red-black tree (excluding the external nodes), let n be the number of internal nodes, and let r be the rank of the root. a) h ≦ 2r b) n ≧ 2 r -1 c) h ≦ 2log 2 (n+1) From b), we have r ≦ log 2 (n+1). This equality together with a) yields c).

Properties Since the height of a red-black tree is at most 2log 2 (n+1), search, insert, and delete algorithms that work in O(h) time have complexity O(log n). Notice: The worst-case height of a red-black tree is more than the worst-case height (approximately 1.44log 2 (n+2)) of an AVL tree with the same number of internal nodes.

Inserting into a RED-Black Tree Elements is first inserted using the strategy used for ordinary binary tree. New node color options. Black node: one root-to-external-node path has an extra black node (violation to RB3). Hard to remedy. Red node: one root-to-external-node path may have two consecutive red nodes may or may not violate to RB2 Can be remedied by color flips and/or a rotation. We will make the new node red.

Classification of Two Red Nodes LL type u is red, pu is red and gu is black (gu must be black. Why?) LLr: the other child of gu, d, is red. LLb: the other child of gu, d, is black. ab c d gu pu u

Classification of Two Red Nodes Type XYz X: relationship between gu and pu. For example, if pu is the left child of gu, then X = L. Y: relationship between pu and u. For example, if u is the right child of pu, then Y = R. z: b (black) if d is black. r (red) if d is red.

Resolution of Type XYr Color change Move u, pu, and gu up two levels. gu becomes new u. Continue rebalancing if necessary. If RB2 is satisfied, stop propagation. If gu is the root, forced gu to be black (The number of black nodes for all root-to-external-node paths increases by 1.) Otherwise, continue color change or rotation. ab c d gu pu u ab c d gu pu u

Resolution of Type XYb Rotate Same as the rotation schemes taken for an AVL tree. For example, when dealing with LLb rotation, abcd y x z ab c d gu pu u x z y LLb u

Resolution of Type XYb LRb Rotation: same as LR rotation of AVL tree. RRb and RLb are symmetric. y a b z cd x b c a d gp pp p y x z LRb u

Example 10.4 Insert 50, 10, 80, 90, 70, 60, 65, Insert 50 Insert Insert

Insert u pu gu d u pu gu d u pu gu d 80 This violates RB1

u pu gu d Insert Insert u pu

u pu gu d Insert pu u gu

Insert u pu gu d Insert u pu gu d 60

u pu gu d u

Deletion from a Red-Black Tree Delete as for unbalanced binary search tree. If red node deleted, no rebalancing needed. If black node deleted, a subtree becomes one black pointer (node) deficient.

Delete A Black Leaf Define y as the root of deficient subtree. py is parent of y py y

Delete A Black Node of Degree 1 Define y as the root of deficient subtree. py is parent of y py y

Delete A Black Node of Degree 2 Not possible, degree 2 nodes are never deleted (Why?)

Rebalancing Strategy py y

Define the root of deficient subtree y is root of deficient subtree. py is parent of y.