Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Data Structures C++ A node\'s level is the number of parent\'s nodes from the ro

ID: 3839846 • Letter: D

Question

Data Structures C++

A node's level is the number of parent's nodes from the root to node. The root is always at level 0. The root's children are a level 1 and so on. A level's width is the number of nodes at the level. A tree's width the greatest width of all of a tree's levels' width. Add iterative functions to the binaryTree class to compute a tree's width.

BINARYSEARCHTREE.H

//Header File Binary Search Tree

#ifndef H_binarySearchTree
#define H_binarySearchTree
#include <iostream>
#include <cassert>
#include "binaryTree.h"

//*************************************************************
// Author: D.S. Malik
//
// This class specifies the basic operations to implement a
// binary search tree.
//*************************************************************

using namespace std;

template <class elemType>
class bSearchTreeType: public binaryTreeType<elemType>
{
public:
bool search(const elemType& searchItem) const;
//Function to determine if searchItem is in the binary
//search tree.
//Postcondition: Returns true if searchItem is found in the
// binary search tree; otherwise, returns false.

void insert(const elemType& insertItem);
//Function to insert insertItem in the binary search tree.
//Postcondition: If no node in the binary search tree has the
// same info as insertItem, a node with the info insertItem
// is created and inserted in the binary search tree.

void deleteNode(const elemType& deleteItem);
//Function to delete deleteItem from the binary search tree
//Postcondition: If a node with the same info as deleteItem
// is found, it is deleted from the binary search tree

private:
void deleteFromTree(binaryTreeNode<elemType>* &p);
//Function to delete the node to which p points is deleted
//from the binary search tree.
//Postcondition: The node to which p points is deleted from
// the binary search tree.
};


template <class elemType>
bool bSearchTreeType<elemType>::
search(const elemType& searchItem) const
{
binaryTreeNode<elemType> *current;
bool found = false;

if (root == NULL)
cerr << "Cannot search the empty tree." << endl;
else
{
current = root;

while (current != NULL && !found)
{
if (current->info == searchItem)
found = true;
else if (current->info > searchItem)
current = current->llink;
else
current = current->rlink;
}//end while
}//end else

return found;
}//end search

template <class elemType>
void bSearchTreeType<elemType>::insert(const elemType& insertItem)
{
binaryTreeNode<elemType> *current; //pointer to traverse the tree
binaryTreeNode<elemType> *trailCurrent; //pointer behind current
binaryTreeNode<elemType> *newNode; //pointer to create the node

newNode = new binaryTreeNode<elemType>;
assert(newNode != NULL);
newNode->info = insertItem;
newNode->llink = NULL;
newNode->rlink = NULL;

if (root == NULL)
root = newNode;
else
{
current = root;

while (current != NULL)
{
trailCurrent = current;

if (current->info == insertItem)
{
cerr << "The insert item is already in the list-";
cerr << "duplicates are not allowed."
<< insertItem << endl;
return;
}
else if (current->info > insertItem)
current = current->llink;
else
current = current->rlink;
}//end while

if (trailCurrent->info > insertItem)
trailCurrent->llink = newNode;
else
trailCurrent->rlink = newNode;
}
}//end insert

template <class elemType>
void bSearchTreeType<elemType>::deleteNode(const elemType& deleteItem)
{
binaryTreeNode<elemType> *current; //pointer to traverse the tree
binaryTreeNode<elemType> *trailCurrent; //pointer behind current
bool found = false;

if (root == NULL)
cout << "Cannot delete from the empty tree." << endl;
else
{
current = root;
trailCurrent = root;

while (current != NULL && !found)
{
if (current->info == deleteItem)
found = true;
else
{
trailCurrent = current;

if (current->info > deleteItem)
current = current->llink;
else
current = current->rlink;
}
}//end while

if (current == NULL)
cout << "The delete item is not in the tree." << endl;
else if (found)
{
if (current == root)
deleteFromTree(root);
else if (trailCurrent->info > deleteItem)
deleteFromTree(trailCurrent->llink);
else
deleteFromTree(trailCurrent->rlink);
}//end if
}
}//end deleteNode

template <class elemType>
void bSearchTreeType<elemType>::deleteFromTree
(binaryTreeNode<elemType>* &p)
{
binaryTreeNode<elemType> *current; //pointer to traverse the tree
binaryTreeNode<elemType> *trailCurrent; //pointer behind current
binaryTreeNode<elemType> *temp; //pointer to delete the node

if (p == NULL)
cerr << "Error: The node to be deleted is NULL." << endl;
else if(p->llink == NULL && p->rlink == NULL)
{
temp = p;
p = NULL;
delete temp;
}
else if(p->llink == NULL)
{
temp = p;
p = temp->rlink;
delete temp;
}
else if(p->rlink == NULL)
{
temp = p;
p = temp->llink;
delete temp;
}
else
{
current = p->llink;
trailCurrent = NULL;

while (current->rlink != NULL)
{
trailCurrent = current;
current = current->rlink;
}//end while

p->info = current->info;

if (trailCurrent == NULL) //current did not move;
//current == p->llink; adjust p
p->llink = current->llink;
else
trailCurrent->rlink = current->llink;

delete current;
}//end else
}//end deleteFromTree

#endif

BINARYTREE.H

//Header File Binary Search Tree
#ifndef H_binaryTree
#define H_binaryTree

//*************************************************************
// Author: D.S. Malik
//
// class binaryTreeType
// This class specifies the basic operations to implement a
// binary tree.
//*************************************************************

#include <iostream>

using namespace std;

//Definition of the node
template <class elemType>
struct binaryTreeNode
{
elemType info;
binaryTreeNode<elemType> *llink;
binaryTreeNode<elemType> *rlink;
};

//Definition of the class
template <class elemType>
class binaryTreeType
{
public:
const binaryTreeType<elemType>& operator=
(const binaryTreeType<elemType>&);
//Overload the assignment operator.
bool isEmpty() const;
//Returns true if the binary tree is empty;
//otherwise, returns false.
void inorderTraversal() const;
//Function to do an inorder traversal of the binary tree.
void preorderTraversal() const;
//Function to do a preorder traversal of the binary tree.
void postorderTraversal() const;
//Function to do a postorder traversal of the binary tree.

int treeHeight() const;
//Returns the height of the binary tree.
int treeNodeCount() const;
//Returns the number of nodes in the binary tree.
int treeLeavesCount() const;
//Returns the number of leaves in the binary tree.
void destroyTree();
//Deallocates the memory space occupied by the binary tree.
//Postcondition: root = NULL;

binaryTreeType(const binaryTreeType<elemType>& otherTree);
//copy constructor

binaryTreeType();   
//default constructor

~binaryTreeType();   
//destructor

   //leavesCount();

protected:
binaryTreeNode<elemType> *root;

private:
void copyTree(binaryTreeNode<elemType>* &copiedTreeRoot,
binaryTreeNode<elemType>* otherTreeRoot);
//Makes a copy of the binary tree to which
//otherTreeRoot points. The pointer copiedTreeRoot
//points to the root of the copied binary tree.

void destroy(binaryTreeNode<elemType>* &p);
//Function to destroy the binary tree to which p points.
//Postcondition: p = NULL

void inorder(binaryTreeNode<elemType> *p) const;
//Function to do an inorder traversal of the binary
//tree to which p points.
void preorder(binaryTreeNode<elemType> *p) const;
//Function to do a preorder traversal of the binary
//tree to which p points.
void postorder(binaryTreeNode<elemType> *p) const;
//Function to do a postorder traversal of the binary
//tree to which p points.

int height(binaryTreeNode<elemType> *p) const;
//Function to return the height of the binary tree
//to which p points.
int max(int x, int y) const;
//Returns the larger of x and y.
int nodeCount(binaryTreeNode<elemType> *p) const;
//Function to return the number of nodes in the binary
//tree to which p points
int leavesCount(binaryTreeNode<elemType> *p) const;
//Function to return the number of leaves in the binary
//tree to which p points
};

//Definition of member functions

template <class elemType>
binaryTreeType<elemType>::binaryTreeType()
{
root = NULL;
}

template <class elemType>
bool binaryTreeType<elemType>::isEmpty() const
{
return (root == NULL);
}

template <class elemType>
void binaryTreeType<elemType>::inorderTraversal() const
{
inorder(root);
}

template <class elemType>
void binaryTreeType<elemType>::preorderTraversal() const
{
preorder(root);
}

template <class elemType>
void binaryTreeType<elemType>::postorderTraversal() const
{
postorder(root);
}

template <class elemType>
int binaryTreeType<elemType>::treeHeight() const
{
return height(root);
}

template <class elemType>
int binaryTreeType<elemType>::treeNodeCount() const
{
return nodeCount(root);
}

template <class elemType>
int binaryTreeType<elemType>::treeLeavesCount() const
{
return leavesCount(root);
}

template <class elemType>
void binaryTreeType<elemType>::copyTree
(binaryTreeNode<elemType>* &copiedTreeRoot,
       binaryTreeNode<elemType>* otherTreeRoot)
{
if (otherTreeRoot == NULL)
copiedTreeRoot = NULL;
else
{
copiedTreeRoot = new binaryTreeNode<elemType>;
copiedTreeRoot->info = otherTreeRoot->info;
copyTree(copiedTreeRoot->llink, otherTreeRoot->llink);
copyTree(copiedTreeRoot->rlink, otherTreeRoot->rlink);
}
} //end copyTree

template <class elemType>
void binaryTreeType<elemType>::inorder(binaryTreeNode<elemType> *p) const
{
if (p != NULL)
{
inorder(p->llink);
cout << p->info << " ";
inorder(p->rlink);
}
}

template <class elemType>
void binaryTreeType<elemType>::preorder(binaryTreeNode<elemType> *p) const
{
   if (p != NULL)
   {
       cout<<p->info<<" ";
       preorder(p->llink);
       preorder(p->rlink);
   }
}

template <class elemType>
void binaryTreeType<elemType>::postorder(binaryTreeNode<elemType> *p) const
{
if (p != NULL)
{
postorder(p->llink);
postorder(p->rlink);
cout << p->info << " ";
}      
}

//Overload the assignment operator
template <class elemType>
const binaryTreeType<elemType>& binaryTreeType<elemType>::
operator=(const binaryTreeType<elemType>& otherTree)
{
if (this != &otherTree) //avoid self-copy
{
if (root != NULL) //if the binary tree is not empty,
//destroy the binary tree
destroy(root);

if (otherTree.root == NULL) //otherTree is empty
root = NULL;
else
copyTree(root, otherTree.root);
}//end else

return *this;
}

template <class elemType>
void binaryTreeType<elemType>::destroy(binaryTreeNode<elemType>* &p)
{
if (p != NULL)
{
destroy(p->llink);
destroy(p->rlink);
delete p;
p = NULL;
}
}

template <class elemType>
void binaryTreeType<elemType>::destroyTree()
{
destroy(root);
}

   //copy constructor
template <class elemType>
binaryTreeType<elemType>::binaryTreeType
(const binaryTreeType<elemType>& otherTree)
{
if (otherTree.root == NULL) //otherTree is empty
root = NULL;
else
copyTree(root, otherTree.root);
}

template <class elemType>
binaryTreeType<elemType>::~binaryTreeType()
{
destroy(root);
}

template <class elemType>
int binaryTreeType<elemType>::height(binaryTreeNode<elemType> *p) const
{
if (p == NULL)
return 0;
else
return 1 + max(height(p->llink), height(p->rlink));
}

template <class elemType>
int binaryTreeType<elemType>::max(int x, int y) const
{
if (x >= y)
return x;
else
return y;
}

template <class elemType>
int binaryTreeType<elemType>::nodeCount(binaryTreeNode<elemType> *p) const
{
cout << "Write the definition of the function nodeCount"
<< endl;

return 0;
}

template <class elemType>
int binaryTreeType<elemType>::leavesCount(binaryTreeNode<elemType> *p) const
{
   if (p == NULL)
       return 0;
   else
       if (p->llink == NULL && rlink == NULL)
           return 1;
       else
           return leavesCount(p->llink) + leavesCount(p->rlink);
}

#endif

How exactly do I approach this problem?

Explanation / Answer

binarySearchTree.h

#ifndef BINARYSEARCHTREE_H_INCLUDED
#define BINARYSEARCHTREE_H_INCLUDED


#include <iostream>
#include "binaryTree.h"

using namespace std;

template <class elemType>
class bSearchTreeType : public binaryTreeType<elemType>
{
public:
   bool search(const elemType& searchItem) const;
   //Function to determine if searchItem is in the binary
   //search tree.
   //Postcondition: Returns true if searchItem is found in
   //               the binary search tree; otherwise,
   //               returns false.

    void printElement(const elemType& searchItem) const;
    //Function print element that matches the search paramater
    //or shows message that not found

   void insert(const elemType& insertItem);
   //Function to insert insertItem in the binary search tree.
   //Postcondition: If there is no node in the binary search
   //               tree that has the same info as
   //               insertItem, a node with the info
   //               insertItem is created and inserted in the
   //               binary search tree.

   void deleteNode(const elemType& deleteItem);
   //Function to delete deleteItem from the binary search tree
   //Postcondition: If a node with the same info as deleteItem
   //               is found, it is deleted from the binary
   //               search tree.
   //               If the binary tree is empty or deleteItem
   //               is not in the binary tree, an appropriate
   //               message is ptinted.

private:
   void deleteFromTree(nodeType<elemType>* &p);
   //Function to delete the node to which p points is
   //deleted from the binary search tree.
   //Postcondition: The node to which p points is deleted
   //               from the binary search tree.
};


template <class elemType>
bool bSearchTreeType<elemType>::search
(const elemType& searchItem) const
{
   nodeType<elemType> *current;
   bool found = false;

   if (binaryTreeType<elemType>::root == NULL)
       cout << "Cannot search an empty tree." << endl;
   else
   {
       current = binaryTreeType<elemType>::root;

       while (current != NULL && !found)
       {
           if (current->info == searchItem)
               found = true;
           else if (current->info > searchItem)
               current = current->lLink;
           else
               current = current->rLink;
       }//end while
   }//end else

   return found;
}//end search

template <class elemType>
void bSearchTreeType<elemType>::printElement
(const elemType& searchItem) const
{
   nodeType<elemType> *current;
   bool found = false;

   if (binaryTreeType<elemType>::root == NULL)
       cout << "Tree is empty." << endl;
   else
   {
       current = binaryTreeType<elemType>::root;

       while (current != NULL && !found)
       {
           if (current->info == searchItem){
                found = true;
                cout << " __FOUND__" << endl;
               cout << " " << current->info << endl;
           }
           else if (current->info > searchItem)
               current = current->lLink;
           else
               current = current->rLink;
       }//end while
   }//end else
   if (found == false && binaryTreeType<elemType>::root != NULL){
        cout << "Not found in tree" << endl;
   }
}


template <class elemType>
void bSearchTreeType<elemType>::insert
(const elemType& insertItem)
{
   nodeType<elemType> *current; //pointer to traverse the tree
   nodeType<elemType> *trailCurrent = nullptr; //pointer behind current
   nodeType<elemType> *newNode; //pointer to create the node

   newNode = new nodeType<elemType>;
            newNode->info = insertItem;
   newNode->lLink = NULL;
   newNode->rLink = NULL;

   if (binaryTreeType<elemType>::root == NULL)
       binaryTreeType<elemType>::root = newNode;
   else
   {
       current = binaryTreeType<elemType>::root;

       while (current != NULL)
       {
           trailCurrent = current;

           if (current->info == insertItem)
           {
               cout << "The item to be inserted is already ";
               cout << "in the tree -- duplicates are not "
                   << "allowed." << endl;
               return;
           }
           else if (current->info > insertItem)
               current = current->lLink;
           else
               current = current->rLink;
       }//end while

       if (trailCurrent->info > insertItem)
           trailCurrent->lLink = newNode;
       else
           trailCurrent->rLink = newNode;
   }
}//end insert

template <class elemType>
void bSearchTreeType<elemType>::deleteNode
(const elemType& deleteItem)
{
   nodeType<elemType> *current; //pointer to traverse the tree
   nodeType<elemType> *trailCurrent; //pointer behind current
   bool found = false;

   if (binaryTreeType<elemType>::root == NULL)
       cout << "Cannot delete from an empty tree."
       << endl;
   else
   {
       current = binaryTreeType<elemType>::root;
       trailCurrent = binaryTreeType<elemType>::root;

       while (current != NULL && !found)
       {
           if (current->info == deleteItem)
               found = true;
           else
           {
               trailCurrent = current;

               if (current->info > deleteItem)
                   current = current->lLink;
               else
                   current = current->rLink;
           }
       }//end while

       if (current == NULL)
           cout << "The item to be deleted is not in the tree."
           << endl;
       else if (found)
       {
           if (current == binaryTreeType<elemType>::root)
               deleteFromTree(binaryTreeType<elemType>::root);
           else if (trailCurrent->info > deleteItem)
               deleteFromTree(trailCurrent->lLink);
           else
               deleteFromTree(trailCurrent->rLink);
       }
       else
           cout << "The item to be deleted is not in the tree."
           << endl;
   }
} //end deleteNode

template <class elemType>
void bSearchTreeType<elemType>::deleteFromTree
(nodeType<elemType>* &p)
{
   nodeType<elemType> *current; //pointer to traverse the tree
   nodeType<elemType> *trailCurrent; //pointer behind current
   nodeType<elemType> *temp;      //pointer to delete the node

   if (p == NULL)
       cout << "Error: The node to be deleted is NULL."
       << endl;
   else if (p->lLink == NULL && p->rLink == NULL)
   {
       temp = p;
       p = NULL;
       delete temp;
   }
   else if (p->lLink == NULL)
   {
       temp = p;
       p = temp->rLink;
       delete temp;
   }
   else if (p->rLink == NULL)
   {
       temp = p;
       p = temp->lLink;
       delete temp;
   }
   else
   {
       current = p->lLink;
       trailCurrent = NULL;

       while (current->rLink != NULL)
       {
           trailCurrent = current;
           current = current->rLink;
       }//end while

       p->info = current->info;

       if (trailCurrent == NULL) //current did not move;
           //current == p->lLink; adjust p
           p->lLink = current->lLink;
       else
           trailCurrent->rLink = current->lLink;

       delete current;
   }//end else
} //end deleteFromTree


#endif // BINARYSEARCHTREE_H_INCLUDED


binaryTree.h

#ifndef BINARYTREE_H_INCLUDED
#define BINARYTREE_H_INCLUDED


#include <iostream>

using namespace std;

//Definition of the Node
template <class elemType>
struct nodeType
{
   elemType info;
   nodeType<elemType> *lLink;
   nodeType<elemType> *rLink;
};

//Definition of the class
template <class elemType>
class binaryTreeType
{
public:
   const binaryTreeType<elemType>& operator=
       (const binaryTreeType<elemType>&);
   //Overload the assignment operator.

   bool isEmpty() const;
   //Function to determine whether the binary tree is empty.
   //Postcondition: Returns true if the binary tree is empty;
   //               otherwise, returns false.

   void inorderTraversal() const;
   //Function to do an inorder traversal of the binary tree.
   //Postcondition: Nodes are printed in inorder sequence.

   void preorderTraversal() const;
   //Function to do a preorder traversal of the binary tree.
   //Postcondition: Nodes are printed in preorder sequence.

   void postorderTraversal() const;
   //Function to do a postorder traversal of the binary tree.
   //Postcondition: Nodes are printed in postorder sequence.

   int treeHeight() const;
   //Function to determine the height of a binary tree.
   //Postcondition: Returns the height of the binary tree.

   int treeNodeCount() const;
   //Function to determine the number of nodes in a
   //binary tree.
   //Postcondition: Returns the number of nodes in the
   //               binary tree.

   int treeLeavesCount() const;
   //Function to determine the number of leaves in a
   //binary tree.
   //Postcondition: Returns the number of leaves in the
   //               binary tree.

   void destroyTree();
   //Function to destroy the binary tree.
   //Postcondition: Memory space occupied by each node
   //               is deallocated.
   //               root = NULL;

   virtual bool search(const elemType& searchItem) const = 0;
   //Function to determine if searchItem is in the binary
   //tree.
   //Postcondition: Returns true if searchItem is found in
   //               the binary tree; otherwise, returns
   //               false.

   virtual void insert(const elemType& insertItem) = 0;
   //Function to insert insertItem in the binary tree.
   //Postcondition: If there is no node in the binary tree
   //               that has the same info as insertItem, a
   //               node with the info insertItem is created
   //               and inserted in the binary search tree.

   virtual void deleteNode(const elemType& deleteItem) = 0;
   //Function to delete deleteItem from the binary tree
   //Postcondition: If a node with the same info as
   //               deleteItem is found, it is deleted from
   //               the binary tree.
   //               If the binary tree is empty or
   //               deleteItem is not in the binary tree,
   //               an appropriate message is printed.

   binaryTreeType(const binaryTreeType<elemType>& otherTree);
   //Copy constructor

   binaryTreeType();
   //Default constructor

   ~binaryTreeType();
   //Destructor

protected:
   nodeType<elemType> *root;

private:
    int numberOfNodes;
   void copyTree(nodeType<elemType>* &copiedTreeRoot,
       nodeType<elemType>* otherTreeRoot);
   //Makes a copy of the binary tree to which
   //otherTreeRoot points.
   //Postcondition: The pointer copiedTreeRoot points to
   //               the root of the copied binary tree.

   void destroy(nodeType<elemType>* &p);
   //Function to destroy the binary tree to which p points.
   //Postcondition: Memory space occupied by each node, in
   //               the binary tree to which p points, is
   //               deallocated.
   //               p = NULL;

   void inorder(nodeType<elemType> *p) const;
   //Function to do an inorder traversal of the binary
   //tree to which p points.
   //Postcondition: Nodes of the binary tree, to which p
   //               points, are printed in inorder sequence.

   void preorder(nodeType<elemType> *p) const;
   //Function to do a preorder traversal of the binary
   //tree to which p points.
   //Postcondition: Nodes of the binary tree, to which p
   //               points, are printed in preorder
   //               sequence.

   void postorder(nodeType<elemType> *p) const;
   //Function to do a postorder traversal of the binary
   //tree to which p points.
   //Postcondition: Nodes of the binary tree, to which p
   //               points, are printed in postorder
   //               sequence.

   int height(nodeType<elemType> *p) const;
   //Function to determine the height of the binary tree
   //to which p points.
   //Postcondition: Height of the binary tree to which
   //               p points is returned.

   int max(int x, int y) const;
   //Function to determine the larger of x and y.
   //Postcondition: Returns the larger of x and y.

   int nodeCount(nodeType<elemType> *p) const;
   //Function to determine the number of nodes in
   //the binary tree to which p points.
   //Postcondition: The number of nodes in the binary
   //               tree to which p points is returned.

   int leavesCount(nodeType<elemType> *p) const;
   //Function to determine the number of leaves in
   //the binary tree to which p points
   //Postcondition: The number of leaves in the binary
   //               tree to which p points is returned.
};

//Definition of member functions

template <class elemType>
binaryTreeType<elemType>::binaryTreeType()
{
   root = NULL;
   numberOfNodes = 0;
}

template <class elemType>
bool binaryTreeType<elemType>::isEmpty() const
{
   return (root == NULL);
}

template <class elemType>
void binaryTreeType<elemType>::inorderTraversal() const
{
   inorder(root);
}

template <class elemType>
void binaryTreeType<elemType>::preorderTraversal() const
{
   preorder(root);
}

template <class elemType>
void binaryTreeType<elemType>::postorderTraversal() const
{
   postorder(root);
}

template <class elemType>
int binaryTreeType<elemType>::treeHeight() const
{
   return height(root);
}

template <class elemType>
int binaryTreeType<elemType>::treeNodeCount() const
{
   return nodeCount(root);
}

template <class elemType>
int binaryTreeType<elemType>::treeLeavesCount() const
{
   return leavesCount(root);
}

template <class elemType>
void binaryTreeType<elemType>::copyTree
(nodeType<elemType>* &copiedTreeRoot,
nodeType<elemType>* otherTreeRoot)
{
   if (otherTreeRoot == NULL)
       copiedTreeRoot = NULL;
   else
   {
       copiedTreeRoot = new nodeType<elemType>;
       copiedTreeRoot->info = otherTreeRoot->info;
       copyTree(copiedTreeRoot->lLink, otherTreeRoot->lLink);
       copyTree(copiedTreeRoot->rLink, otherTreeRoot->rLink);
   }
} //end copyTree

template <class elemType>
void binaryTreeType<elemType>::inorder
(nodeType<elemType> *p) const
{
   if (p != NULL)
   {
       inorder(p->lLink);
       cout << p->info << " ";
       inorder(p->rLink);
   }
}

template <class elemType>
void binaryTreeType<elemType>::preorder
(nodeType<elemType> *p) const
{
   if (p != NULL)
   {
       cout << p->info << " ";
       preorder(p->lLink);
       preorder(p->rLink);
   }
}

template <class elemType>
void binaryTreeType<elemType>::postorder
(nodeType<elemType> *p) const
{
   if (p != NULL)
   {
       postorder(p->lLink);
       postorder(p->rLink);
       cout << p->info << " ";
   }
}

//Overload the assignment operator
template <class elemType>
const binaryTreeType<elemType>& binaryTreeType<elemType>::
operator=(const binaryTreeType<elemType>& otherTree)
{
   if (this != &otherTree) //avoid self-copy
   {
       if (root != NULL)   //if the binary tree is not empty,
           //destroy the binary tree
           destroy(root);

       if (otherTree.root == NULL) //otherTree is empty
           root = NULL;
       else
           copyTree(root, otherTree.root);
   }//end else

   return *this;
}

template <class elemType>
void binaryTreeType<elemType>::destroy(nodeType<elemType>* &p)
{
   if (p != NULL)
   {
       destroy(p->lLink);
       destroy(p->rLink);
       delete p;
       p = NULL;
   }
}

template <class elemType>
void binaryTreeType<elemType>::destroyTree()
{
   destroy(root);
}

//copy constructor
template <class elemType>
binaryTreeType<elemType>::binaryTreeType
(const binaryTreeType<elemType>& otherTree)
{
   if (otherTree.root == NULL) //otherTree is empty
       root = NULL;
   else
       copyTree(root, otherTree.root);
}

//Destructor
template <class elemType>
binaryTreeType<elemType>::~binaryTreeType()
{
   destroy(root);
}

template<class elemType>
int binaryTreeType<elemType>::height
(nodeType<elemType> *p) const
{
   if (p == NULL)
       return 0;
   else
       return 1 + max(height(p->lLink), height(p->rLink));
}

template <class elemType>
int binaryTreeType<elemType>::max(int x, int y) const
{
   if (x >= y)
       return x;
   else
       return y;
}

template <class elemType>
int binaryTreeType<elemType>::nodeCount(nodeType<elemType> *p) const
{
    int count = 0;
    if (p != NULL) {
        count = 1 + nodeCount(p->lLink) + nodeCount(p->rLink);
    }
    return count;

}

template <class elemType>
int binaryTreeType<elemType>::leavesCount(nodeType<elemType> *p) const
{
   cout << "Write the definition of the function leavesCount."
       << endl;

   return 0;
}

#endif // BINARYTREE_H_INCLUDED