Binary Tree & Binary Search Tree (cont'd)
The property of Binary Search Tree (BST) (value of left child node < value of parent node < value of right node) makes the operations on the BST much more complicated than those on tree or BT because we must remain the property after any operation to keep the BST balanced. Now let's see two typical BST operations Insert and Delete that works on a simple BST like:
5
2 7
1 3 6 8
1. Insert a node.
Actually we can only add any new node as a leaf node because it's either larger than or less than the parent node rather than equal to the parent node. So it's impossible to insert a duplicate element. For example, if we want to insert 4, the insertion is like:
4 < 5 ---> 4 > 2 ---> 4 > 3 ---> 3.right = 4. The BST after the insertion is like:
5
2 7
1 3 6 8
4
Below is the recursive insert function.The base case is if the BST node is not existed. When the base is reached, a new BST node is established. But the difficulty of the BST insertion is to update the links between the new node and all its upper parent nodes (not just its direct parent node). So the core of the recursive function is to bring each parent node downward (current_bn = bn) until the base case is reached and the new node is established and then to update all the upper parent nodes (bn.left = or bn.right =) one by one until the root is reached.
def bst_insert_leaf(bn, item):
current_bn = bn
if not bn:
current_bn = BinaryNode(item)
elif item < bn.data:
bn.left = bst_insert_leaf(bn.left, item)
elif item > bn.data:
bn.right = bst_insert_leaf(bn.right, item)
else:
pass
return current_bn
2. Delete a node.
When trying to delete a BST node, we must take into account of 3 cases: (1). the node doesn't have children (root or leaf); (2). the node has one child; (3). the node has two children. Below is an iterative delete function that covers all the 3 cases. So the algorithm of the function is:
if node is not in BST,
return False
if node is in BST
if node doesn't have children,
if node is leaf,
parent node's left or right = None
if node is root,
BST's root = None
if node has one child,
if node is not root:
parent node' left or right = the node's child node (node.left or node.right)
if node is root,
BST's root = node's child
if node has two children,
find rightmost node in node's left sub-tree
node's value = rightmost node's value
if rightmost node is a leaf,
parent node's right = None
if rightmost node is not a leaf (i.e., it has a left child),
parent node's left = rightmost node's left child
Note, for the case 3, we use the rightmost node in the left sub-tree (called in-order predecessor ) to replace the deleted node's value but remain its links to the child nodes. Another option is the leftmost node in the right sub-tree (called in-order successor). For example, as to the BST above, in-order predecessor is 3 and, after deleting the root (5), the BST is like:
3
2 7
1 6 8
In-order successor is 6 and, after deleting the root (5), the BST is like:
6
2 7
1 3 8
With either the node, the BST property is satisfied after the deletion.
def bst_del(bn, item):
node_list = find_bst_node(bn, item)
parent_node, curr_node = node_list[0], node_list[1]
if not curr_node:
return False
else:
children_count = count_children(curr_node)
if children_count == 0:
if parent_node:
if parent_node.left == curr_node:
parent_node.left = None
else:
parent_node.right = None
else:
bn.data = None
elif children_count == 1:
new_child = curr_node.left if curr_node.left else curr_node.right
if parent_node:
if parent.left == curr_node:
parent.left = new_child
else:
parent.right = new_child
else:
bn.data = new_child.data
bn.left, bn.right = new_child.left, new_child.right
else:
temp_parent, rightmost = curr_node, curr_node.left
while rightmost.right:
temp_parent = rightmost
rightmost = rightmost.right
curr_node.data = rightmost.data
if temp_parent.right == rightmost:
temp_parent.right = rightmost.left
else:
temp_parent.left = rightmost.left
# help function
def find_bst_node(bn, item):
parent_node, curr_node = None, bn
while curr_node and curr_node.data != item:
if item < curr_node.data:
parent_node = curr_node
curr_node = parent_node.left
elif item > curr_node.data:
parent_node = curr_node
curr_node = parent_node.right
else:
pass
return [parent_node, curr_node]
def count_children(bn, count=0):
if bn.left:
count += 1
if bn.right:
count += 1
return count
End of this post
No comments:
Post a Comment