So far, we've learned about binary node tree with nodes and children namely left child and right child.
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
Now, I know how to create Binary Tree Node. But what do I do with this?
This tree also contains data so finding data that I want will be something I need to study. How do I find the data within the tree then? In order to find the specific data, I will have to go through each node in tree and verify if that is the correct one. Once I find the right one, I can return the result.
There are three ways to visiting noes:
- inorder
- preorder
- postorder
Inorder visit the left subtree inorder first, then node itself, the right subtree. Preorder, on the other hand, visit the node itself, then the left subtree preorder and the right subtree preorder. At last, postorder the left subtree postorder and the right subtree postorder and then visit the node itself.

<Inorder>

<Preorder>

<Postorder>
Ok, it's not pretty drawings but there's so much I can do with mouse and I'm not really good at drawing. But pictures are way easier to understand and remember this for me, so, for the sake of my memory, I am taking the risk of public embarrassment on the web. At least, I will never forget this.
No comments:
Post a Comment