Answered step by step
Verified Expert Solution
Link Copied!

Question

...
1 Approved Answer

Data Members: struct Node { Type data; Node * left, * right; Node * parent; / / node Constructor goes here if implemented correctly }

Data Members:
struct Node {
Type data;
Node* left, *right;
Node* parent;
//node Constructor goes here if implemented correctly
}
BST:
mRoot - Pointer to the root (top) node
Methods:
Node Constructor
Node(const Type& _data, Node*_parent = nullptr) : data(_data), parent(_parent){
left = right = nullptr;
}
BST Constructor
BST(){
mRoot = nullptr;
}
Push
void Push(const Type& _val){
Node* newNode = new Node(_val);
if (mRoot == nullptr){
mRoot = newNode;
return;
}
Node* temp = mRoot;
while (temp != nullptr){
if (_val <*temp){
if (temp->left == nullptr){
temp->left = newNode;
break;
}
temp = temp->left;
} else {
if (temp->right == nullptr){
temp->right = newNode;
break;
}
temp = temp->right;
}
}
}
Clear
void Clear(){
Clear(mRoot);
mRoot = nullptr;
}
//Recursive helper method for use with Clear()
void Clear(Node*_curr){
if (_curr == nullptr){
return;
}
Clear(_curr->left);
Clear(_curr->right);
delete _curr;
}
Destructor
~BST(){
Clear();
}
Contains
bool Contains(const Type& _val){
Node* temp = mRoot;
while (temp != nullptr){
if (_val < temp->data){
temp = temp->left;
} else if (_val > temp->data){
temp = temp->right;
} else {
return true; // value found
}
}
return false; // value not found
}
FindNode (Optional)
Node* FindNode(const Type& _val){
Checks to see if a value is present in the tree and returns the address if found
Create a temporary pointer to traverse down the tree
}
RemoveCase0
void RemoveCase0(Node*_node){
Removes a node from the tree that has no children
o Can assume the node passed in is a leaf node
Three sub-cases
o Root nodeo Is a left child
o Is a right child
}
RemoveCase1
void RemoveCase1(Node*_node){
Removes a node from the tree that has one child
o Can assume the node passed in has exactly one child
Six sub-cases
o Root node with left child
o Root node with right child
o Left child that has a left child
o Left child that has a right child
o Right child that has a left child
o Right child that has a right child
}
RemoveCase2
void RemoveCase2(Node*_node){
Removes a node from the tree that has both children
o Can assume the node passed in has both children
This will ultimately lead to a Case0 or Case1 removal
}
Remove
bool Remove(const Type& _val){
Removes a node from the tree by calling the appropriate RemoveCase method
Find the address of the node to be removed (Theres potentially a method for this)
Check to see how many children that node has, and call the appropriate RemoveCase method
Return true, if something was removed
}
InOrder
std::string InOrder(){
Creates a space-delimited string that contains the values of the tree in ascending order
Start with the root and use the recursive InOrder method to build out the string one
value at a time
Use std::tostring to convert the int into its string equivalent
}
//Recursive helper method to help with InOrder
void InOrder(Node*_curr, std::string& _str){
//Implement this method
}
Assignment Operator
BST& operator=(const BST& _assign){
Assigns all values to match those of the object passed in
Clean up existing memory before the deep copy (Theres a method that does this)
Deep copy the entire tree
o Use the recursive Copy method to duplicate the tree with a pre-order traversal
Each recursive call to copy should create a single node by Pushing it
}
Copy Constructor
BST(const BST& _copy){
Assigns all values to match those of the object passed in
Deep copy the entire tree
o Use the recursive Copy method to duplicate the tree with a pre-order traversal
Each recursive call to copy should create a single node by Pushing it
Remember that data members are not automatically initialized in C++
}
//Recursive helper method for use with rule of 3
void Copy(const Node*_curr){
//Implement this method
}
Extra instructions for what some methods should do
Contains should check found and not found
RemoveCase0 should check root, left, right,
RemoveCase1 should check root-left, root-right, left-left, left-right, right-left, right-right
RemoveCase2 case0
RemoveCase2 case1
Check Remove not found
Some of these I have completed, I'm just a little confused about the otherss and was looking for clearer instructions on how to implement these?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started