eliminate recursive solution to finding rightmost node

This commit is contained in:
David 2021-07-25 21:41:12 -04:00
parent 8f80698f50
commit f6aed92862

View File

@ -107,16 +107,17 @@ func (sbt *StringyBTreeNode) getNode(k string) *StringyBTreeNode {
return nil // not found return nil // not found
} }
func (sbt *StringyBTreeNode) rightmost(parent *StringyBTreeNode) (*StringyBTreeNode, *StringyBTreeNode) {
if sbt.right == nil {
return parent, sbt
}
return sbt.right.(*StringyBTreeNode).rightmost(sbt)
}
// swap the rightmost node to the node we're removing // swap the rightmost node to the node we're removing
func (sbt *StringyBTreeNode) Delete(k string) *StringyBTreeNode { func (sbt *StringyBTreeNode) Delete(k string) *StringyBTreeNode {
parent, rightmost := sbt.rightmost(nil) parent := sbt
rightmost := sbt
for {
if rightmost.right == nil {
break
} else {
parent, rightmost = rightmost, parent.right.(*StringyBTreeNode)
}
}
delete := sbt.getNode(k) delete := sbt.getNode(k)
delete.k = rightmost.k delete.k = rightmost.k
delete.v = rightmost.v delete.v = rightmost.v