19.06.2013 Views

Starting Out with C++: Early Objects - EEMB DERSLER

Starting Out with C++: Early Objects - EEMB DERSLER

Starting Out with C++: Early Objects - EEMB DERSLER

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

1120 Chapter 19 Binary Trees<br />

• Inorder traversal<br />

1. The node’s left subtree is traversed.<br />

2. The node’s data is processed.<br />

3. The node’s right subtree is traversed.<br />

• Preorder traversal<br />

1. The node’s data is processed.<br />

2. The node’s left subtree is traversed.<br />

3. The node’s right subtree is traversed.<br />

• Postorder traversal<br />

1. The node’s left subtree is traversed.<br />

2. The node’s right subtree is traversed.<br />

3. The node’s data is processed.<br />

The IntBinaryTree class can display all the values in the tree using all three of these algorithms.<br />

The algorithms are initiated by the following inline public member functions:<br />

void showInOrder(void)<br />

{ displayInOrder(root); }<br />

void showPreOrder()<br />

{ displayPreOrder(root); }<br />

void showPostOrder()<br />

{ displayPostOrder(root); }<br />

Each of the public member functions calls a recursive private member function and passes the<br />

root pointer as an argument. The recursive functions are very simple and straightforward:<br />

void IntBinaryTree::displayInOrder(TreeNode *tree)<br />

{<br />

if (tree)<br />

{<br />

displayInOrder(tree->left);<br />

cout value right);<br />

}<br />

}<br />

void IntBinaryTree::displayPreOrder(TreeNode *tree)<br />

{<br />

if (tree)<br />

{<br />

cout value left);<br />

displayPreOrder(tree->right);<br />

}<br />

}<br />

void IntBinaryTree::displayPostOrder(TreeNode *tree)<br />

{<br />

if (tree)<br />

{<br />

displayPostOrder(tree->left);<br />

displayPostOrder(tree->right);<br />

cout value

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!