We revisited binary search tree (BST) and
implemented new functions that allowed us to mutate its nodes by adding or
removing wanted nodes to the given BST. Though I did not have much trouble
comprehending/implementing insert function, delete function was very difficult.
For insert function, all we really have to
do was to add a wanted node on a left side of BST (or left child) if its data
(or node.data) is smaller than node.data, otherwise on a right tree. On the
other hand, we need to consider more cases to implement delete function.
Firstly, we need to check whether value to be deleted exists on the tree. If
the value does exist, we need to check if it’s larger or smaller than a node’s
value and send it down to appropriate side of tree. In cases where node to be
removed has only one of either left or right child, the existing child will
take the place of removed node. However,
if the node to be removed has both children that are not None, node with
maximum value from the left subtree will take its place, and this maximum value
node will be removed from its original place.
No comments:
Post a Comment