The successor is the node that are next-largest. Because of the property of tree, we can conclude that there are three cases when look for an item.
- If the node has a right child, then the successor is the smallest key in the right subtree.
- If the node has no right child and is the left child of its parent, then the parent is the successor.
- If the node is the right child of its parent, and itself has no right child, then the successor to this node is the successor of its parent, excluding this node.
def _find_min(node: BTNode) -> BTNode:
'''Find and return minimal node, assume node is not None'''
return _find_min(node.left) if node.left else node
This is a part of the code from the class BTNode and details omitted. Since finding maximum has been discussed in the class, I tried out finding a minimum based on the lecture. Notice that the only the left children are checked, and the process is recursive.
There are more things that can be done using Binary Search Tree. So, understanding this will be a crucial when it comes to data structures.
No comments:
Post a Comment