9781423902225_IM_ch20

advertisement
C++ Programming: Program Design Including Data Structures, Fourth Edition
Chapter 20
Binary Trees
At a Glance
Instructor’s Manual Table of Contents

Overview

Objectives

Teaching Tips

Quick Quizzes

Class Discussion Topics

Additional Projects

Additional Resources

Key Terms
20-1
C++ Programming: Program Design Including Data Structures, Fourth Edition
20-2
Lecture Notes
Overview
Chapter 20 discusses how to organize data dynamically so that item insertion, deletion,
and lookups are more efficient. Students will become familiar with the structure of both
binary trees and binary search trees. Students will also learn how to implement
commonly used recursive algorithms for tree traversal, tree copy, search, insertion, and
deletion. In addition, algorithms for determining specific characteristics of binary trees
such as height, number of nodes, and number of leaves will be discussed. The chapter
concludes with a discussion of nonrecursive versions of some of the algorithms
presented in the chapter.
Chapter Objectives





Learn about binary trees
Explore various binary tree traversal algorithms
Learn how to organize data in a binary search tree
Learn how to insert and delete items in a binary search tree
Explore nonrecursive binary tree traversal algorithms
Teaching Tips
Binary Trees
1. Define the following terms and introduce related notation. Recall that some of these
terms were introduced in Chapter 19.














binary tree (T)
root
left subtree (LT)
right subtree (RT)
left child
right child
parent
directed edge
directed branch (branch)
leaf
path
length
level
height
2. Use Examples 20-1 through 20-6 and the accompanying figures to illustrate each term.
C++ Programming: Program Design Including Data Structures, Fourth Edition
20-3
3. Discuss empty trees and the structure of each node in a binary tree. Explain the
approaches to determining various characteristics of binary trees, such as counting the
number of nodes, counting the number of leaf nodes, determining the height of the tree,
etc.
Teaching
Tip
After reviewing node structure and the recursive algorithms presented in this
section, ask your students to think about how they might implement a binary tree
using arrays. Would it be difficult to code? What would the overhead be?
Copy Tree
1. Discuss the difference between a shallow copy of a binary tree and an identical copy of
a binary tree.
2. Discuss the copyTree function presented on page 1260.
Teaching
Tip
Discuss how to determine when a shallow copy of a binary tree might be
sufficient and when a deep (identical) copy is necessary.
Quick Quiz 1
1. Define a directed branch.
Answer: A directed branch is an arrow from a parent node to a child node; it is also
called a directed edge or branch
2. Each node in a binary tree can have at most ____ links.
Answer: two
3. True or False: A shallow copy tree has the same number of nodes as the original binary
tree.
Answer: False
Binary Tree Traversal
1. Discuss why traversal is an important operation on binary trees and what “visiting a
node” means.
2. Discuss the three commonly used traversal algorithms: inorder traversal, preorder
traversal, and postorder traversal. Use Figure 20-9 to illustrate each algorithm and the
associated output sequence.
3. Using the coding examples in this section, discuss the implementation of each traversal
algorithm.
C++ Programming: Program Design Including Data Structures, Fourth Edition
20-4
Teaching
Tip
Ask your students if all three traversal algorithms would be equally useful for
making a copy of a binary tree and for deleting all the nodes in a binary tree.
Teaching
Tip
Ask your students if they can think of any other way to traverse a tree, and
introduce the idea of a level-by-level (breadth-first) traversal.
Implementing Binary Trees
1. Review the operations typically performed on a binary tree:









Determine whether the binary tree is empty.
Search the binary tree for a particular item.
Insert an item in the binary tree.
Delete an item from the binary tree.
Find the height of the binary tree.
Find the number of nodes in the binary tree.
Find the number of leaves in the binary tree.
Traverse the binary tree.
Copy the binary tree.
2. Using the code on pages 1265 through 1268, discuss the class that defines binary trees
as an ADT.
3. Using the code on pages 1268 through 1272, discuss the definitions of the nonabstract
member functions of the class binaryTreeType.
Teaching
Tip
Ask your students what can happen when a binary tree (a class object) is passed
by value into a function. Is the result a shallow or deep copy?
Teaching
Tip
Ask your students to discuss the advantages of overloading the assignment
operator. Are there any disadvantages?
Binary Search Trees
1. Discuss the efficiency of searching an arbitrary binary tree.
C++ Programming: Program Design Including Data Structures, Fourth Edition
Teaching
Tip
20-5
Ask your students to compare searching an arbitrary binary search tree with
searching other types of data structures such as an arbitrary linked list or an
array.
2. Define a binary search tree, using Figure 20-11 to illustrate the definition.
3. Review the operations typically performed on a binary search tree, and mention the
advantages of performing each on a binary search tree as opposed to an arbitrary binary
tree.









Teaching
Tip
Determine whether the binary search tree is empty.
Search the binary search tree for a particular item.
Insert an item in the binary search tree.
Delete an item from the binary search tree.
Find the height of the binary search tree.
Find the number of nodes in the binary search tree.
Find the number of leaves in the binary search tree.
Traverse the binary search tree.
Copy the binary search tree.
Ask your students why searching a binary search tree is efficient. Under what
circumstances will all of the nodes be visited during a search?
4. Use the code on pages 1275 through 1276 to discuss how inheritance can be used to
extend the definition of a binary tree to define a binary search tree.
5. Discuss the search function using the code on pages 1276 through 1277.
6. Discuss the insert function using the code on pages 1277 through 1279.
7. Discuss the delete function using Figures 20-12 through 20-17 and the code on pages
1285 through 1287.
Teaching
Tip
Ask your students what would happen if the extra pointers used in the search,
insert, and delete functions were omitted.
C++ Programming: Program Design Including Data Structures, Fourth Edition
20-6
Teaching
Tip
Ask your students to discuss the types of incorrect outputs they might see after
running their own programs. How would they go about determining the location
of the coding errors? Are there code segments (or specific functions) that should
be examined first for certain kinds of errors?
Teaching
Tip
Consider the problem of updating data in a binary search tree. Under what
circumstances would you want to create another binary search tree in which the
data in each node is the updated data of the original binary search tree – and not
destroy the old binary search tree?
Binary Search Tree: Analysis
1. Discuss the relationship of the shape of the binary search tree to the efficiency of the
search algorithm.
2. Discuss the worst case and average case for a linear binary search tree.
3. Discuss the average-case behavior for a binary search tree of undetermined shape.
Teaching
Tip
Ask your students to think about the best case of the search algorithm for a
binary search tree. What is the shape of the tree?
Teaching
Tip
Ask your students if the number of leaf nodes (and/or levels) in the binary tree
significantly impact the efficiency of the search algorithm.
Quick Quiz 2
1. What are the three primary steps in the preorder traversal algorithm?
Answer: (1) visit the node, (2) traverse the left subtree, and (3) traverse the right subtree
2. Both the average number of nodes visited and the number of key comparisons in a
search of a binary search tree is O(____).
Answer: log2n
3. Explain the difference between a binary tree and a binary search tree.
Answer: The nodes of a binary tree may not be in sorted order, whereas the nodes of a
binary search tree are in sorted order.
C++ Programming: Program Design Including Data Structures, Fourth Edition
20-7
Nonrecursive Binary Tree Traversal Algorithms
1. Discuss the importance of the traversal operation. Explain why nonrecursive algorithms
are important.
Nonrecursive Inorder Traversal
1. Discuss the nonrecursive, inorder traversal algorithm.
Teaching
Tip
Discuss why a stack is a useful data structure when performing a nonrecursive
inorder traversal of a binary tree.
Nonrecursive Preorder Traversal
1. Discuss the nonrecursive, preorder traversal algorithm.
Nonrecursive Postorder Traversal
1. Discuss the nonrecursive, postorder traversal algorithm.
Teaching
Tip
What values are stored on the stack for the three nonrecursive traversal
algorithms?
Binary Tree Traversal and Functions as Parameters
1. Discuss passing a function as a parameter. Include a discussion of the type of functions
that might be passed into a traversal function.
2. Using the code on page 1293, discuss the syntax of passing a function as a parameter.
3. Trace through the sample run for Example 20-7.
4. Step through the “Video Store” Programming Example to consolidate the concepts
presented in this chapter. Encourage students to compile and run this program on their
own.
Class Discussion Topics
1. What would the structure of a tree node look like if it was to contain information for an
individual employee found in an employee database? What would the key field be?
Should the binary search tree be sorted on the key field? Is there a limit to how many
fields you would want to “carry around” in each node?
C++ Programming: Program Design Including Data Structures, Fourth Edition
20-8
2. Discuss approaches to balance a linear binary tree. In other words, how would you
create a tree for which the number of nodes in the left subtree is approximately equal to
the number of nodes in the right subtree?
3. Consider an employee database that is stored as a binary search tree. Each employee
record contains two fields: an ID number field (which contains a six-digit, unique
identifier assigned to each employee) and a salary field. How would the node structure
and search algorithms need to be modified to print out a list of employee IDs and their
associated salaries in increasing order by salary?
Additional Projects
1. Write a program that can handle duplicate nodes in a binary search tree as follows:
include a field in the structure of each node that will contain a count of the number of
occurrences of a particular value. Increment the count by one when inserting a value
that is already in the tree. Decrement the count by one when deleting a value in the tree
for which the count is greater than one. Be sure to handle the case where the count is
decremented to zero. Print out the inorder sequence after each insertion and deletion.
When a node has a count greater than one, print the node “count” number of times. For
example, an inorder sequence may look like: A B B C D D D E.
2. Write a program that can search for an item in a binary search tree and print out the
value of the search item and the value of its parent node. This program will require you
to include a third pointer field in the structure for each node. The third field will be a
pointer to a node’s parent. Note that for the root node, you will need to set the third field
to the root node.
Additional Resources
1. Binary Tree:
www.cprogramming.com/tutorial/lesson18.html
2. Binary Search Tree:
http://en.wikipedia.org/wiki/Binary_search_tree
http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree
3. Binary Trees and Huffman Encoding:
http://cprogramming.com/tutorial/computersciencetheory/huffman.html
4. AVL Trees:
http://en.wikipedia.org/wiki/Avl_tree
5. Red Black Trees:
http://en.wikipedia.org/wiki/Red_black_tree
C++ Programming: Program Design Including Data Structures, Fourth Edition
20-9
Key Terms
 AVL-tree: also called a height-balanced tree; a binary search tree such that: (i) the
height of the left and right subtrees of the root differ by at most 1, and (ii) the left and
right subtrees of the root are AVL trees
 binary tree: a binary tree, T, is either empty or such that: (i) T has a special node called
the root node; (ii) T has two sets of nodes, LT and RT, called the left subtree and right
subtree of T, respectively; and (iii) LT and RT are binary trees
 binary search tree: a binary search tree, T, is either empty or: (i) T has a special node
called the root node; (ii) T has two sets of nodes, LT and RT, called the left subtree and
right subtree of T, respectively; (iii) the key in the root node is larger than every key in
the left subtree and smaller than every key in the right subtree; and (iv) LT and RT are
binary search trees
 branch: an arrow from a parent node to a child node; also called a directed edge or
directed branch
 breadth first traversal: a type of tree traversal that visits the nodes level-by-level
 directed branch: an arrow from a parent node to a child node; also called a directed
edge or branch
 directed edge: an arrow from a parent node to a child node. Also called a directed
branch or branch.
 height: the number of nodes on the longest path from the root to a leaf in a binary tree.
 height-balanced tree: also called an AVL-tree; a binary search tree such that: (i) the
height of the left and right subtrees of the root differ by at most 1, and (ii) the left and
right subtrees of the root are AVL trees
 inorder sequence: the listing of the nodes produced by the inorder traversal of a binary
tree
 leaf: a node in a binary tree that has no left or right children
 left child: for T, a binary tree with the root node A, B is called the left child of A if B is
the root node of the left subtree of A
 left subtree: a set of nodes in a binary tree, LT, that are also a binary tree
 length: the number of branches on a path in a binary tree
 level: the number of branches on the path from the root to a node in a binary tree
 level-by-level: a type of tree traversal that visits the nodes level-by-level; also called
breadth first traversal
 parent: for U and V, two nodes in the binary tree T, U is called the parent of V if there
is a branch from U to V
 path: a path from a node X to a node Y in a binary tree is a sequence of nodes X0, X1, . .
., Xn such that: (i) X = X0, Xn = Y, (ii) Xi-1 is the parent of Xi for all i = 1, 2, . . ., n; that is,
there is a branch from X0 to X1, X1 to X2, . . ., Xi-1 to Xi, . . ., Xn-1 to Xn. If X0, X1, . . ., Xn
is a path from node X to node Y, sometimes it is denoted by X = X0 - X1 - … - Xn-1 - Xn =
Y or simply X - X1 - … - Xn-1 - Y
 postorder sequence: the listing of the nodes produced by the postorder traversal
 preorder sequence: the listing of the nodes produced by the preorder traversal
 right child: for T, a binary tree with the root node A, C is called the right child of A if C
is the root node of the right subtree of A
 right subtree: a set of nodes in a binary tree, RT, that are also a binary tree
 root: a special node at the top of a binary tree
 traversal: an operation in which each node of a binary tree is visited
Download