CHAPTER 14 EXERCISES 517 Group to define a (draughts) board where under program
ID: 3661746 • Letter: C
Question
CHAPTER 14 EXERCISES 517 Group to define a (draughts) board where under program control Pieces can be moved 10. can without dow that looks as a window as you bel, make it heroic efforts. It should much like corners, a la- control icons. Maybe have rounded need not you could add some fake "contents: such actually as an image. do anything, t is acceptable (and indeed recommended) to have it appear within a simple window. 11. Define a Binary tree class from Sha Give the number of levels ter (levels pe. a0 means no nodes, levels 1 means one node. as a paramet top 2 means one top node with two sub nodes, levels means one node with two sub nodes each with two sub nodes, etc). Let a be represented b a small circle. Connect the nodes lines as tional. PS. In computer science, trees grow downward from a top node (amusingly, but logically, often called the 12. Modify Binary tree to draw its nodes using a virtual function. Then, derive a new class from Binary tree that overrides that virtual function to use a different representation for a node (e.g a triangle) 13. Modify Binary tree to take a parameter (or parameters) to kind of line to use to connect the nodes (e.g., an arrow pointing down or a red arrow pointing up). Note how this exercise and the last use two alternative ways of making a class hierarchy more flexible and useful on to Binary. tree that adds text to a node. You may 14. Add an operati to implement this Choose to modify the design of Binary tree string ple, you give a a way to identify a node; for examp a binary tree (the root navigating left, right, right, left, and right down node would match both init an initial r Define a class an with graphics. 15. Most class hierarchies have nothing to do that a Iterator with a pure virtual function next0 Chapter 17). Now derive Vector iterator and List iterator from Iterator so that next0 for a Vector iterator yields a pointer to the next element erator does the same for a and the first call of a Vector iterator with a there is no next ele- You initialize a first element, if any. If (iterator&) to print of nexto yields a pointer to its function void print level into ment, 0. Test this by using a a son0, ofi0, should be a the elements of a vector and functions set or 16. Define a class Controller with virtual Controller. One to on and show0. Derive at least four from the class is set some- two classes whether should where show0 prints out derived class is up off and what is the current level. The nd class. seco how control the line color a shape such Controller to control with to you. Try to find a third "thing"Explanation / Answer
//Definition of the Node
template <class elemType>
struct nodeType
{
elemType information;
nodeType *leftLink;
nodeType *rightLink;
};
//Definition of the class
template <class elemType>
class binaryTree
{
public:
const binaryTree <elemType> & operator= (const binaryTree <elemType> &);
//Overload the assignment operator.
bool isEmpty() const;
void postorderTravel() const;
int Height() const;
int NodeCount() const;
int LeavesCount() const;
virtual void insertNode(const elemType& Item) = 0;
BinaryTree();
-BinaryTree();
protected:
nodeType <elemType> * rootNode;
void postorder(nodeType <elemType> *p) const;
int height1(nodeType<elemType> *p) const;
int nodeCount1(nodeType<elemType> *p) const;
int leavesCount1(nodeType<elemType> *p) const;
};
template class <elemType>
bool binaryTree<elemType>::isEmpty() const
{
return (rootNode == NULL);
}
template class <elemType>
void binaryTree<elemType> :: postorderTraversal() const
{
postorder(rootNode);
}
template class <elemType>
int binaryTree<elemType> :: Height() const
{
return height1(rootNode);
}
template class <elemType>
int binaryTree :: NodeCount() const
{
return NodeCount1(rootNode);
}
template class<elemType>
int binaryTree<elemType> :: LeavesCount() const
{
return leavesCount1(root);
}
template class<elemType>
void binaryTree <elemType>:: postorder (nodeType <elemType> *p) const
{
if (p != NULL)
{
postorder(p->leftLink);
postorder(p->rightLink);
cout << p->information << " ";
}
}
template class<elemType>
int binaryTree<elemType> :: height1 (nodeType <elemType>*p) const
{
if (p == NULL)
return 0;
else return 1 + max(height1(p->leftLink), height1(p->rightLink));
}
template class<elemType>
binaryTree<elemType> :: ~binaryTree()
{
destroy(rootNode);
}
template class<elemType>
void binaryTree<elemType> :: insertNode (const elemType& insertItem)
{
nodeType<elemType> *current;
nodeType<elemType> *trailercurrent;
nodeType<elemType> *newNode;
newNode = new nodeType<elemType>;
newNode->information = Item;
newNode->leftlLink = NULL;
newNode->rightLink = NULL;
if (rootNode == NULL)
rootNode = newNode;
else
{
current = rootNode;
while (current != NULL)
{
trailerCurrent = current;
if (current->information == Item)
{
cout << "Already Existing Item ";
return;
}
else if (current->information > Item)
current = current->leftLink;
else
current = current->rightLink;
}
if (trailerCurrent->information > Item)
trailerCurrent->leftLink = newNode;
else trailerCurrent->rightLink = newNode;
}
}