Question

Detailed Specification: Write six basic functions for the BST: Insert, Delete, Search, Find max, Find min, and Print_BST 1. Search(x): Find out the index that stores element x using binary search tree mechanism. Print out all the elements in the search path. 2. Find max(): Find and print maximum value in BST 3. Find min): Find and print minimum value in BST 4. Print BST: Print out the BST structure in the form of array with index 5. Insert(x): Insert a value element x into BST 6. (BOUNS) Delete(x): Delete element x in BST including ALL 3 situation:s we discussed After you finished the all functions, following are the things you need to carry out 1. Insert(5) 2. Insert(8) 3. Insert(3) 4. Insert(1) 5. Insert(4) 6. Insert(9) 7. Insert(18) 8. Insert (20) 9. Insert(19) 10. Insert(2) 11. Perform Print_BSTO 12. Perform Find max() 13. Perform Find min() 14. Perform Search(3) in the BST 15. Perform Search(18) in the BST 16. Perform Search(19) in the BST 17. Delete(19) in the BST, perform Print BSTO (BOUNS) 18. Delete(2) in the BST, perform Print BSTO (BOUNS) 19. Delete(8) in the BST, perform Print BSTO (BOUNS) 20. Delete(3) in the BST, perform Print BSTO (BOUNS) 21. Delete(5) in the BST, perform Print_BSTO (BOUNS) (p.s. Although the input values are provided, your code should be able to handle random input values.)

***************************************PLEASE USE AN ARRAY NOT A POINTER********************************************

0 0
Add a comment Improve this question Transcribed image text
Answer #1

EXECUTABLE CODE:

#include <iostream>

#include <stdlib.h>

using namespace std;

typedef struct node

{

int data;

struct node* left;

struct node* right;

}BST;

BST* insert(int val) //this function creates a node

{

BST* node = (BST*)malloc(sizeof(BST));

node->data = val;

node->left = NULL;

node->right = NULL;

return node;

}

BST nodeInsert(BST root, int val)

{

if(root == NULL)

return insert(val); //if root is null then creates a node

if(val < root->data)

root->left = nodeInsert(root->left, val); //creates nodeat left subtree if value is less than root

else

root->right = nodeInsert(root->right, val); //creates nodeat right subtree if value is greater than root

return root;

}

BST deleteNode(BST root, int val)

{

if (root == NULL)

return root; //if root is null return null

if (val < root->data)

root->left = deleteNode(root->left, val); //if search valis less than root, then traverse left subtree

else if (val > root->data)

root->right = deleteNode(root->right, val); //if searchval is greater than root, then traverse right subtree

else

{

if (root->left == NULL) //swaps the root->right withcurrent node

{

BST *temp = root->right;

free(root);

return temp;

}

else if (root->right == NULL) //swaps root->left withcurrent node

{

BST *temp = root->left;

free(root);

return temp;

}

BST* temp = root->right;

while (temp->left != NULL)

temp = temp->left;

root->data = temp->data;

root->right = deleteNode(root->right, temp->data);

}

return root;

}

int findMin(BST* root)

{

BST *temp = root;

while(temp->left)

temp = temp->left;

return temp->data;

}

int findMax(BST* root)

{

BST *temp = root;

while(temp->right)

temp = temp->right;

return temp->data;

}

void printInorder(BST* root) //preorder traversal to printtree

{

if(root == NULL) return;

printInorder(root->left);

cout<<root->data <<" ";

printInorder(root->right);

}

int search(BST* root, int val)

{

if (root == NULL)

return 0;

if (root->data == val)

return 1;

if (root->data < val)

return search(root->right, val);

return search(root->left, val);

}

int main()

{

BST* root = NULL;

root = insert(5); //adding root element

nodeInsert(root,8); //adding element to existing tree

nodeInsert(root,3);

nodeInsert(root,1);

nodeInsert(root,4);

nodeInsert(root,9);

nodeInsert(root,18);

nodeInsert(root,20);

nodeInsert(root,19);

nodeInsert(root,2);

cout << "INORDER : ";

printInorder(root);

cout << endl;

int min = findMin(root);

int max = findMax(root);

cout << "Minimum element is : " << min <<endl;

cout << "Maximum element is : " << max <<endl;

if(search(root, 3))

cout << "Found" << endl;

else

cout << "Not found" << endl;

if(search(root, 18))

cout << "Found" << endl;

else

cout << "Not found" << endl;

if(search(root, 19))

cout << "Found" << endl;

else

cout << "Not found" << endl;

root = deleteNode(root, 19);

printInorder(root);

cout << endl;

root = deleteNode(root, 2);

printInorder(root);

cout << endl;

root = deleteNode(root, 8);

printInorder(root);

cout << endl;

root = deleteNode(root, 3);

printInorder(root);

cout << endl;

root = deleteNode(root, 5);

printInorder(root);

cout << endl;

return 0;

}

OUTPUT:gcc version 4.6.3 INORDER 1 2 3 4589 18 19 20 Minimum element is 1 Maximum element is: 20 Found Found Found 1 2 34 5 8 9 18 20 1 345 8 9 18 20 1 345 9 18 20 1 4 59 18 20 1 4918 20

Add a comment
Know the answer?
Add Answer to:
***************************************PLEASE USE AN ARRAY NOT A POINTER******************************************** Detailed Specification: Write six basic functions for the BST:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++: Create a class called "HashTable" This class will implement hashing on an array of integers...

    C++: Create a class called "HashTable" This class will implement hashing on an array of integers Make the table size 11 Implement with linear probing. The hash function should be: h(x) = x % 11 Create search(), insert() and delete() member functions. Search should return the index of where the target is located, insert should allow the user to insert a value, and delete removes the desired value from the table. In main perform the following: 1. Insert: 17 11...

  • In C++ and use functions that are asked for, thanks! Implement the BinarySearchTree ADT in a...

    In C++ and use functions that are asked for, thanks! Implement the BinarySearchTree ADT in a file BinarySearchTree.h exactly as shown below. // BinarySearchTree.h // after Mark A. Weiss, Chapter 4, Dr. Kerstin Voigt #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <cassert> #include <iostream> using namespace std; template <typename C> class BinarySearchTree { public: BinarySearchTree( ) : root{ nullptr } { } ~BinarySearchTree( ) { makeEmpty(); } const C & findMin( ) const { assert(!isEmpty()); return findMin( root )->element; } const C...

  • Write a program that asks the user to input 10 integers of an array. The program...

    Write a program that asks the user to input 10 integers of an array. The program then inserts a new value at position (or index) given by the user, shifting each element right and dropping off the last element. For example, in the sample input/output below, the program will insert the value 30 at index 3. All the previous numbers of the array starting from position 3 (i.e., 7 1 20 9 23 8 22 will be shifted one position...

  • C++ Use C++ functions and build a program that does the most basic job all students...

    C++ Use C++ functions and build a program that does the most basic job all students have to contend with, process the grades on a test and produce a summary of the results. The big wrinkle is, it should be a multi-file program. -Requires three simple things: Figure out the best score of all scores produced Figure out the worst score of all scores produced Assign a letter grade for each score produced Complete this lab by writing three functions....

  • please write the code in C++ Implement the BinarySearchTree ADT in a file BinarySearchTree.h exactly as...

    please write the code in C++ Implement the BinarySearchTree ADT in a file BinarySearchTree.h exactly as shown below. // BinarySearchTree.h // after Mark A. Weiss, Chapter 4, Dr. Kerstin Voigt #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <cassert> #include <iostream> using namespace std; template <typename C> class BinarySearchTree { public: BinarySearchTree( ) : root{ nullptr } { } ~BinarySearchTree( ) { makeEmpty(); } const C & findMin( ) const { assert(!isEmpty()); return findMin( root )->element; } const C & findMax( ) const...

  • c++, we have to write functions for the code given below and other instructions for it...

    c++, we have to write functions for the code given below and other instructions for it to compile. I am having issues understanding how to confront the problem and how to write functions and read the program so it can eventually be solved so it can be compiled 7/ * INSTRUCTIONS: Write two functions in the space // * indicated below. // * // * #1 => Find index of maximum value: Write a function that will // * find...

  • Assuming that the array upc declared below has already been filled with MAX_LENGTH values, write a...

    Assuming that the array upc declared below has already been filled with MAX_LENGTH values, write a function that will ask to input an integer from the keyboard and perform a sequential search of the array for the input integer. If the input integer is found, print the index of the matching value in the array. Otherwise, print "Not found". Include declarations for all variables that you use.             const int MAX_LENGTH = 25;             int upc[MAX_LENGTH]; Please write in C++

  • Create a program that creates an array of 500 random numbers between -10 and 10. Write...

    Create a program that creates an array of 500 random numbers between -10 and 10. Write several functions: Function 1 - prints the array with a certain number of elements per line. Prototype: void printArray(int array[], int numElements, int numPerLine, ostream &os) Function 2 prints only the array elements that are evenly divisible by an input number. Prototype: void printDivBy(int array[], int numElements, int numPerLine, int divBy, ostream &os) Function 3 takes the input array and returns the maximum, the...

  • in C language Write a program to find the element of an array, which occurs maximum...

    in C language Write a program to find the element of an array, which occurs maximum number of times and its count. The program should accept the array elements as input from the user and print out the most occurring element along with its count. Hint: array size is 5. For example: Enter array elements (array size 5): 22321 Output: Max occurring element: 2. count: 3

  • Write a program that deletes an element of a one-dimensional array of characters. The program should:...

    Write a program that deletes an element of a one-dimensional array of characters. The program should: Ask user to input the number of characters in the array the values of the array a character to be deleted Call a method to delete the character Print the resulting array or, if the character is not found, print “Value not found” The method called by the main program should: Pass the array and the character to be found as parameters If the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT