This week we talked about binary search tree. It is a special kind tree in which every parent node has at most two children.
’’’Binary Tree node.’’’
def __init__(self, data, left=None, right=None):
’’’ (BTNode, object, BTNode, BTNode) -> NoneType
Create BTNode (self) with data
and children left and right.
’’’
self.data, self.left, self.right = data, left, right
We have different implementations for binary search tree which
seems not that difficult to understand since we can always refer to a graph if feeling confused. Many things told were similar to those
talked about in last week's lectures. The new stuffs introduced were about the travelling of trees.
There are 3 orders : inorder, preorder and postorder
1. inorder
from left to right and recursively
if root is None:
pass
else:
inorder_visit(root.left, perform)
perform(root)
inorder_visit(root.right, perform)
2.preorder
from the root to the leaves and recursively
perform(root)
preorder_visit(root.left, perform)
preorder_visit(root.right, perform)
3. postorder
from leaves to the root and recursively
preorder_visit(root.left, perform)
preorder_visit(root.right, perform)
perform(root)
No comments:
Post a Comment