Linked list and binary tree traversals
Nodes that do not have children are called leaf nodes or leaves. D, E, F, and G are leaves in the tree above. The height of a tree is the number of nodes along the longest path from the root to any leaf. Some trees are said to be full trees. These trees have all leaves at the same level.
The tree above is a full tree and its height is 3. The tree below is not full. Its height is 5, but the some nodes are at levels less than 5. A node's level is one more than the number of ancestors that it has. The previous two figures are examples of binary trees. In a full binary tree , such as in Figure 1 , every leaf has the same depth, and every nonleaf has two children. For a complete binary tree , every level except the deepest must contain as many nodes as possible; and at the deepest level, all nodes are as far left as possible.
Traversing Trees When we wanted to visit all of the nodes in a list, we could do it one of two ways -- forward or backward. Trees are a lot more interesting. We can visit the nodes in many different ways. We'll discuss three traversals: Pre-order traversal Post-order traversal In-order traversal It is easiest to describe the traversal of trees recursively , although an iterative traversal is possible and, in fact, more efficient.
For binary trees, each of these traversals does three things: Visits the root of the subtree Traverses the left subtree Traverses the right subtree The big difference among the traversals is the order in which each performs these operations.
Let's take a closer look. Pre-order Traversal The word "pre-order" refers to when the root is processed: So, a pre-order traversal has these three steps for a nonempty tree: Process the nodes in the left subtree with a recursive call. Process the nodes in the right subtree with a recursive call. The pre-order traversal of the tree in Figure 2 is: In-order Traversal In an in-order traversal, the root is processed in between the processing of its two subtrees.
Here are the three steps for a nonempty tree: The relationship between parent index and child index, allow us to move from the parent to child, as well as from the child to the parent.
Important note that the tree should be complete , what means for the specific height it should contain exactly elements. You can avoid such requirement by placing null instead of missing elements. The enumeration still applies for empty nodes as well. This should cover any possible binary tree. Such array representation for binary tree we will call Binary Tree Array. We always assume it will contain elements. There are two search algorithms exist for binary tree: BFS search nodes level by level, starting from the root node.
Then checking its children. Then children for children and so on. Until all nodes are processed or node which satisfies search condition is found. It checks all nodes from leftmost path from the root to the leaf, then jumps up and check right node and so on. For example, assume you need to find node with value 1 in the following tree. Yellow cell are cells which are tested in search algorithm before needed node found. Take into account that for some cases DFS require less nodes for processing.
For some cases it requires more. Breadth-first search for binary tree array is trivial. Since elements in binary tree array are placed level by level and bfs is also checking element level by level, bfs for binary tree array is just linear search. Using stack data structure it could be implemented the same way as for classic binary tree, just put indices into the stack. From this point recursion is not different at all, you just use implicit method call stack instead of data structure stack.
What if we could implement DFS without stack and recursion. In the end is just a selecting correct index on each step? As in BFS we start from the root index 0. If next left child produce index out of array bounds exception we need to stop doubling and jump to the right child.
The observation here is the number of elements in the tree of some height is two times larger and plus one than the tree with height - 1.