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...

  • 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....

  • 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++

  • 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...

  • Java Program Create a class to store an array of with enough space to store 10 integer values. Us...

    Java Program Create a class to store an array of with enough space to store 10 integer values. Using the principle of recursion, implement the following: *getSize : returns the size of the array. *get (i): returns the i-th element of the array. If the element does not exist, it throws a "NoSuchElementException” which is a subclass of Java class RunTimeException. *add (val): inserts value as the last element of the array. If necessary, double the size of the current...

  • JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration...

    JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration skill Problem description: Write the following eight methods and write a main function to test these methods // return the index of the first occurrence of key in arr // if key is not found in arra, return -1 public static int linearSearch(int arr[], int key) // sort the arr from least to largest by using select sort algorithm public stati void selectSort(int arr[])...

  • In C language Write a program that includes a function search() that finds the index of...

    In C language Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays...

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
ADVERTISEMENT