Unformatted Attachment Preview
Section 1
11. Write a program to do the following:
a. Build a binary search tree, T1.
b. Do a postorder traversal of T1 and while doing the postorder traversal, insert the nodes
into a second binary search tree T2.
c. Do a preorder traversal of T2 and while doing the preorder traversal, insert the node
into a third binary search tree T3.
d. Do an inorder traversal of T3.
e. Output the heights and the number of leafs in each of the three binary search trees.
12. Write the definitions of the functions of the class videoBinaryTree not given in the Programming
Example Video Store. Also write a program to test the video store program.
13. (Video Store Program) In Programming Exercise 14 in Chapter 5, you were asked to design and implement
a class to maintain customer data in a linked list. Because the search on a linked list is sequential and,
therefore, can be time consuming, design and implement the class customerBTreeType so that this
customer data can be stored in a binary search tree. The class customerBTreeType must be derived from
the class bSearchTreeType as designed in this chapter.
14. (Video Store Program) Using classes to implement the video data, video list data, customer data, and
customer list data, as designed in this chapter and in Programming Exercises 12 and 13, design and
complete the program to put the video store into operation.
Section 2
2. Write a program that outputs the nodes of a graph in a breadth-first traversal.
6. Write a program to test the breadth-first topological ordering algorithm.
8. Write a program to implement Fleury’s algorithm as described in this chapter.
Section 3
10. Suppose that intList is a vector container and:
intList = {2, 4, 6, 8, 10, 12, 14, 16}
What is the value of result after the following statement executes? result = accumulate(intList.begin(),
intList.end(), 0);
Breadth-First Traversal
The breadth-first traversal of a graph is similar to traversing a binary tree level-by-level (the nodes at each level are
visited from left to right). All the nodes at any level, i, are visited before visiting the nodes at level i + 1.
The breadth-first ordering of the vertices of the graph G3 in Figure 12-7 is as follows:
0 1 5 2 3 6 4 8 10 7 9
For the graph G3, we start the breadth traversal at vertex 0. After visiting the vertex 0, we visit the vertices that are
directly connected to it and are not visited, which are 1 and 5.
Next, we visit the vertices that are directly connected to 1 and are not visited, which are 2 and 3. After this, we visit the
vertices that are directly connected to 5 and are not visited, which is 6. After this, we visit the vertices that are directly
connected to 2 and are not visited, and so on.
As in the case of the depth-first traversal, because it might not be possible to traverse the entire graph from a single
vertex, the breadth-first traversal also traverses the graph from each vertex that is not visited. Starting at the first
vertex, the graph is traversed as much as possible; we then go to the next vertex that has not been visited. To
implement the breadth-first search algorithm, we use a queue. The general algorithm is as follows:
1. For each vertex v in the graph if v is not visited add v to the queue //start the breadth first search at v
2. Mark v as visited
3. While the queue is not empty
3.1. Remove vertex u from the queue
3.2. Retrieve the vertices adjacent to u
3.3. For each vertex w that is adjacent to u if w is not visited
3.3.1. Add w to the queue
3.3.2. Mark w as visited
The following C++ function, breadthFirstTraversal, implements this algorithm:
void graphType::breadthFirstTraversal()
{
linkedQueueType queue;
bool *visited;
visited = new bool[gSize];
for (int ind = 0; ind < gSize; ind++)
visited[ind] = false; //initialize the array
//visited to false
linkedListIterator graphIt;
for (int index = 0; index < gSize; index++)
if (!visited[index])
{
queue.addQueue(index);
visited[index] = true;
cout children[location];
}
return found;
} //end search
14. (Programming Example Video Store)
a. Complete the design and implementation of the class customerType defined in the
Programming Example Video Store.
b. Design and implement the class customerListType to create and maintain a list of customers
for the video store.
#include
#include
#include
using namespace std;
class Node
{
string data;
Node *nextNode;
public:
Node()
{
data = "\0";
nextNode = NULL;
}
void setData(string d)
{
data = d;
}
void setNext(Node *n)
{
nextNode = n;
}
string getData()
{
return data;
}
Node* getNext()
{
return nextNode;
}
};
// class Node
// Constructor
// funtion to set value of node
// funtion to set address of next node
// funtion to get value of node
// funtion to get address of next node
class personType
{
string firstName;
string lastName;
public:
personType();
personType(string first, string last);
void print() const;
void setName(string first, string last);
string getFName() const;
string getLName() const;
};
personType::personType()
{
firstName = lastName = "\0";
}
personType::personType(string first, string last)
{
firstName = first;
lastName = last;
}
void personType::print() const
{
cout