5.1.14 -5.1.17 Binary trees



A binary tree is a tree data structures in which each node has at most two child nodes, usually distinguished as "left" and "right". Nodes with children are parent nodes, and child nodes may contain references to their parents. Outside the tree, there is often a reference to the "root" node (the ancestor of all nodes), if it exists. Any node in the data structure can be reached by starting at root node and repeatedly following references to either the left or right child. A tree which does not have any node other than root node is called a null tree. In a binary tree, a degree of every node is maximum two. A tree with n nodes has exactly n−1 branches or degree.




The order in which you would traverse a tree in preorder is:

Visit the root
Traverse to the left subtree until the leaf.
Traverse to the right subtree

The order in which you would traverse a tree in inorder is:

Traverse the left most subtree starting at the left external node.
Traverse towards the root but traverse into each right subtree.
Traverse the right subtree starting at the left external node, following step 2

The order in which you would traverse a tree in postorder is:

Traverse all the left external nodes starting with the left most subtree which is then followed by bubble-up all the internal nodes
Traverse the right subtree starting at the left external node which is then followed by bubble-up all the internal nodes
Visit the root