This week we learned about Binary Trees. Binary tree is like a real tree, it has a root, and have children which are like the leaves on a tree. The binary tree node class has attributes value (which is the value of the current node), left children, and right children. I found the tree classes very useful in real everyday life.
The recursive functions work on the binary trees really well. There are three different ways to visit a tree. The first one is inorder, where you visit the left subtree inorder first, then visit the node itself, then visit the right subtree inorder. When visiting the left and right subtree inorder, it involves recursion, which we call the function on the left/right child of the node recursively. The second way is preorder, where you visit the node itself first, then the left subtree preorder, and then the right subtree preorder. Lasly, postorder, where you visit the left subtree postorder, then the right subtree postorder, and then the node itself.
No comments:
Post a Comment