The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | May 2017 |
| Last Sign in: | 399 Weeks Ago |
| Questions Answered: | 66690 |
| Tutorials Posted: | 66688 |
MCS,PHD
Argosy University/ Phoniex University/
Nov-2005 - Oct-2011
Professor
Phoniex University
Oct-2001 - Nov-2016
Add two functions to a binary tree class. The first function returns an array list or vector. It is modified version of the public in order tree traversal function. The first function invokes the second function, a modified version of the private in order tree traversal function. The second function copies the tree nodes' data to the array list or vector as it visits each node. .
Write a program to test your function.
binaryTree.h
#ifndef H_binaryTree
#define H_binaryTree
#include
#include
using namespace std;
template
struct binaryTreeNode
{
elemType info;
binaryTreeNode *llink;
binaryTreeNode *rlink;
};
template
class binaryTreeType
{
public:
const binaryTreeType& operator=
(const binaryTreeType&);
//Overload the assignment operator.
bool isEmpty() const;
void inorderTraversal() const;
void preorderTraversal() const;
void postorderTraversal() const;
int treeHeight() const;
int treeNodeCount() const;
int treeLeavesCount() const;
int singleParent()const;
//function to determine the nubmer of nodes that are single
void destroyTree();
binaryTreeType(const binaryTreeType& otherTree);
binaryTreeType();
~binaryTreeType();
protected:
binaryTreeNode *root;
private:
void copyTree(binaryTreeNode* &copiedTreeRoot,
binaryTreeNode* otherTreeRoot);
void destroy(binaryTreeNode* &p);
void inorder(binaryTreeNode *p) const;
void preorder(binaryTreeNode *p) const;
void postorder(binaryTreeNode *p) const;
int height(binaryTreeNode *p) const;
int max(int x, int y) const;
int nodeCount(binaryTreeNode *p) const;
int leavesCount(binaryTreeNode *p) const;
int singleP(binaryTreeNode *p) const;
//parent in the binary tree to which P points.
};
template
int binaryTreeType::singleParent()const
{
return singleP(root);
}
template
int binaryTreeType::singleP(binaryTreeNode *p)const
{
if (p == NULL)
return 0;
else if (p->llink == NULL && p->rlink != NULL)
return 1 + singleP(p->rlink);
else if (p->llink != NULL && p->rlink == NULL)
return 1 + singleP(p->llink);
else
return singleP(p->llink) + singleP(p->rlink);
}
template
binaryTreeType::binaryTreeType()
{
root = NULL;
}
template
bool binaryTreeType::isEmpty() const
{
return (root == NULL);
}
template
void binaryTreeType::inorderTraversal() const
{
inorder(root);
}
template
void binaryTreeType::preorderTraversal() const
{
preorder(root);
}
template
void binaryTreeType::postorderTraversal() const
{
postorder(root);
}
template
int binaryTreeType::treeHeight() const
{
return height(root);
}
template
int binaryTreeType::treeNodeCount() const
{
return nodeCount(root);
}
template
int binaryTreeType::treeLeavesCount() const
{
return leavesCount(root);
}
template
void binaryTreeType::copyTree
(binaryTreeNode* &copiedTreeRoot,
binaryTreeNode* otherTreeRoot)
{
if (otherTreeRoot == NULL)
copiedTreeRoot = NULL;
else
{
copiedTreeRoot = new binaryTreeNode;
copiedTreeRoot->info = otherTreeRoot->info;
copyTree(copiedTreeRoot->llink, otherTreeRoot->llink);
copyTree(copiedTreeRoot->rlink, otherTreeRoot->rlink);
}
} //end copyTree
template
void binaryTreeType::inorder(binaryTreeNode *p) const
{
if (p != NULL)
{
inorder(p->llink);
cout << p->info
inorder(p->rlink);
}
}
template
void binaryTreeType::preorder(binaryTreeNode *p) const
{
if (p != NULL)
{
cout
info
preorder(p->llink);
preorder(p->rlink);
}
}
template
void binaryTreeType::postorder(binaryTreeNode *p) const
{
if (p != NULL)
{
postorder(p->llink);
postorder(p->rlink);
cout << p->info
}
}
//Overload the assignment operator
template
const binaryTreeType& binaryTreeType::
operator=(const binaryTreeType& 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
void binaryTreeType::destroy(binaryTreeNode* &p)
{
if (p != NULL)
{
destroy(p->llink);
destroy(p->rlink);
delete p;
p = NULL;
}
}
template
void binaryTreeType::destroyTree()
{
destroy(root);
}
//copy constructor
template
binaryTreeType::binaryTreeType
(const binaryTreeType& otherTree)
{
if (otherTree.root == NULL) //otherTree is empty
root = NULL;
else
copyTree(root, otherTree.root);
}
template
binaryTreeType::~binaryTreeType()
{
destroy(root);
}
template
int binaryTreeType::height(binaryTreeNode *p) const
{
if (p == NULL)
return 0;
else
return 1 + max(height(p->llink), height(p->rlink));
}
template
int binaryTreeType::max(int x, int y) const
{
if (x >= y)
return x;
else
return y;
}
template
int binaryTreeType::nodeCount(binaryTreeNode *p) const
{
cout
return 0;
}
template
int binaryTreeType::leavesCount(binaryTreeNode *p) const
{
cout
return 0;
}
#endif
Hel-----------lo -----------Sir-----------/Ma-----------dam-----------Tha-----------nk -----------You----------- fo-----------r u-----------sin-----------g o-----------ur -----------web-----------sit-----------e a-----------nd -----------and----------- ac-----------qui-----------sit-----------ion----------- of----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n.P-----------lea-----------se -----------pin-----------g m-----------e o-----------n c-----------hat----------- I -----------am -----------onl-----------ine----------- or----------- in-----------box----------- me----------- a -----------mes-----------sag-----------e I----------- wi-----------ll