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 ID: 759205
Download Presentation The PPT/PDF document "Properties of BST delete" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Properties of BST delete
We first do the normal BST deletion:0 children: just delete it1 child: delete it, connect child to parent2 children: put successor in your place, delete successorWhich nodes’ heights may have changed:0 children: path from deleted node to root1 child: path from deleted node to root2 children: path from deleted successor to rootWill rebalance as we return along the “path in question” to the root
1
CSE332: Data Abstractions
20
9
2
15
5
12
7
10
Slide2Case #1 Left-left due to right deletion
CSE332: Data Abstractions
2
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+1
h+2
h+3
Slide3Case #1: Left-left due to right deletion
CSE332: Data Abstractions
3
h
a
Z
Y
b
X
h+1
h
h+1
h+2
h+3
b
Z
Y
a
h+1
h
h+1
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
Slide4Case #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
h+1
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
Slide5No third right-deletion case needed
So far we have handled these two cases:left-left left-right
5
CSE332: Data Abstractions
h
a
Z
Y
b
X
h+1
h
h+1
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
Slide6Mirror 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