Question

This is my current code and im having trouble inserting the findMin, findMax, and Search functions...

This is my current code and im having trouble inserting the findMin, findMax, and Search functions in main to output them.

#include<iostream>

using namespace std;

/*Define the class for BST*/

class BST_TREE

{

   /*Structure of the tree*/

   struct tree

   {

       int value;

       tree* left;

       tree* right;

   };

   tree* root;

   /*Method to clear the tree*/

   tree* Empty(tree* t)

   {

       if (t == NULL)

           return NULL;
         
       {

           Empty(t->left);

           Empty(t->right);

           delete t;

       }

       return NULL;

   }

   /*Method to Insert Node*/

   tree* Insert(int x, tree* t)

   {

       if (t == NULL)

       {

           t = new tree;

           t->value = x;

           t->left = t->right = NULL;

       }

       else if (x < t->value)

           t->left = Insert(x, t->left);

       else if (x > t->value)

           t->right = Insert(x, t->right);

       return t;

   }

   /*Method to find the Minimum Value*/

   tree* findMin(tree* t)

   {

       if (t == NULL)

           return NULL;

       else if (t->left == NULL)

           return t;

       else

           return findMin(t->left);

   }

   /*Method to find the maximum Value*/

   tree* findMax(tree* t)

   {

       if (t == NULL)

           return NULL;

       else if (t->right == NULL)

           return t;

       else

           return findMax(t->right);

   }

   /*Method to remove the Node*/

   tree* Delete(int x, tree* t)

   {

       tree* temp;

       if (t == NULL)

           return NULL;

       else if (x < t->value)

           t->left = Delete(x, t->left);

       else if (x > t->value)

           t->right = Delete(x, t->right);

       else if (t->left && t->right)

       {

           temp = findMin(t->right);

           t->value = temp->value;

           t->right = Delete(t->value, t->right);

       }

       else

       {

           temp = t;

           if (t->left == NULL)

               t = t->right;

           else if (t->right == NULL)

               t = t->left;

           delete temp;

       }

       return t;

   }

   /*Method to display the Tree in INorder*/

   void Print_BST(tree* t)

   {

       if (t == NULL)

           return;

       Print_BST(t->left);

       cout << t->value << " ";

       Print_BST(t->right);


   }

   /*method to search the given Value*/

   tree* Search(tree* t, int x)

   {

       if (t == NULL)

           return NULL;

       else if (x < t->value)

           return Search(t->left, x);

       else if (x > t->value)

           return Search(t->right, x);

       else

           return t;

   }

public:

   /*Constructor to initialize the tree*/

   BST_TREE()

   {

       root = NULL;

   }

   /*Destructor to empty the tree*/

   ~BST_TREE()

   {

       root = Empty(root);

   }

   void insert(int x)

   {

       root = Insert(x, root);

   }

   void Delete(int x)

   {

       root = Delete(x, root);

   }

   void FindMin(tree *t)
   {
       root = findMin(root);
   }

   void Print_BST()

   {

       Print_BST(root);

       cout << endl;

   }

   void Search(int x)

   {

       root = Search(root, x);

   }

};

int main()

{

   BST_TREE t;

   t.insert(5);
   t.insert(8);
   t.insert(3);
   t.insert(1);
   t.insert(4);
   t.insert(9);


   t.insert(18);

   t.insert(20);

   t.insert(19);

   t.insert(2);

   t.Print_BST();

   //t.FindMin(*t);

   t.Delete(19);

   t.Print_BST();

   t.Delete(2);

   t.Print_BST();

   t.Delete(8);

   t.Print_BST();
   t.Delete(3);

   t.Print_BST();
   t.Delete(5);

   t.Print_BST();

Homework Answers

Answer #1

#include<iostream>

using namespace std;

/*Define the class for BST*/

class BST_TREE

{

/*Structure of the tree*/

struct tree

{

int value;

tree* left;

tree* right;

};

tree* root;

/*Method to clear the tree*/

tree* Empty(tree* t)

{

if (t == NULL)

return NULL;

{

Empty(t->left);

Empty(t->right);

delete t;

}

return NULL;

}

/*Method to Insert Node*/

tree* Insert(int x, tree* t)

{

if (t == NULL)

{

t = new tree;

t->value = x;

t->left = t->right = NULL;

}

else if (x < t->value)

t->left = Insert(x, t->left);

else if (x > t->value)

t->right = Insert(x, t->right);

return t;

}

/*Method to find the Minimum Value*/

tree* findMin(tree* t)

{

if (t == NULL)

return NULL;

else if (t->left == NULL)

return t;

else

return findMin(t->left);

}

/*Method to find the maximum Value*/

tree* findMax(tree* t)

{

if (t == NULL)

return NULL;

else if (t->right == NULL)

return t;

else

return findMax(t->right);

}

/*Method to remove the Node*/

tree* Delete(int x, tree* t)

{

tree* temp;

if (t == NULL)

return NULL;

else if (x < t->value)

t->left = Delete(x, t->left);

else if (x > t->value)

t->right = Delete(x, t->right);

else if (t->left && t->right)

{

temp = findMin(t->right);

t->value = temp->value;

t->right = Delete(t->value, t->right);

}

else

{

temp = t;

if (t->left == NULL)

t = t->right;

else if (t->right == NULL)

t = t->left;

delete temp;

}

return t;

}

/*Method to display the Tree in INorder*/

void Print_BST(tree* t)

{

if (t == NULL)

return;

Print_BST(t->left);

cout << t->value << " ";

Print_BST(t->right);


}

/*method to search the given Value*/

tree* Search(tree* t, int x)

{

if (t == NULL)

return NULL;

else if (x < t->value)

return Search(t->left, x);

else if (x > t->value)

return Search(t->right, x);

else

return t;

}

public:

/*Constructor to initialize the tree*/

BST_TREE()

{

root = NULL;

}

/*Destructor to empty the tree*/

~BST_TREE()

{

root = Empty(root);

}

void insert(int x)

{

root = Insert(x, root);

}

void Delete(int x)

{

root = Delete(x, root);

}

void FindMin()

{

tree *min = findMin(root);

cout<<"Min value is: "<<min->value<<endl;

}

void Print_BST()

{

Print_BST(root);

cout << endl;

}

void Search(int x)

{

tree *s = Search(root, x);

if(s==NULL)

cout<< "value is not found"<<endl;

else

cout<< "Value is found"<<endl;

}

void FindMax()

{

tree *max = findMax(root);

cout<<"Min value is: "<<max->value<<endl;

}

};

int main()

{

BST_TREE t;

t.insert(5);

t.insert(8);

t.insert(3);

t.insert(1);

t.insert(4);

t.insert(9);


t.insert(18);

t.insert(20);

t.insert(19);

t.insert(2);

t.Print_BST();

t.FindMin();

t.FindMax();

t.Search(20);

t.Delete(19);

t.Print_BST();

t.Delete(2);

t.Print_BST();

t.Delete(8);

t.Print_BST();

t.Delete(3);

t.Print_BST();

t.Delete(5);

t.Print_BST();


}

=============================================
SEE OUTPUT

========================================
Thanks, let me now if there is any concern.

Know the answer?
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
Similar Questions
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class is empty. Complete the destructor so the memory allocated for each node in the BST is freed. Make a couple of different trees in your main method or in a function to test the destructor (the program should not crash upon exiting). bst.zip (includes the following files below in c++): bst.h: #pragma once #include #include "node.cpp" using namespace std; template class BST { public:...
do a code trace on how a key gets deleted package interview; import java.util.*; public class...
do a code trace on how a key gets deleted package interview; import java.util.*; public class binarySearchTree { Node root; void insert(int key) { root = insertRec(root,key); } void delete(int key) { root = deleteRec(root,key); } Node insertRec(Node root, int key) { if(root == null) { root = new Node(key); return root; } if(key < root.key) { root.leftChild = insertRec(root.leftChild,key); } else if(key > root.key){ root.rightChild = insertRec(root.rightChild,key); } return root; } Node deleteRec(Node root, int key) { if(root ==...
I have written working code for this problem however it is not producing an output and...
I have written working code for this problem however it is not producing an output and it states is a wrong answer. Please just fix my code below and make it work for the sample test cases and all test cases. You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the...
You will be traversing through an integer tree to print the data. Given main(), write the...
You will be traversing through an integer tree to print the data. Given main(), write the methods in the 'IntegerBinaryTree' class specified by the // TODO: sections. There are 6 methods in all to write. Ex: If the input is 70 86 60 90 49 62 81 85 38 -1 the output should be: Enter whole numbers to insert into the tree, -1 to stop Inorder: 38 - 49 - 60 - 62 - 70 - 81 - 85 -...
Java Program: You will be traversing through an integer tree to print the data. Given main(),...
Java Program: You will be traversing through an integer tree to print the data. Given main(), write the methods in the 'IntegerBinaryTree' class specified by the // TODO: sections. There are 6 methods in all to write. Ex: If the input is: 70 86 60 90 49 62 81 85 38 -1 the output should be: Enter whole numbers to insert into the tree, -1 to stop Inorder: 38 - 49 - 60 - 62 - 70 - 81 -...
Copy the file SimplerBST.java from the week 1 examples package and rename the file and class...
Copy the file SimplerBST.java from the week 1 examples package and rename the file and class A3BST. Add two public methods: one named countTwins that takes no parameters and that, when called, traverses every node of the tree to count the number of nodes having two children. one named lessThanValueCount that takes a paremeter of generic type V, and that, when called, traverses every node of the tree to count the number of nodes whose value (not its key) is...
i want to complete this code to insert a new node in the middle of list...
i want to complete this code to insert a new node in the middle of list (take a node data from user, search the node and insert new node after this node). this is the code #include <iostream> #include <stdlib.h> using namespace std ; struct Node{                int data;                Node *link ;}; struct Node *head=NULL, *tail=NULL; /* pointers to Node*/ void InsertFront(); void InsertRear(); void DeleteFront(); void DeleteRear(); int main(){                int choice;                do{                               cout << "1:...
In this lab, you will write a program that creates a binary search tree based on...
In this lab, you will write a program that creates a binary search tree based on user input. Then, the user will indicate what order to print the values in. **Please write in C code** Start with the bst.h and bst.c base code provided to you. You will need to modify the source and header file to complete this lab. bst.h: #ifndef BST_H #define BST_H typedef struct BSTNode { int value; struct BSTNode* left; struct BSTNode* right; } BSTNode; BSTNode*...
Java Program: You will be inserting values into a generic tree, then printing the values inorder,...
Java Program: You will be inserting values into a generic tree, then printing the values inorder, as well as printing the minimum and maximum values in the tree. Given main(), write the methods in the 'BSTree' class specified by the // TODO: sections. There are 5 TODOs in all to complete. Ex: If the input is like ferment bought tasty can making apples super improving juice wine -1 the output should be: Enter the words on separate lines to insert...
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class....
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class. Make every Node object have a false isBlack field, all new node is red by default. In the end of the insert method, set the root node of your red black tree to be black. Implement the rotate() and recolor() functions, and create tests for them in a separate class. import java.util.LinkedList; public class BinarySearchTree<T extends Comparable<T>> { protected static class Node<T> { public...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT