SlideShare a Scribd company logo
1 of 12
Download to read offline
In c++ format, for each function in the code, please using the comments fill in the code and write
the whole program.
#include
#include
using namespace std;
template
class BST
{
private:
struct Node
{
bstdata data;
Node* left;
Node* right;
Node(bstdata newdata): data(newdata), left(NULL), right(NULL) {}
};
typedef struct Node* NodePtr;
NodePtr root;
/**Private helper functions*/
void insertHelper(NodePtr root, bstdata value);
//private helper function for insert
//recursively inserts a value into the BST
void destructorHelper(NodePtr root);
//private helper function for the destructor
//recursively frees the memory in the BST
void inOrderPrintHelper(NodePtr root);
//private helper function for inOrderPrint
//recursively prints tree values in order from smallest to largest
void preOrderPrintHelper(NodePtr root);
//private helper function for preOrderPrint
//recursively prints tree values in preorder
void postOrderPrintHelper(NodePtr root);
//private helper function for postOrderPrint
//recursively prints tree values in postorder
bstdata maximumHelper(NodePtr root);
//recursively searches for the maximum value in the Binary Search Tree
bstdata minimumHelper(NodePtr root);
//recursively locates the minimum value in the tree
//returns this value once it is located
void getSizeHelper(Nodeptr root, int& size);
//recursively calculates the size of the tree
int getHeightHelper(NodePtr root);
//recursively calculates the height of the tree
bool findHelper(NodePtr root, bstdata value);
//recursively searches for value in the tree
void remove(NodePtr root, bstdata value);
//recursively removes the specified value from the tree
void copyHelper(NodePtr copy);
//recursively makes a deep copy of a binary search tree
/**Public functions*/
public:
add the constructor
BST();
//Instantiates a new Binary Search Tree
//post: a new Binary Search Tree object
Add the following copy constructor:
BST(const BST& tree);
//makes a deep copy of tree
//Calls the copyHelper function to make a copy recursively
destructor
~BST();
//frees the memory of the BST object
//All memory has been deallocated
bool isEmpty();
//determines whether the Binary Search Tree is empty
void insert(bstdata value);
//inserts a new value into the Binary Search Tree
//post: a new value inserted into the Binary Search Tree
bstdata getRoot();
//returns the value stored at the root of the Binary Search Tree
//pre: the Binary Search Tree is not empty
void inOrderPrint();
//calls the inOrderPrintHelper function to print out the values
//stored in the Binary Search Tree
//If the tree is empty, prints nothing
void preOrderPrint();
//calls the preOrderPrintHelper function to print out the values
//stored in the Binary Search Tree
//If the tree is empty, prints nothing
void postOrderPrint();
//calls the postOrderPrintHelper function to print out the values
//stored in the Binary Search Tree
//If the tree is empty, prints nothing
bstdata maximum();
//finds the maximum value in the Binary Search Tree and returns it
//calls the maximumHelper function to search for the max recursively
//pre: !isEmpty()
bstdata minimum();
//calls the minimumHelper function to return the minimum value in the tree
//Pre: the tree is not empty
int getSize();
//returns the size of the tree
//calls the getSizeHelper function to calculate the size recursively
int getHeight();
//recursively finds the height of the tree and returns it
//calls the getHeight helper function to calculate the height recursively
//returns -1 if the tree is empty
bool find(bstdata value);
//returns whether the value is in the tree
//calls the findHelper function to search for the value recursively
//Pre: !isEmpty()
void remove(bstdata value);
//removes the specified value from the tree
//Pre: !isEmpty()
//Pre: The value is contained in the Binary Search Tree
//If the value is not in the Binary Search Tree, the tree is left unchanged
};
Solution
BinarySearchTree.h completed file:
#ifndef BINARYSEARCHTREE_H_
#define BINARYSEARCHTREE_H_
#include
#include
#include
using namespace std;
template
class BinarySearchTree {
private:
struct Node {
bstitem data;
Node* left;
Node* right;
Node(bstitem newdata) :
data(newdata), left(NULL), right(NULL) {
}
};
typedef struct Node* NodePtr;
NodePtr root;
/**Private helper functions*/
void insertHelper(NodePtr root, bstitem value);
//private helper function for insert
//recursively inserts a value into the BST
void inOrderPrintHelper(NodePtr root);
//private helper function for inOrderPrint
//recursively prints tree values in order from smallest to largest
void preOrderPrintHelper(NodePtr root);
//private helper function for preOrderPrint
//recursively prints tree values in preorder
void postOrderPrintHelper(NodePtr root);
//private helper function for postOrderPrint
//recursively prints tree values in postorder
bool findHelper(NodePtr root, bstitem value);
bstitem minimumHelper(NodePtr root);
bstitem maximumHelper(NodePtr root);
NodePtr removeHelper(NodePtr root, bstitem value);
int getSizeHelper(NodePtr root);
int getHeightHelper(NodePtr root);
void destructorHelper(NodePtr root);
Node& copyHelper(NodePtr root, const BinarySearchTree &binarysearchtree);
/**Public functions*/
public:
BinarySearchTree();
//Instantiates a new Binary Search Tree
//post: a new Binary Search Tree object
~BinarySearchTree();
BinarySearchTree(const BinarySearchTree &binarysearchtree);
//Access functions
bstitem minimum();
//finds the minimum value in the Binary Search Tree and returns it
//pre: !isEmpty()
bstitem maximum();
//finds the maximum value in the Binary Search Tree and returns it
//pre: !isEmpty()
bool isEmpty();
//returns whether the tree is empty
int getSize();
//returns the size of the tree
bstitem getRoot();
//returns the value stored at the root of the tree
//Pre: !isEmpty()
int getHeight();
//recursively finds the height of the tree and returns it
//Pre: !isEmpty()
bool find(bstitem value);
//returns whether the value is in the tree
//Pre: !isEmpty()
//Maniupulation functions
void insert(bstitem value);
//adds the specified value to the tree
void remove(bstitem value);
//removes the specified value from the tree
//Pre: !isEmpty()
//Pre: The value is contained in the Binary Search Tree
//Additional functions
void inOrderPrint();
// recursively prints the values contained in the Binary Search Tree
// according to an in order traversal
void preOrderPrint();
//recursively prints the values contained in the Binary Search Tree
// according to a pre order traversal
void postOrderPrint();
//recursively prints the values contained in the Binary Search Tree
// according to a post order traversal
};
template
BinarySearchTree::BinarySearchTree() :
root(NULL) {
}
template
void BinarySearchTree::destructorHelper(NodePtr root) {
if (root) {
if (root->left)
destructorHelper(root->left);
if (root->right)
destructorHelper(root->right);
delete root;
}
}
template
BinarySearchTree::~BinarySearchTree() {
destructorHelper(root);
}
/*template
Node& BinarySearchTree::copyHelper(NodePtr copyRoot, NodePtr root) {
if (root == NULL)
copyRoot = NULL;
else {
copyRoot = new Node(root->data);
copyRoot->left = copyHelper(copyRoot->left, root->left);
copyRoot->right = copyHelper(copyRoot->right, root->right);
}
}
template
BinarySearchTree::BinarySearchTree(
const BinarySearchTree &binarysearchtree) {
root = copyHelper(root, &binarysearchtree);
}*/
template
void BinarySearchTree::insert(bstitem value) {
{
if (root == NULL) {
root = new Node(value);
} else {
insertHelper(root, value);
}
}
}
template
void BinarySearchTree::insertHelper(NodePtr root, bstitem value) {
if (value == root->data)
return;
else if (value < root->data) {
if (root->left == NULL)
root->left = new Node(value);
else
insertHelper(root->left, value);
} else {
if (root->right == NULL)
root->right = new Node(value);
else
insertHelper(root->right, value);
}
}
template
void BinarySearchTree::inOrderPrintHelper(NodePtr root) {
if (root != NULL) {
inOrderPrintHelper(root->left);
cout << root->data << " ";
inOrderPrintHelper(root->right);
}
}
template
void BinarySearchTree::inOrderPrint() {
inOrderPrintHelper(root);
cout << endl;
}
template
void BinarySearchTree::preOrderPrintHelper(NodePtr root) {
if (root != NULL) {
cout << root->data << " ";
preOrderPrintHelper(root->left);
preOrderPrintHelper(root->right);
}
}
template
void BinarySearchTree::preOrderPrint() {
preOrderPrintHelper(root);
cout << endl;
}
template
void BinarySearchTree::postOrderPrintHelper(NodePtr root) {
if (root != NULL) {
postOrderPrintHelper(root->left);
postOrderPrintHelper(root->right);
cout << root->data << " ";
}
}
template
void BinarySearchTree::postOrderPrint() {
postOrderPrintHelper(root);
cout << endl;
}
template
bool BinarySearchTree::findHelper(NodePtr root, bstitem value) {
if (value == root->data)
return true;
else if (value < root->data) {
if (root->left == NULL)
return false;
else
findHelper(root->left, value);
} else {
if (root->right == NULL)
return false;
else
findHelper(root->right, value);
}
}
template
bool BinarySearchTree::find(bstitem value) {
assert(!isEmpty());
if (value == root->data)
return true;
else
return findHelper(root, value);
}
template
bstitem BinarySearchTree::minimumHelper(NodePtr root) {
while (root->left != NULL) {
root = root->left;
}
return root->data;
}
template
bstitem BinarySearchTree::minimum() {
assert(!isEmpty());
return minimumHelper(root);
}
template
bstitem BinarySearchTree::maximumHelper(NodePtr root) {
while (root->right != NULL) {
root = root->right;
}
return root->data;
}
template
bstitem BinarySearchTree::maximum() {
assert(!isEmpty());
return maximumHelper(root);
}
template
bool BinarySearchTree::isEmpty() {
return (root == NULL);
}
template
bstitem BinarySearchTree::getRoot() {
assert(!isEmpty());
return root->data;
}
template
int BinarySearchTree::getSizeHelper(NodePtr root) {
if (root == NULL) {
return 0;
} else {
return (1 + getSizeHelper(root->left) + getSizeHelper(root->right));
}
}
template
int BinarySearchTree::getSize() {
return getSizeHelper(root);
}
template
typename BinarySearchTree::NodePtr BinarySearchTree::removeHelper(
NodePtr root, bstitem value) {
if (root == NULL)
return root;
else if (value < root->data)
root->left = removeHelper(root->left, value);
else if (value > root->data)
root->right = removeHelper(root->right, value);
else {
if (root->left == NULL && root->right == NULL) {
delete root;
root = NULL;
} else if (root->left != NULL && root->right == NULL) {
NodePtr temp = root;
root = root->left;
delete temp;
} else if (root->left == NULL && root->right != NULL) {
NodePtr temp = root;
root = root->right;
delete temp;
} else {
root->data = minimumHelper(root->right);
root->right = removeHelper(root->right, minimumHelper(root->right));
}
}
return root;
}
template
void BinarySearchTree::remove(bstitem value) {
assert(!isEmpty());
assert(find(value));
root = removeHelper(root, value);
}
template
int BinarySearchTree::getHeightHelper(NodePtr root) {
if (root == NULL) {
return -1;
} else {
return (max(getHeightHelper(root->left), getHeightHelper(root->right))
+ 1);
}
}
template
int BinarySearchTree::getHeight() {
assert(!isEmpty());
return getHeightHelper(root);
}
#endif /* BINARYSEARCHTREE_H_ */

More Related Content

Similar to In c++ format, for each function in the code, please using the comme.pdf

Templated Binary Tree implementing function help I need to im.pdf
Templated Binary Tree implementing function help I need to im.pdfTemplated Binary Tree implementing function help I need to im.pdf
Templated Binary Tree implementing function help I need to im.pdf
manjan6
 
Please read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdfPlease read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdf
shanki7
 
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdfA)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
anton291
 
create a binary search tree from an empty one by adding the key valu.pdf
create a binary search tree from an empty one by adding the key valu.pdfcreate a binary search tree from an empty one by adding the key valu.pdf
create a binary search tree from an empty one by adding the key valu.pdf
erremmfab
 
Need help with the TODO's (DONE IN C++) #pragma once #include -funct.pdf
Need help with the TODO's (DONE IN C++) #pragma once   #include -funct.pdfNeed help with the TODO's (DONE IN C++) #pragma once   #include -funct.pdf
Need help with the TODO's (DONE IN C++) #pragma once #include -funct.pdf
actexerode
 
Write a C++ program that implements a binary search tree (BST) to man.pdf
Write a C++ program that implements a binary search tree (BST) to man.pdfWrite a C++ program that implements a binary search tree (BST) to man.pdf
Write a C++ program that implements a binary search tree (BST) to man.pdf
hardjasonoco14599
 
Required to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docxRequired to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docx
debishakespeare
 
Once you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docxOnce you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docx
farrahkur54
 
Implementing a basic directory-tree structure that is derived from a.pdf
Implementing a basic directory-tree structure that is derived from a.pdfImplementing a basic directory-tree structure that is derived from a.pdf
Implementing a basic directory-tree structure that is derived from a.pdf
funkybabyindia
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdf
michardsonkhaicarr37
 
Create an implementation of a binary tree using the recursive appr.pdf
Create an implementation of a binary tree using the recursive appr.pdfCreate an implementation of a binary tree using the recursive appr.pdf
Create an implementation of a binary tree using the recursive appr.pdf
federaleyecare
 
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdfC++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
rohit219406
 
Write a program in Java to implement the ADT Binary Tree part of who.docx
Write a program in Java to implement the ADT Binary Tree part of who.docxWrite a program in Java to implement the ADT Binary Tree part of who.docx
Write a program in Java to implement the ADT Binary Tree part of who.docx
rochellwa9f
 
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
arihantmobileselepun
 
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
rambagra74
 
RightTrianglerightTriangle.cppRightTrianglerightTriangle.cpp.docx
RightTrianglerightTriangle.cppRightTrianglerightTriangle.cpp.docxRightTrianglerightTriangle.cppRightTrianglerightTriangle.cpp.docx
RightTrianglerightTriangle.cppRightTrianglerightTriangle.cpp.docx
joellemurphey
 
C++ BinaryTree Help Creating main function for Trees...Here are .pdf
C++ BinaryTree Help  Creating main function for Trees...Here are .pdfC++ BinaryTree Help  Creating main function for Trees...Here are .pdf
C++ BinaryTree Help Creating main function for Trees...Here are .pdf
forecastfashions
 
Data StructuresPlease I need help completing this c++ program..pdf
Data StructuresPlease I need help completing this c++ program..pdfData StructuresPlease I need help completing this c++ program..pdf
Data StructuresPlease I need help completing this c++ program..pdf
arkleatheray
 
maincpp Build and procees a sorted linked list of Patie.pdf
maincpp   Build and procees a sorted linked list of Patie.pdfmaincpp   Build and procees a sorted linked list of Patie.pdf
maincpp Build and procees a sorted linked list of Patie.pdf
adityastores21
 

Similar to In c++ format, for each function in the code, please using the comme.pdf (20)

Templated Binary Tree implementing function help I need to im.pdf
Templated Binary Tree implementing function help I need to im.pdfTemplated Binary Tree implementing function help I need to im.pdf
Templated Binary Tree implementing function help I need to im.pdf
 
Please read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdfPlease read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdf
 
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdfA)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
 
create a binary search tree from an empty one by adding the key valu.pdf
create a binary search tree from an empty one by adding the key valu.pdfcreate a binary search tree from an empty one by adding the key valu.pdf
create a binary search tree from an empty one by adding the key valu.pdf
 
Need help with the TODO's (DONE IN C++) #pragma once #include -funct.pdf
Need help with the TODO's (DONE IN C++) #pragma once   #include -funct.pdfNeed help with the TODO's (DONE IN C++) #pragma once   #include -funct.pdf
Need help with the TODO's (DONE IN C++) #pragma once #include -funct.pdf
 
Write a C++ program that implements a binary search tree (BST) to man.pdf
Write a C++ program that implements a binary search tree (BST) to man.pdfWrite a C++ program that implements a binary search tree (BST) to man.pdf
Write a C++ program that implements a binary search tree (BST) to man.pdf
 
Required to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docxRequired to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docx
 
Once you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docxOnce you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docx
 
Implementing a basic directory-tree structure that is derived from a.pdf
Implementing a basic directory-tree structure that is derived from a.pdfImplementing a basic directory-tree structure that is derived from a.pdf
Implementing a basic directory-tree structure that is derived from a.pdf
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdf
 
Create an implementation of a binary tree using the recursive appr.pdf
Create an implementation of a binary tree using the recursive appr.pdfCreate an implementation of a binary tree using the recursive appr.pdf
Create an implementation of a binary tree using the recursive appr.pdf
 
write recursive function that calculates and returns the length of a.pdf
write recursive function that calculates and returns the length of a.pdfwrite recursive function that calculates and returns the length of a.pdf
write recursive function that calculates and returns the length of a.pdf
 
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdfC++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
 
Write a program in Java to implement the ADT Binary Tree part of who.docx
Write a program in Java to implement the ADT Binary Tree part of who.docxWrite a program in Java to implement the ADT Binary Tree part of who.docx
Write a program in Java to implement the ADT Binary Tree part of who.docx
 
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
 
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
 
RightTrianglerightTriangle.cppRightTrianglerightTriangle.cpp.docx
RightTrianglerightTriangle.cppRightTrianglerightTriangle.cpp.docxRightTrianglerightTriangle.cppRightTrianglerightTriangle.cpp.docx
RightTrianglerightTriangle.cppRightTrianglerightTriangle.cpp.docx
 
C++ BinaryTree Help Creating main function for Trees...Here are .pdf
C++ BinaryTree Help  Creating main function for Trees...Here are .pdfC++ BinaryTree Help  Creating main function for Trees...Here are .pdf
C++ BinaryTree Help Creating main function for Trees...Here are .pdf
 
Data StructuresPlease I need help completing this c++ program..pdf
Data StructuresPlease I need help completing this c++ program..pdfData StructuresPlease I need help completing this c++ program..pdf
Data StructuresPlease I need help completing this c++ program..pdf
 
maincpp Build and procees a sorted linked list of Patie.pdf
maincpp   Build and procees a sorted linked list of Patie.pdfmaincpp   Build and procees a sorted linked list of Patie.pdf
maincpp Build and procees a sorted linked list of Patie.pdf
 

More from rajkumarm401

Having issues with passing my values through different functions aft.pdf
Having issues with passing my values through different functions aft.pdfHaving issues with passing my values through different functions aft.pdf
Having issues with passing my values through different functions aft.pdf
rajkumarm401
 
eee230 Instruction details Answer the following questions in a typed.pdf
eee230 Instruction details Answer the following questions in a typed.pdfeee230 Instruction details Answer the following questions in a typed.pdf
eee230 Instruction details Answer the following questions in a typed.pdf
rajkumarm401
 
Essay questionPorter Combining business strategy What is itmeani.pdf
Essay questionPorter Combining business strategy What is itmeani.pdfEssay questionPorter Combining business strategy What is itmeani.pdf
Essay questionPorter Combining business strategy What is itmeani.pdf
rajkumarm401
 
Describe the primary differences between WEP, WPA, and WPA2 protocol.pdf
Describe the primary differences between WEP, WPA, and WPA2 protocol.pdfDescribe the primary differences between WEP, WPA, and WPA2 protocol.pdf
Describe the primary differences between WEP, WPA, and WPA2 protocol.pdf
rajkumarm401
 
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
Complete the provided partial C++ Linked List program. Main.cpp is g.pdfComplete the provided partial C++ Linked List program. Main.cpp is g.pdf
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
rajkumarm401
 
Click the desktop shortcut icon that you created in this module, and .pdf
Click the desktop shortcut icon that you created in this module, and .pdfClick the desktop shortcut icon that you created in this module, and .pdf
Click the desktop shortcut icon that you created in this module, and .pdf
rajkumarm401
 
What was the causes of the Vietnam war What was the causes of .pdf
What was the causes of the Vietnam war What was the causes of .pdfWhat was the causes of the Vietnam war What was the causes of .pdf
What was the causes of the Vietnam war What was the causes of .pdf
rajkumarm401
 
This is for an homework assignment using Java code. Here is the home.pdf
This is for an homework assignment using Java code. Here is the home.pdfThis is for an homework assignment using Java code. Here is the home.pdf
This is for an homework assignment using Java code. Here is the home.pdf
rajkumarm401
 
Program Structure declare ButtonState Global variable holding statu.pdf
Program Structure declare ButtonState Global variable holding statu.pdfProgram Structure declare ButtonState Global variable holding statu.pdf
Program Structure declare ButtonState Global variable holding statu.pdf
rajkumarm401
 
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdfObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
rajkumarm401
 

More from rajkumarm401 (20)

Having issues with passing my values through different functions aft.pdf
Having issues with passing my values through different functions aft.pdfHaving issues with passing my values through different functions aft.pdf
Having issues with passing my values through different functions aft.pdf
 
Explain the characterstic of web service techonolgy.SolutionT.pdf
Explain the characterstic of web service techonolgy.SolutionT.pdfExplain the characterstic of web service techonolgy.SolutionT.pdf
Explain the characterstic of web service techonolgy.SolutionT.pdf
 
Executive Summary i. Provide a succinct overview of your strategic p.pdf
Executive Summary i. Provide a succinct overview of your strategic p.pdfExecutive Summary i. Provide a succinct overview of your strategic p.pdf
Executive Summary i. Provide a succinct overview of your strategic p.pdf
 
eee230 Instruction details Answer the following questions in a typed.pdf
eee230 Instruction details Answer the following questions in a typed.pdfeee230 Instruction details Answer the following questions in a typed.pdf
eee230 Instruction details Answer the following questions in a typed.pdf
 
Essay questionPorter Combining business strategy What is itmeani.pdf
Essay questionPorter Combining business strategy What is itmeani.pdfEssay questionPorter Combining business strategy What is itmeani.pdf
Essay questionPorter Combining business strategy What is itmeani.pdf
 
During oogenesis, will the genotypes of the first and second polar b.pdf
During oogenesis, will the genotypes of the first and second polar b.pdfDuring oogenesis, will the genotypes of the first and second polar b.pdf
During oogenesis, will the genotypes of the first and second polar b.pdf
 
Does personal information available on the Internet make an employee.pdf
Does personal information available on the Internet make an employee.pdfDoes personal information available on the Internet make an employee.pdf
Does personal information available on the Internet make an employee.pdf
 
Determine whether each series is convergent or divergent..pdf
Determine whether each series is convergent or divergent..pdfDetermine whether each series is convergent or divergent..pdf
Determine whether each series is convergent or divergent..pdf
 
Describe the primary differences between WEP, WPA, and WPA2 protocol.pdf
Describe the primary differences between WEP, WPA, and WPA2 protocol.pdfDescribe the primary differences between WEP, WPA, and WPA2 protocol.pdf
Describe the primary differences between WEP, WPA, and WPA2 protocol.pdf
 
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
Complete the provided partial C++ Linked List program. Main.cpp is g.pdfComplete the provided partial C++ Linked List program. Main.cpp is g.pdf
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
 
Click the desktop shortcut icon that you created in this module, and .pdf
Click the desktop shortcut icon that you created in this module, and .pdfClick the desktop shortcut icon that you created in this module, and .pdf
Click the desktop shortcut icon that you created in this module, and .pdf
 
YOU DO IT 4! create an named YouDolt 4 and save it in the vB 2015Chap.pdf
YOU DO IT 4! create an named YouDolt 4 and save it in the vB 2015Chap.pdfYOU DO IT 4! create an named YouDolt 4 and save it in the vB 2015Chap.pdf
YOU DO IT 4! create an named YouDolt 4 and save it in the vB 2015Chap.pdf
 
What was the causes of the Vietnam war What was the causes of .pdf
What was the causes of the Vietnam war What was the causes of .pdfWhat was the causes of the Vietnam war What was the causes of .pdf
What was the causes of the Vietnam war What was the causes of .pdf
 
Transactions costs are zero in financial markets. zero in financial i.pdf
Transactions costs are zero in financial markets. zero in financial i.pdfTransactions costs are zero in financial markets. zero in financial i.pdf
Transactions costs are zero in financial markets. zero in financial i.pdf
 
This is for an homework assignment using Java code. Here is the home.pdf
This is for an homework assignment using Java code. Here is the home.pdfThis is for an homework assignment using Java code. Here is the home.pdf
This is for an homework assignment using Java code. Here is the home.pdf
 
Question 34 (1 point) D A check is 1) not money because it is not off.pdf
Question 34 (1 point) D A check is 1) not money because it is not off.pdfQuestion 34 (1 point) D A check is 1) not money because it is not off.pdf
Question 34 (1 point) D A check is 1) not money because it is not off.pdf
 
Program Structure declare ButtonState Global variable holding statu.pdf
Program Structure declare ButtonState Global variable holding statu.pdfProgram Structure declare ButtonState Global variable holding statu.pdf
Program Structure declare ButtonState Global variable holding statu.pdf
 
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdfObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
 
Need help in assembly. Thank you Implement the following expression .pdf
Need help in assembly. Thank you Implement the following expression .pdfNeed help in assembly. Thank you Implement the following expression .pdf
Need help in assembly. Thank you Implement the following expression .pdf
 
mine whether the following s are true or false False A parabola has e.pdf
mine whether the following s are true or false False A parabola has e.pdfmine whether the following s are true or false False A parabola has e.pdf
mine whether the following s are true or false False A parabola has e.pdf
 

Recently uploaded

Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
中 央社
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 

Recently uploaded (20)

Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 

In c++ format, for each function in the code, please using the comme.pdf

  • 1. In c++ format, for each function in the code, please using the comments fill in the code and write the whole program. #include #include using namespace std; template class BST { private: struct Node { bstdata data; Node* left; Node* right; Node(bstdata newdata): data(newdata), left(NULL), right(NULL) {} }; typedef struct Node* NodePtr; NodePtr root; /**Private helper functions*/ void insertHelper(NodePtr root, bstdata value); //private helper function for insert //recursively inserts a value into the BST void destructorHelper(NodePtr root); //private helper function for the destructor //recursively frees the memory in the BST void inOrderPrintHelper(NodePtr root); //private helper function for inOrderPrint //recursively prints tree values in order from smallest to largest void preOrderPrintHelper(NodePtr root); //private helper function for preOrderPrint //recursively prints tree values in preorder void postOrderPrintHelper(NodePtr root); //private helper function for postOrderPrint //recursively prints tree values in postorder
  • 2. bstdata maximumHelper(NodePtr root); //recursively searches for the maximum value in the Binary Search Tree bstdata minimumHelper(NodePtr root); //recursively locates the minimum value in the tree //returns this value once it is located void getSizeHelper(Nodeptr root, int& size); //recursively calculates the size of the tree int getHeightHelper(NodePtr root); //recursively calculates the height of the tree bool findHelper(NodePtr root, bstdata value); //recursively searches for value in the tree void remove(NodePtr root, bstdata value); //recursively removes the specified value from the tree void copyHelper(NodePtr copy); //recursively makes a deep copy of a binary search tree /**Public functions*/ public: add the constructor BST(); //Instantiates a new Binary Search Tree //post: a new Binary Search Tree object Add the following copy constructor: BST(const BST& tree); //makes a deep copy of tree //Calls the copyHelper function to make a copy recursively destructor ~BST(); //frees the memory of the BST object //All memory has been deallocated bool isEmpty(); //determines whether the Binary Search Tree is empty void insert(bstdata value); //inserts a new value into the Binary Search Tree
  • 3. //post: a new value inserted into the Binary Search Tree bstdata getRoot(); //returns the value stored at the root of the Binary Search Tree //pre: the Binary Search Tree is not empty void inOrderPrint(); //calls the inOrderPrintHelper function to print out the values //stored in the Binary Search Tree //If the tree is empty, prints nothing void preOrderPrint(); //calls the preOrderPrintHelper function to print out the values //stored in the Binary Search Tree //If the tree is empty, prints nothing void postOrderPrint(); //calls the postOrderPrintHelper function to print out the values //stored in the Binary Search Tree //If the tree is empty, prints nothing bstdata maximum(); //finds the maximum value in the Binary Search Tree and returns it //calls the maximumHelper function to search for the max recursively //pre: !isEmpty() bstdata minimum(); //calls the minimumHelper function to return the minimum value in the tree //Pre: the tree is not empty int getSize(); //returns the size of the tree //calls the getSizeHelper function to calculate the size recursively int getHeight(); //recursively finds the height of the tree and returns it //calls the getHeight helper function to calculate the height recursively //returns -1 if the tree is empty bool find(bstdata value); //returns whether the value is in the tree //calls the findHelper function to search for the value recursively //Pre: !isEmpty()
  • 4. void remove(bstdata value); //removes the specified value from the tree //Pre: !isEmpty() //Pre: The value is contained in the Binary Search Tree //If the value is not in the Binary Search Tree, the tree is left unchanged }; Solution BinarySearchTree.h completed file: #ifndef BINARYSEARCHTREE_H_ #define BINARYSEARCHTREE_H_ #include #include #include using namespace std; template class BinarySearchTree { private: struct Node { bstitem data; Node* left; Node* right; Node(bstitem newdata) : data(newdata), left(NULL), right(NULL) { } }; typedef struct Node* NodePtr; NodePtr root; /**Private helper functions*/ void insertHelper(NodePtr root, bstitem value); //private helper function for insert //recursively inserts a value into the BST void inOrderPrintHelper(NodePtr root); //private helper function for inOrderPrint
  • 5. //recursively prints tree values in order from smallest to largest void preOrderPrintHelper(NodePtr root); //private helper function for preOrderPrint //recursively prints tree values in preorder void postOrderPrintHelper(NodePtr root); //private helper function for postOrderPrint //recursively prints tree values in postorder bool findHelper(NodePtr root, bstitem value); bstitem minimumHelper(NodePtr root); bstitem maximumHelper(NodePtr root); NodePtr removeHelper(NodePtr root, bstitem value); int getSizeHelper(NodePtr root); int getHeightHelper(NodePtr root); void destructorHelper(NodePtr root); Node& copyHelper(NodePtr root, const BinarySearchTree &binarysearchtree); /**Public functions*/ public: BinarySearchTree(); //Instantiates a new Binary Search Tree //post: a new Binary Search Tree object ~BinarySearchTree(); BinarySearchTree(const BinarySearchTree &binarysearchtree); //Access functions bstitem minimum(); //finds the minimum value in the Binary Search Tree and returns it //pre: !isEmpty() bstitem maximum(); //finds the maximum value in the Binary Search Tree and returns it //pre: !isEmpty() bool isEmpty(); //returns whether the tree is empty int getSize(); //returns the size of the tree bstitem getRoot(); //returns the value stored at the root of the tree //Pre: !isEmpty()
  • 6. int getHeight(); //recursively finds the height of the tree and returns it //Pre: !isEmpty() bool find(bstitem value); //returns whether the value is in the tree //Pre: !isEmpty() //Maniupulation functions void insert(bstitem value); //adds the specified value to the tree void remove(bstitem value); //removes the specified value from the tree //Pre: !isEmpty() //Pre: The value is contained in the Binary Search Tree //Additional functions void inOrderPrint(); // recursively prints the values contained in the Binary Search Tree // according to an in order traversal void preOrderPrint(); //recursively prints the values contained in the Binary Search Tree // according to a pre order traversal void postOrderPrint(); //recursively prints the values contained in the Binary Search Tree // according to a post order traversal }; template BinarySearchTree::BinarySearchTree() : root(NULL) { } template void BinarySearchTree::destructorHelper(NodePtr root) { if (root) { if (root->left) destructorHelper(root->left); if (root->right) destructorHelper(root->right); delete root;
  • 7. } } template BinarySearchTree::~BinarySearchTree() { destructorHelper(root); } /*template Node& BinarySearchTree::copyHelper(NodePtr copyRoot, NodePtr root) { if (root == NULL) copyRoot = NULL; else { copyRoot = new Node(root->data); copyRoot->left = copyHelper(copyRoot->left, root->left); copyRoot->right = copyHelper(copyRoot->right, root->right); } } template BinarySearchTree::BinarySearchTree( const BinarySearchTree &binarysearchtree) { root = copyHelper(root, &binarysearchtree); }*/ template void BinarySearchTree::insert(bstitem value) { { if (root == NULL) { root = new Node(value); } else { insertHelper(root, value); } } } template void BinarySearchTree::insertHelper(NodePtr root, bstitem value) { if (value == root->data) return; else if (value < root->data) {
  • 8. if (root->left == NULL) root->left = new Node(value); else insertHelper(root->left, value); } else { if (root->right == NULL) root->right = new Node(value); else insertHelper(root->right, value); } } template void BinarySearchTree::inOrderPrintHelper(NodePtr root) { if (root != NULL) { inOrderPrintHelper(root->left); cout << root->data << " "; inOrderPrintHelper(root->right); } } template void BinarySearchTree::inOrderPrint() { inOrderPrintHelper(root); cout << endl; } template void BinarySearchTree::preOrderPrintHelper(NodePtr root) { if (root != NULL) { cout << root->data << " "; preOrderPrintHelper(root->left); preOrderPrintHelper(root->right); } } template void BinarySearchTree::preOrderPrint() { preOrderPrintHelper(root); cout << endl;
  • 9. } template void BinarySearchTree::postOrderPrintHelper(NodePtr root) { if (root != NULL) { postOrderPrintHelper(root->left); postOrderPrintHelper(root->right); cout << root->data << " "; } } template void BinarySearchTree::postOrderPrint() { postOrderPrintHelper(root); cout << endl; } template bool BinarySearchTree::findHelper(NodePtr root, bstitem value) { if (value == root->data) return true; else if (value < root->data) { if (root->left == NULL) return false; else findHelper(root->left, value); } else { if (root->right == NULL) return false; else findHelper(root->right, value); } } template bool BinarySearchTree::find(bstitem value) { assert(!isEmpty()); if (value == root->data) return true; else
  • 10. return findHelper(root, value); } template bstitem BinarySearchTree::minimumHelper(NodePtr root) { while (root->left != NULL) { root = root->left; } return root->data; } template bstitem BinarySearchTree::minimum() { assert(!isEmpty()); return minimumHelper(root); } template bstitem BinarySearchTree::maximumHelper(NodePtr root) { while (root->right != NULL) { root = root->right; } return root->data; } template bstitem BinarySearchTree::maximum() { assert(!isEmpty()); return maximumHelper(root); } template bool BinarySearchTree::isEmpty() { return (root == NULL); } template bstitem BinarySearchTree::getRoot() { assert(!isEmpty()); return root->data; } template
  • 11. int BinarySearchTree::getSizeHelper(NodePtr root) { if (root == NULL) { return 0; } else { return (1 + getSizeHelper(root->left) + getSizeHelper(root->right)); } } template int BinarySearchTree::getSize() { return getSizeHelper(root); } template typename BinarySearchTree::NodePtr BinarySearchTree::removeHelper( NodePtr root, bstitem value) { if (root == NULL) return root; else if (value < root->data) root->left = removeHelper(root->left, value); else if (value > root->data) root->right = removeHelper(root->right, value); else { if (root->left == NULL && root->right == NULL) { delete root; root = NULL; } else if (root->left != NULL && root->right == NULL) { NodePtr temp = root; root = root->left; delete temp; } else if (root->left == NULL && root->right != NULL) { NodePtr temp = root; root = root->right; delete temp; } else { root->data = minimumHelper(root->right); root->right = removeHelper(root->right, minimumHelper(root->right)); }
  • 12. } return root; } template void BinarySearchTree::remove(bstitem value) { assert(!isEmpty()); assert(find(value)); root = removeHelper(root, value); } template int BinarySearchTree::getHeightHelper(NodePtr root) { if (root == NULL) { return -1; } else { return (max(getHeightHelper(root->left), getHeightHelper(root->right)) + 1); } } template int BinarySearchTree::getHeight() { assert(!isEmpty()); return getHeightHelper(root); } #endif /* BINARYSEARCHTREE_H_ */