Presentation is loading. Please wait.

Presentation is loading. Please wait.

Properties of BST delete We first do the normal BST deletion: – 0 children: just delete it – 1 child: delete it, connect child to parent – 2 children:

Similar presentations


Presentation on theme: "Properties of BST delete We first do the normal BST deletion: – 0 children: just delete it – 1 child: delete it, connect child to parent – 2 children:"— Presentation transcript:

1 Properties of BST delete We first do the normal BST deletion: – 0 children: just delete it – 1 child: delete it, connect child to parent – 2 children: put successor in your place, delete successor Which nodes’ heights may have changed: – 0 children: path from deleted node to root – 1 child: path from deleted node to root – 2 children: path from deleted successor to root Will rebalance as we return along the “path in question” to the root 1CSE332: Data Abstractions 20 92 155 12 7 10

2 Case #1 Left-left due to right deletion CSE332: Data Abstractions2 Start with some subtree where if right child Z becomes shorter we are unbalanced due to height of left-left grandchild X A delete in the right child Z could cause this right-side shortening h a Z Y b X h+1 h h+2 h+3

3 Case #1: Left-left due to right deletion CSE332: Data Abstractions3 h a Z Y b X h+1 h h+2 h+3 b ZY a h+1 h h+2 X h h+1 Same single rotation as when an insert in the left-left grandchild X caused imbalance due to X becoming taller But here the “height” at the top decreases, so more rebalancing farther up the tree might still be necessary

4 Case #2: Left-right due to right deletion a h-1 h h V U h+1 h+2 h+3 Z X b c h+1 h c X h-1 h+1 h V U h+2 Z a b h h+1 h Same double rotation when an insert in the left-right grandchild caused imbalance due to c becoming taller But here the “height” at the top decreases, so more rebalancing farther up the tree might still be necessary

5 No third right-deletion case needed So far we have handled these two cases: left-leftleft-right 5CSE332: Data Abstractions h a Z Y b X h+1 h h+2 h+3 a h-1 h h V U h+1 h+2 h+3 Z X b c h+1 h But what if the two left grandchildren X & Y are now both too tall (h+1)? Then it turns out left-left solution still works The children of the “new top node” will have heights differing by 1 instead of 0, but that’s fine

6 Mirror Image cases For each of the cases described above, we can also have the mirror image: deletion in the left side of the tree, and needing to compare the heights of the right grandchildren


Download ppt "Properties of BST delete We first do the normal BST deletion: – 0 children: just delete it – 1 child: delete it, connect child to parent – 2 children:"

Similar presentations


Ads by Google