chi
03/18/2018, 9:44 PMfun deleteNode(root: BinaryTreeNode<T>?, value: T): BinaryTreeNode<T>? {
var troot: BinaryTreeNode<T> = root ?: return null
when {
troot.data > value -> troot.leftChild = deleteNode(troot.leftChild, value)
troot.data < value -> troot.rightChild = deleteNode(troot.rightChild, value)
else -> {
when {
troot.leftChild != null && troot.rightChild != null -> {
val temp = troot
val inOrderSuccessor = temp.rightChild?.getMinimum()
troot.data = inOrderSuccessor?.data!!
deleteNode(troot.rightChild, inOrderSuccessor.data)
}
troot.leftChild != null -> troot = troot.leftChild!!
troot.rightChild != null -> troot = troot.rightChild!!
else -> return null
}
}
}
return troot
}