Linked list and binary tree traversals


An Introduction to Trees Figure 1. In the case of a linked list, the references were used to keep the nodes organized into a list. In the case of a tree, the references are used to keep the nodes organized into a hierarchy. The root of the tree is the beginning. We begin with the root of the tree when we perform operations on the tree.

An empty tree a tree that has no nodes has no root. A tree that consists of only one node has exactly one node, the root. In the Figure 1 example above, A is the root. Each node, except the root has a parentthe node above it in the hierarchy. Nodes that have the same parent are called siblings. In general, nodes in trees can have any number of children. But trees where nodes have at most 2 children are particularly useful. We'll focus on this type of tree, called the binary tree.

If we recursively find the parent of a node, we will find all of its ancestors. For example, linked list and binary tree traversals the tree above, B and A are the ancestors of D.

The descendants of a node are its children, and all of their children, and so on. D and E are descendents of B. 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 linked list and binary tree traversals 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 treesuch as in Figure 1every leaf has the same depth, and every nonleaf has two children.

For a complete binary treeevery 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 linked list and binary tree traversals Post-order traversal In-order traversal It is easiest to describe the traversal of trees recursivelyalthough 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 Linked list and binary tree traversals 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 in-order traversal linked list and binary tree traversals the tree in Figure 2 is: Post-order Traversal In a post-order traversal, the linked list and binary tree traversals of the root is post poned until last in a nonempty tree: The post-order traversal of the tree in Figure 2 is: A binary tree of integers Consider the tree shown above, and answer the following questions.

Which is the root? Which are leaf nodes? What is the height or depth of the tree? What is the result of pre-order traversal? What is the result of in-order traversal? What is the result of post-order traversal?