Linked lists, stacks and queues are linear data structures. A tree is a nonlinear, two-dimensional data structure. Tree nodes contain two or more links. This section discusses binary trees (Fig. 19.18)—trees whose nodes all contain two links (none, one or both of which may have the value nullptr
).
For this discussion, refer to nodes A
, B
, C
and D
in Fig. 19.18. The root node (node B
) is the first node in a tree. Each link in the root node refers to a child (nodes A
and D
). The left child (node A
) is the root node of the left subtree (which contains only node A
), and the right child (node D
) is the root node of the right subtree (which contains nodes D
and C
). The children of a given node are called siblings (e.g., nodes A
and D
are siblings). A node with no children is a leaf node (e.g., nodes A
and C
are leaf nodes). Computer scientists normally draw trees from the root node down—the opposite of how trees grow in nature.
A binary search tree (with no duplicate node values) has the characteristic that the values in any left subtree are less than the value in its parent node, and the values in any right subtree are greater than the value in its parent node. Figure 19.19 illustrates a binary search tree with 9 values. Note that the shape of the binary search tree that corresponds to a set of data can vary, depending on the order in which the values are inserted into the tree.
The program of Figs. 19.20–19.22 creates a binary search tree and traverses it (i.e., walks through all its nodes) three ways—using recursive inorder, preorder and postorder traversals. We explain these traversal algorithms shortly.
Tree
Class TemplateWe begin our discussion with the driver program (Fig. 19.20), then continue with the implementations of classes TreeNode
(Fig. 19.21) and Tree
(Fig. 19.22). Function main
(Fig. 19.20) begins by instantiating integer tree intTree
of type Tree<int>
(line 9). The program prompts for 10 integers, each of which is inserted in the binary tree by calling insertNode
(line 17). The program then performs preorder, inorder and postorder traversals (these are explained shortly) of intTree
(lines 21, 24 and 27, respectively). Next, we instantiate floating-point tree doubleTree
of type Tree<double>
(line 29), then prompt for 10 double
values, each of which is inserted in the binary tree by calling insertNode
(line 38). Finally, we perform preorder, inorder and postorder traversals of doubleTree
(lines 42, 45 and 48, respectively).
TreeNode
The TreeNode
class template (Fig. 19.21) definition declares Tree<NODETYPE>
as its friend
(line 12). This makes all member functions of a given specialization of class template Tree
(Fig. 19.22) friend
s of the corresponding specialization of class template TreeNode
, so they can access the private
members of TreeNode
objects of that type. Because the TreeNode
template parameter NODETYPE
is used as the template argument for Tree
in the friend
declaration, TreeNode
s specialized with a particular type can be processed only by a Tree
specialized with the same type (e.g., a Tree
of int
values manages TreeNode
objects that store int
values).
Lines 20–22 declare a TreeNode
’s private
data—the node’s data
value, and pointers leftPtr
(to the node’s left subtree) and rightPtr
(to the node’s right subtree). Both pointers are initialized to nullptr
—thus initializing this node to be a leaf node. The constructor (line 15) sets data
to the value supplied as a constructor argument. Member function getData
(line 18) returns the data
value.
Tree
Class template Tree
(Fig. 19.22) has as private
data rootPtr
(line 33), a pointer to the tree’s root node that’s initialized to nullptr
to indicate an empty tree. The class’s public
member functions are insertNode
(lines 13–15) that inserts a new node in the tree and preOrderTraversal
(lines 18–20), inOrderTraversal
(lines 23–25) and postOrderTraversal
(lines 28–30), each of which walks the tree in the designated manner. Each of these member functions calls its own recursive utility function to perform the appropriate operations on the internal representation of the tree, so the program is not required to access the underlying private
data to perform these functions. Remember that the recursion requires us to pass in a pointer that represents the next subtree to process.
Tree
Member Function insertNodeHelper
The Tree
class’s utility function insertNodeHelper
(lines 37–58) is called by insertNode
(lines 13–15) to recursively insert a node into the tree. A node can only be inserted as a leaf node in a binary search tree. If the tree is empty, a new TreeNode
is created, initialized and inserted in the tree (lines 40–42).
If the tree is not empty, the program compares the value to be inserted with the data
value in the root node. If the insert value is smaller (line 45), the program recursively calls insertNodeHelper
(line 46) to insert the value in the left subtree. If the insert value is larger (line 50), the program recursively calls insertNodeHelper
(line 51) to insert the value in the right subtree. If the value to be inserted is identical to the data value in the root node, the program prints the message " dup"
(line 54) and returns without inserting the duplicate value into the tree. Note that insertNode
passes the address of rootPtr
to insertNodeHelper
(line 14) so it can modify the value stored in rootPtr
(i.e., the address of the root node). To receive a pointer to rootPtr
(which is also a pointer), insertNodeHelper
’s first argument is declared as a pointer to a pointer to a TreeNode
.
Tree
Traversal FunctionsMember functions preOrderTraversal
(lines 18–20), inOrderTraversal
(lines 23–25) and postOrderTraversal
(lines 28–30) traverse the tree and print the node values. For the purpose of the following discussion, we use the binary search tree in Fig. 19.23.
Function inOrderTraversal
invokes utility function inOrderHelper
(lines 70–76) to perform the inorder traversal of the binary tree. The steps for an inorder traversal are:
Traverse the left subtree with an inorder traversal. (This is performed by the call to inOrderHelper
at line 72.)
Process the value in the node—i.e., print the node value (line 73).
Traverse the right subtree with an inorder traversal. (This is performed by the call to inOrderHelper
at line 74.)
The value in a node is not processed until the values in its left subtree are processed, because each call to inOrderHelper
immediately calls inOrderHelper
again with the pointer to the left subtree. The inorder traversal of the tree in Fig. 19.23 is
6 13 17 27 33 42 48
The inorder traversal of a binary search tree prints the node values in ascending order. The process of creating a binary search tree actually sorts the data—thus, this process is called the binary tree sort.
Function preOrderTraversal
invokes utility function preOrderHelper
(lines 61–67) to perform the preorder traversal of the binary tree. The steps for a preorder traversal are:
Process the value in the node (line 63).
Traverse the left subtree with a preorder traversal. (This is performed by the call to preOrderHelper
at line 64.)
Traverse the right subtree with a preorder traversal. (This is performed by the call to preOrderHelper
at line 65.)
The value in each node is processed as the node is visited. After the value in a given node is processed, the values in the left subtree are processed. Then the values in the right subtree are processed. The preorder traversal of the tree in Fig. 19.23 is
27 13 6 17 42 33 48
Function postOrderTraversal
invokes utility function postOrderHelper
(lines 79–85) to perform the postorder traversal of the binary tree. The steps for a postorder traversal are:
Traverse the left subtree with a postorder traversal. (This is performed by the call to postOrderHelper
at line 81.)
Traverse the right subtree with a postorder traversal. (This is performed by the call to postOrderHelper
at line 82.)
Process the value in the node (line 83).
The value in each node is not printed until the values of its children are printed. The postOrderTraversal
of the tree in Fig. 19.23 is
6 17 13 33 48 42 27
The binary search tree facilitates duplicate elimination. As the tree is being created, an attempt to insert a duplicate value will be recognized, because a duplicate will follow the same “go left” or “go right” decisions on each comparison as the original value did when it was inserted in the tree. Thus, the duplicate will eventually be compared with a node containing the same value. The duplicate value may be discarded at this point.
Searching a binary tree for a value that matches a key is also fast. If the tree is balanced, then each branch contains about half the nodes in the tree. Each comparison of a node to the search key eliminates half the nodes. This is called an O(log n) algorithm (Big O notation is discussed in Chapter 20). So a binary search tree with n elements would require a maximum of comparisons either to find a match or to determine that no match exists. For example, searching a (balanced) 1000-element binary search tree requires no more than 10 comparisons, because . Searching a (balanced) 1,000,000-element binary search tree requires no more than 20 comparisons, because .
In the exercises, algorithms are presented for several other binary tree operations such as deleting an item from a binary tree, printing a binary tree in a two-dimensional tree format and performing a level-order traversal of a binary tree. The level-order traversal of a binary tree visits the nodes of the tree row by row, starting at the root node level. On each level of the tree, the nodes are visited from left to right. Other binary tree exercises include allowing a binary search tree to contain duplicate values, inserting string values in a binary tree and determining how many levels are contained in a binary tree.