SlideShare a Scribd company logo
Add these three functions to the class binaryTreeType (provided).
Write the definition of the function, nodeCount, that returns the number of nodes in the binary
tree.
Write the definition of the function, leavesCount, that takes as a parameter a pointer to the root
node of a binary tree and returns the number of leaves in a binary tree.
Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Print
the original tree and the resulting tree using a pre-order traversal.
Write a program to test your new functions. Use the data provided to build your binary search
tree (-999 is the sentinel): (C++)
Data: 65 55 22 44 61 19 90 10 78 52 -999
Already completed code is below:
//Header File Binary Search Tree
#ifndef H_binarySearchTree
#define H_binarySearchTree
#include "binaryTree.h"
#include
using namespace std;
template
class bSearchTreeType: public binaryTreeType
{
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 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 printed.
private:
void deleteFromTree(nodeType* &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
bool bSearchTreeType::search
(const elemType& searchItem) const
{
nodeType *current;
bool found = false;
if (this->root == NULL)
cout << "Cannot search an empty tree." << endl;
else
{
current = this->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
void bSearchTreeType::insert
(const elemType& insertItem)
{
nodeType *current; //pointer to traverse the tree
nodeType *trailCurrent; //pointer behind current
nodeType *newNode; //pointer to create the node
newNode = new nodeType;
newNode->info = insertItem;
newNode->lLink = NULL;
newNode->rLink = NULL;
if (this->root == NULL)
this->root = newNode;
else
{
current = this->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
void bSearchTreeType::deleteNode
(const elemType& deleteItem)
{
nodeType *current; //pointer to traverse the tree
nodeType *trailCurrent; //pointer behind current
bool found = false;
if (this->root == NULL)
cout << "Cannot delete from an empty tree."
<< endl;
else
{
current = this->root;
trailCurrent = this->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 == this->root)
deleteFromTree(this->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
void bSearchTreeType::deleteFromTree
(nodeType* &p)
{
nodeType *current; //pointer to traverse the tree
nodeType *trailCurrent; //pointer behind current
nodeType *temp; //pointer to delete the node
if (p == NULL)
cout << "Error: The node to be deleted does not exist."
<< 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
//Header File Binary Search Tree
#ifndef H_binaryTree
#define H_binaryTree
#include
using namespace std;
//Definition of the Node
template
struct nodeType
{
elemType info;
nodeType *lLink;
nodeType *rLink;
};
//Definition of the class
template
class binaryTreeType
{
public:
const binaryTreeType& operator=
(const binaryTreeType&);
//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.
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& otherTree);
//Copy constructor
binaryTreeType();
//Default constructor
~binaryTreeType();
//Destructor
protected:
nodeType *root;
private:
void copyTree(nodeType* &copiedTreeRoot,
nodeType* 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* &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 *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 *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 *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 *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.
};
//Definition of member functions
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
(nodeType* &copiedTreeRoot,
nodeType* otherTreeRoot)
{
if (otherTreeRoot == NULL)
copiedTreeRoot = NULL;
else
{
copiedTreeRoot = new nodeType;
copiedTreeRoot->info = otherTreeRoot->info;
copyTree(copiedTreeRoot->lLink, otherTreeRoot->lLink);
copyTree(copiedTreeRoot->rLink, otherTreeRoot->rLink);
}
} //end copyTree
template
void binaryTreeType::inorder
(nodeType *p) const
{
if (p != NULL)
{
inorder(p->lLink);
cout << p->info << " ";
inorder(p->rLink);
}
}
template
void binaryTreeType::preorder
(nodeType *p) const
{
if (p != NULL)
{
cout << p->info << " ";
preorder(p->lLink);
preorder(p->rLink);
}
}
template
void binaryTreeType::postorder
(nodeType *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(nodeType* &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);
}
//Destructor
template
binaryTreeType::~binaryTreeType()
{
destroy(root);
}
template
int binaryTreeType::height
(nodeType *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;
}
#endif
Solution
//Header File Binary Search Tree
#ifndef H_binarySearchTree
#define H_binarySearchTree
#include "binaryTree.hbinaryTree.h"
#include
using namespace std;
template
class bSearchTreeType: public binaryTreeType
{
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 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 printed.
private:
void deleteFromTree(nodeType* &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
bool bSearchTreeType::search
(const elemType& searchItem) const
{
nodeType *current;
bool found = false;
if (this->root == NULL)
cout << "Cannot search an empty tree." << endl;
else
{
current = this->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
void bSearchTreeType::insert
(const elemType& insertItem)
{
nodeType *current; //pointer to traverse the tree
nodeType *trailCurrent; //pointer behind current
nodeType *newNode; //pointer to create the node
newNode = new nodeType;
newNode->info = insertItem;
newNode->lLink = NULL;
newNode->rLink = NULL;
if (this->root == NULL)
this->root = newNode;
else
{
current = this->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
void bSearchTreeType::deleteNode
(const elemType& deleteItem)
{
nodeType *current; //pointer to traverse the tree
nodeType *trailCurrent; //pointer behind current
bool found = false;
if (this->root == NULL)
cout << "Cannot delete from an empty tree."
<< endl;
else
{
current = this->root;
trailCurrent = this->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 == this->root)
deleteFromTree(this->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
void bSearchTreeType::deleteFromTree
(nodeType* &p)
{
nodeType *current; //pointer to traverse the tree
nodeType *trailCurrent; //pointer behind current
nodeType *temp; //pointer to delete the node
if (p == NULL)
cout << "Error: The node to be deleted does not exist."
<< 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
//Header File Binary Search Tree
#ifndef H_binaryTree
#define H_binaryTree
#include
using namespace std;
//Definition of the Node
template
struct nodeType
{
elemType info;
nodeType *lLink;
nodeType *rLink;
};
//Definition of the class
template
class binaryTreeType
{
public:
const binaryTreeType& operator=
(const binaryTreeType&);
//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.
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& otherTree);
//Copy constructor
binaryTreeType();
//Default constructor
~binaryTreeType();
//Destructor
protected:
nodeType *root;
private:
void copyTree(nodeType* &copiedTreeRoot,
nodeType* 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* &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 *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 *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 *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 *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.
};
//Definition of member functions
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
(nodeType* &copiedTreeRoot,
nodeType* otherTreeRoot)
{
if (otherTreeRoot == NULL)
copiedTreeRoot = NULL;
else
{
copiedTreeRoot = new nodeType;
copiedTreeRoot->info = otherTreeRoot->info;
copyTree(copiedTreeRoot->lLink, otherTreeRoot->lLink);
copyTree(copiedTreeRoot->rLink, otherTreeRoot->rLink);
}
} //end copyTree
template
void binaryTreeType::inorder
(nodeType *p) const
{
if (p != NULL)
{
inorder(p->lLink);
cout << p->info << " ";
inorder(p->rLink);
}
}
template
void binaryTreeType::preorder
(nodeType *p) const
{
if (p != NULL)
{
cout << p->info << " ";
preorder(p->lLink);
preorder(p->rLink);
}
}
template
void binaryTreeType::postorder
(nodeType *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(nodeType* &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);
}
//Destructor
template
binaryTreeType::~binaryTreeType()
{
destroy(root);
}
template
int binaryTreeType::height
(nodeType *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::leavesCount(nodeType *node)
{
if (node == NULL)
return 0;
else if (node->lLink == NULL && node->rLink == NULL)
return 1;
else
return (leavesCount(node->lLink)) + (leavesCount(node->rLink));
}
template
int binaryTreeType::nodeCount(nodeType *node)
{
int count = 1;
if (node == NULL)
return 0;
else
{
count = count + nodeCount(node->lLink);
count = count + nodeCount(node->rLink);
return count;
}
}
template
void binaryTreeType::swapSubtrees(nodeType *node)
{
if (node==NULL)
return;
else
{
struct nodeType* t;
swapSubtrees(node->lLink);
swapSubtrees(node->rLink);
t = node->lLink;
node->lLink = node->rLink;
node->rLink = t;
}
}
#endif

More Related Content

PDF
Please i need help on following program using C++ Language.Add the.pdf
PDF
Create an implementation of a binary tree using the recursive appr.pdf
PDF
Tree Traversals A tree traversal is the process of visiting.pdf
PDF
In c++ format, for each function in the code, please using the comme.pdf
PDF
MAINCPP include ltiostreamgt include ltstringgt u.pdf
DOCX
Once you have all the structures working as intended- it is time to co.docx
PDF
create a binary search tree from an empty one by adding the key valu.pdf
PDF
Write a C program that reads the words the user types at the command.pdf
Please i need help on following program using C++ Language.Add the.pdf
Create an implementation of a binary tree using the recursive appr.pdf
Tree Traversals A tree traversal is the process of visiting.pdf
In c++ format, for each function in the code, please using the comme.pdf
MAINCPP include ltiostreamgt include ltstringgt u.pdf
Once you have all the structures working as intended- it is time to co.docx
create a binary search tree from an empty one by adding the key valu.pdf
Write a C program that reads the words the user types at the command.pdf

Similar to Add these three functions to the class binaryTreeType (provided).W.pdf (20)

PPTX
DS group binary tree all information M.pptx
PPT
FRbsbsvvsvsvbshgsgsvzvsvvsvsvsvsvsvvev.ppt
PDF
Please write in C++ and should be able to compile and debug.Thank yo.pdf
PDF
Binary tree in java
PDF
C programming. Answer question only in C code Ninth Deletion with B.pdf
PPTX
Data Str Data Str Data Str Data Str Data Str
PDF
Implementing a basic directory-tree structure that is derived from a.pdf
DOCX
Write a program in Java to implement the ADT Binary Tree part of who.docx
DOCX
For this project, write a program that stores integers in a binary.docx
DOCX
Binary Tree in C++ coding in the data structure
PPTX
RTTI and Namespaces.pptx ppt of c++ programming language
PDF
Need help with the TODO's (DONE IN C++) #pragma once #include -funct.pdf
DOCX
Write a C++ function to delete the given value from the binary search.docx
PDF
Templated Binary Tree implementing function help I need to im.pdf
PDF
Please read the comment ins codeExpressionTree.java-------------.pdf
PDF
I have C++ question that I do not know how to do, Can you teach me t.pdf
PDF
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
PPTX
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
DOCX
DS Code (CWH).docx
PDF
C++ BinaryTree Help Creating main function for Trees...Here are .pdf
DS group binary tree all information M.pptx
FRbsbsvvsvsvbshgsgsvzvsvvsvsvsvsvsvvev.ppt
Please write in C++ and should be able to compile and debug.Thank yo.pdf
Binary tree in java
C programming. Answer question only in C code Ninth Deletion with B.pdf
Data Str Data Str Data Str Data Str Data Str
Implementing a basic directory-tree structure that is derived from a.pdf
Write a program in Java to implement the ADT Binary Tree part of who.docx
For this project, write a program that stores integers in a binary.docx
Binary Tree in C++ coding in the data structure
RTTI and Namespaces.pptx ppt of c++ programming language
Need help with the TODO's (DONE IN C++) #pragma once #include -funct.pdf
Write a C++ function to delete the given value from the binary search.docx
Templated Binary Tree implementing function help I need to im.pdf
Please read the comment ins codeExpressionTree.java-------------.pdf
I have C++ question that I do not know how to do, Can you teach me t.pdf
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
DS Code (CWH).docx
C++ BinaryTree Help Creating main function for Trees...Here are .pdf
Ad

More from indiaartz (20)

PDF
Some Calvin Cycle enzymes contain disulfide bonds that must be reduc.pdf
PDF
Simplify and write the answer with positive exponents Simplify and w.pdf
PDF
Question 7 (of 20) 7. 1000 poants TB 1301 which of the following an.pdf
PDF
Problem 12-9ACondensed financial data of Waterway Industries follo.pdf
PDF
Please help me with these 3 questions! True or False. Assortment of .pdf
PDF
Performance BudgetingPerformance budgeting has been attempted at t.pdf
PDF
Many bridges are made from rebar-reinforced concrete composites. Cla.pdf
PDF
If the cystic fibrosis allele protects against tuberculosis the same .pdf
PDF
Match the graph of the sine function to the equation over [-4 Pi. 4 P.pdf
PDF
location in the stem, function, and Chemical Composition of their Wa.pdf
PDF
If Zn Binomial (n, ) , and n~possion(), find the distribution of Z .pdf
PDF
Given the global financial crisis of 2007–2009, do you anticipate an.pdf
PDF
Generativity versus stagnation        Early adult transitio.pdf
PDF
Genetics Question 2 In the insect sex determination system, th.pdf
PDF
Explain why malaria infection may lead to anemia. How would you clas.pdf
PDF
Figure 13.2 of a single pair of homologous chromosomes as they might.pdf
PDF
Every individual produced by sexual reproduction is unique. This is .pdf
PDF
Explain to them the probability of future offspring being normal or h.pdf
PDF
explain conceptual questionsvariabilitystandard devationz-sc.pdf
PDF
Explain the difference between fermentation and respiration in biolo.pdf
Some Calvin Cycle enzymes contain disulfide bonds that must be reduc.pdf
Simplify and write the answer with positive exponents Simplify and w.pdf
Question 7 (of 20) 7. 1000 poants TB 1301 which of the following an.pdf
Problem 12-9ACondensed financial data of Waterway Industries follo.pdf
Please help me with these 3 questions! True or False. Assortment of .pdf
Performance BudgetingPerformance budgeting has been attempted at t.pdf
Many bridges are made from rebar-reinforced concrete composites. Cla.pdf
If the cystic fibrosis allele protects against tuberculosis the same .pdf
Match the graph of the sine function to the equation over [-4 Pi. 4 P.pdf
location in the stem, function, and Chemical Composition of their Wa.pdf
If Zn Binomial (n, ) , and n~possion(), find the distribution of Z .pdf
Given the global financial crisis of 2007–2009, do you anticipate an.pdf
Generativity versus stagnation        Early adult transitio.pdf
Genetics Question 2 In the insect sex determination system, th.pdf
Explain why malaria infection may lead to anemia. How would you clas.pdf
Figure 13.2 of a single pair of homologous chromosomes as they might.pdf
Every individual produced by sexual reproduction is unique. This is .pdf
Explain to them the probability of future offspring being normal or h.pdf
explain conceptual questionsvariabilitystandard devationz-sc.pdf
Explain the difference between fermentation and respiration in biolo.pdf
Ad

Recently uploaded (20)

PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Pre independence Education in Inndia.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
master seminar digital applications in india
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Institutional Correction lecture only . . .
PPTX
Lesson notes of climatology university.
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
Insiders guide to clinical Medicine.pdf
PDF
Complications of Minimal Access Surgery at WLH
Renaissance Architecture: A Journey from Faith to Humanism
O7-L3 Supply Chain Operations - ICLT Program
Pre independence Education in Inndia.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
GDM (1) (1).pptx small presentation for students
Module 4: Burden of Disease Tutorial Slides S2 2025
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
master seminar digital applications in india
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Institutional Correction lecture only . . .
Lesson notes of climatology university.
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPH.pptx obstetrics and gynecology in nursing
Microbial disease of the cardiovascular and lymphatic systems
Pharma ospi slides which help in ospi learning
Microbial diseases, their pathogenesis and prophylaxis
STATICS OF THE RIGID BODIES Hibbelers.pdf
Cell Structure & Organelles in detailed.
Insiders guide to clinical Medicine.pdf
Complications of Minimal Access Surgery at WLH

Add these three functions to the class binaryTreeType (provided).W.pdf

  • 1. Add these three functions to the class binaryTreeType (provided). Write the definition of the function, nodeCount, that returns the number of nodes in the binary tree. Write the definition of the function, leavesCount, that takes as a parameter a pointer to the root node of a binary tree and returns the number of leaves in a binary tree. Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Print the original tree and the resulting tree using a pre-order traversal. Write a program to test your new functions. Use the data provided to build your binary search tree (-999 is the sentinel): (C++) Data: 65 55 22 44 61 19 90 10 78 52 -999 Already completed code is below: //Header File Binary Search Tree #ifndef H_binarySearchTree #define H_binarySearchTree #include "binaryTree.h" #include using namespace std; template class bSearchTreeType: public binaryTreeType { 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 there is no node in the binary search // tree that has the same info as // insertItem, a node with the info
  • 2. // 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 printed. private: void deleteFromTree(nodeType* &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 bool bSearchTreeType::search (const elemType& searchItem) const { nodeType *current; bool found = false; if (this->root == NULL) cout << "Cannot search an empty tree." << endl; else { current = this->root; while (current != NULL && !found) { if (current->info == searchItem) found = true; else if (current->info > searchItem) current = current->lLink; else
  • 3. current = current->rLink; }//end while }//end else return found; }//end search template void bSearchTreeType::insert (const elemType& insertItem) { nodeType *current; //pointer to traverse the tree nodeType *trailCurrent; //pointer behind current nodeType *newNode; //pointer to create the node newNode = new nodeType; newNode->info = insertItem; newNode->lLink = NULL; newNode->rLink = NULL; if (this->root == NULL) this->root = newNode; else { current = this->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;
  • 4. }//end while if (trailCurrent->info > insertItem) trailCurrent->lLink = newNode; else trailCurrent->rLink = newNode; } }//end insert template void bSearchTreeType::deleteNode (const elemType& deleteItem) { nodeType *current; //pointer to traverse the tree nodeType *trailCurrent; //pointer behind current bool found = false; if (this->root == NULL) cout << "Cannot delete from an empty tree." << endl; else { current = this->root; trailCurrent = this->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."
  • 5. << endl; else if (found) { if (current == this->root) deleteFromTree(this->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 void bSearchTreeType::deleteFromTree (nodeType* &p) { nodeType *current; //pointer to traverse the tree nodeType *trailCurrent; //pointer behind current nodeType *temp; //pointer to delete the node if (p == NULL) cout << "Error: The node to be deleted does not exist." << 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;
  • 6. } 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 //Header File Binary Search Tree #ifndef H_binaryTree #define H_binaryTree #include using namespace std; //Definition of the Node template struct nodeType
  • 7. { elemType info; nodeType *lLink; nodeType *rLink; }; //Definition of the class template class binaryTreeType { public: const binaryTreeType& operator= (const binaryTreeType&); //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. 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;
  • 8. //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& otherTree); //Copy constructor binaryTreeType(); //Default constructor ~binaryTreeType(); //Destructor protected: nodeType *root; private: void copyTree(nodeType* &copiedTreeRoot, nodeType* 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* &p);
  • 9. //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 *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 *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 *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 *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. }; //Definition of member functions template binaryTreeType::binaryTreeType() { root = NULL;
  • 10. } 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); }
  • 11. template void binaryTreeType::copyTree (nodeType* &copiedTreeRoot, nodeType* otherTreeRoot) { if (otherTreeRoot == NULL) copiedTreeRoot = NULL; else { copiedTreeRoot = new nodeType; copiedTreeRoot->info = otherTreeRoot->info; copyTree(copiedTreeRoot->lLink, otherTreeRoot->lLink); copyTree(copiedTreeRoot->rLink, otherTreeRoot->rLink); } } //end copyTree template void binaryTreeType::inorder (nodeType *p) const { if (p != NULL) { inorder(p->lLink); cout << p->info << " "; inorder(p->rLink); } } template void binaryTreeType::preorder (nodeType *p) const { if (p != NULL) { cout << p->info << " "; preorder(p->lLink); preorder(p->rLink); }
  • 12. } template void binaryTreeType::postorder (nodeType *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(nodeType* &p) { if (p != NULL) { destroy(p->lLink); destroy(p->rLink);
  • 13. 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); } //Destructor template binaryTreeType::~binaryTreeType() { destroy(root); } template int binaryTreeType::height (nodeType *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
  • 14. { if (x >= y) return x; else return y; } #endif Solution //Header File Binary Search Tree #ifndef H_binarySearchTree #define H_binarySearchTree #include "binaryTree.hbinaryTree.h" #include using namespace std; template class bSearchTreeType: public binaryTreeType { 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 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
  • 15. //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 printed. private: void deleteFromTree(nodeType* &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 bool bSearchTreeType::search (const elemType& searchItem) const { nodeType *current; bool found = false; if (this->root == NULL) cout << "Cannot search an empty tree." << endl; else { current = this->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
  • 16. template void bSearchTreeType::insert (const elemType& insertItem) { nodeType *current; //pointer to traverse the tree nodeType *trailCurrent; //pointer behind current nodeType *newNode; //pointer to create the node newNode = new nodeType; newNode->info = insertItem; newNode->lLink = NULL; newNode->rLink = NULL; if (this->root == NULL) this->root = newNode; else { current = this->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; }
  • 17. }//end insert template void bSearchTreeType::deleteNode (const elemType& deleteItem) { nodeType *current; //pointer to traverse the tree nodeType *trailCurrent; //pointer behind current bool found = false; if (this->root == NULL) cout << "Cannot delete from an empty tree." << endl; else { current = this->root; trailCurrent = this->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 == this->root) deleteFromTree(this->root); else if (trailCurrent->info > deleteItem)
  • 18. deleteFromTree(trailCurrent->lLink); else deleteFromTree(trailCurrent->rLink); } else cout << "The item to be deleted is not in the tree." << endl; } } //end deleteNode template void bSearchTreeType::deleteFromTree (nodeType* &p) { nodeType *current; //pointer to traverse the tree nodeType *trailCurrent; //pointer behind current nodeType *temp; //pointer to delete the node if (p == NULL) cout << "Error: The node to be deleted does not exist." << 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;
  • 19. } 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 //Header File Binary Search Tree #ifndef H_binaryTree #define H_binaryTree #include using namespace std; //Definition of the Node template struct nodeType { elemType info; nodeType *lLink; nodeType *rLink; }; //Definition of the class template
  • 20. class binaryTreeType { public: const binaryTreeType& operator= (const binaryTreeType&); //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. 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
  • 21. // 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& otherTree); //Copy constructor binaryTreeType(); //Default constructor ~binaryTreeType(); //Destructor protected: nodeType *root; private: void copyTree(nodeType* &copiedTreeRoot, nodeType* 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* &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 *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
  • 22. // points, are printed in inorder sequence. void preorder(nodeType *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 *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 *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. }; //Definition of member functions template binaryTreeType::binaryTreeType() { root = NULL; } template bool binaryTreeType::isEmpty() const { return (root == NULL); } template void binaryTreeType::inorderTraversal() const {
  • 23. 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 (nodeType* &copiedTreeRoot, nodeType* otherTreeRoot) { if (otherTreeRoot == NULL) copiedTreeRoot = NULL; else {
  • 24. copiedTreeRoot = new nodeType; copiedTreeRoot->info = otherTreeRoot->info; copyTree(copiedTreeRoot->lLink, otherTreeRoot->lLink); copyTree(copiedTreeRoot->rLink, otherTreeRoot->rLink); } } //end copyTree template void binaryTreeType::inorder (nodeType *p) const { if (p != NULL) { inorder(p->lLink); cout << p->info << " "; inorder(p->rLink); } } template void binaryTreeType::preorder (nodeType *p) const { if (p != NULL) { cout << p->info << " "; preorder(p->lLink); preorder(p->rLink); } } template void binaryTreeType::postorder (nodeType *p) const { if (p != NULL) { postorder(p->lLink); postorder(p->rLink);
  • 25. 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(nodeType* &p) { if (p != NULL) { destroy(p->lLink); destroy(p->rLink); delete p; p = NULL; } } template void binaryTreeType::destroyTree() { destroy(root); }
  • 26. //copy constructor template binaryTreeType::binaryTreeType (const binaryTreeType& otherTree) { if (otherTree.root == NULL) //otherTree is empty root = NULL; else copyTree(root, otherTree.root); } //Destructor template binaryTreeType::~binaryTreeType() { destroy(root); } template int binaryTreeType::height (nodeType *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::leavesCount(nodeType *node) {
  • 27. if (node == NULL) return 0; else if (node->lLink == NULL && node->rLink == NULL) return 1; else return (leavesCount(node->lLink)) + (leavesCount(node->rLink)); } template int binaryTreeType::nodeCount(nodeType *node) { int count = 1; if (node == NULL) return 0; else { count = count + nodeCount(node->lLink); count = count + nodeCount(node->rLink); return count; } } template void binaryTreeType::swapSubtrees(nodeType *node) { if (node==NULL) return; else { struct nodeType* t; swapSubtrees(node->lLink); swapSubtrees(node->rLink);
  • 28. t = node->lLink; node->lLink = node->rLink; node->rLink = t; } } #endif