Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Develop Two Functions: 1 . void deleteTree ( BSTnode * root ) Delete an entire Binary Search Tree. 2 . void deleteNode ( BSTnode *

Develop Two Functions:
1. void deleteTree(BSTnode *root)
Delete an entire Binary Search Tree.
2. void deleteNode(BSTnode *root, int value)
Delete a Binary Search Tree node with the given while maintaining the BST. Each node in the Binary Search Tree will have a unique value. If the value is not in the BST, print: "There is no node in the Binary Search Tree with that value." This is to be added to our code definition of BSTs in class, featured below:
#include
#include
#include
using namespace std;
struct BSTnode {
int data;
BSTnode *left;
BSTnode *right;
};
BSTnode *addNode(BSTnode *root, int value){
if (root == nullptr){
root = new BSTnode;
root->left = nullptr;
root->right = nullptr;
root->data = value;
return root;
} else if (value < root->data){
root->left = addNode(root->left, value);
return root;
} else if (value > root->data){
root->right = addNode(root->right, value);
return root;
}
return root;
}
bool containsValue(BSTnode *root, int value){
if (root->data == value){
return true;
} else if (root->data > value){
if (root->left == nullptr){
return false;
} else {
return containsValue(root->left,value);
}
} else if (root -> data < value){
if (root -> right == nullptr){
return false;
} else {
return containsValue(root->right, value);
}
}
return false;
}
BSTnode* nodeSearch (BSTnode *root, int value){
if (root -> data == value){
return root;
} else if (root -> data > value){
return nodeSearch(root->left,value);
} else if (root -> data < value){
return nodeSearch(root->right, value);
}
return nullptr;
}
void display(BSTnode *root){
if (root == nullptr){
return;
} else {
display(root->left);
cout << root->data <<"";
display(root->right);
}
}
int main(){
BSTnode *root = nullptr;
root = addNode(root,1);
root = addNode(root,3);
root = addNode(root,5);
root = addNode(root,8);
root = addNode(root,-11);
root = addNode(root,-15);
root = addNode(root,-10);
cout << containsValue(root,-15)<< endl;
cout << containsValue(root,-135)<< endl;
deleteNode(root,16);
deleteNode(root,8);
}

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

Students also viewed these Databases questions

Question

What influences peoples choice of values?

Answered: 1 week ago