Question

Write in C++ Build and run your project. The program should compile without any errors, but...

Write in C++

Build and run your project. The program should compile without any errors, but if does fix the errors . Note that function main() creates a new BST of int and inserts the nodes 12, 38, 25, 5, 15, 8, 55 (in that order) and displays them using pre-order traversal. Verify that the insertions are done correctly.

1. Add code to test the deleteNode and search member functions. Save screenshot of your test program run.

2. Add code to implement the nodeCount member function of class BinaryTree. Test it and save screenshot of your test program run.

3. Add code to implement the leafCount member function of class BinaryTree. Test it and save screenshot of your test program run.

4. The insert member function uses an iterative algorithm to insert a new value into the BST. Define a new insert function name it recursiveInsert that uses a recursive algorithm instead. Write a test program to test your function and save a screenshot of a test run showing your insert function works properly.

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

//C++ program

#include<iostream>
#include<stdlib.h>
using namespace std;

  

class BST{
private:
   struct node
   {
int key;
struct node *left, *right;
   };
  
   struct node*root;

   struct node* insert(struct node* Node , struct node*newNode)
   {
if (Node == NULL) return newNode;

if (newNode->key < Node->key)
Node->left = insert(Node->left, newNode);
else if (newNode->key > Node->key)
Node->right = insert(Node->right, newNode);   

return Node;
   }

   struct node*findmin(struct node*tree){
   while(tree->left){
       tree=tree->left;
   }
   return tree;
}

node*deletenode(struct node*tree, int key){
   node*temp;
   if(!tree)return NULL;
   else if(tree->key<key)tree->right=deletenode(tree->right,key);
   else if(tree->key>key)tree->left=deletenode(tree->left,key);
   else if(tree->key==key){
       if(!tree->left){
           temp=tree->right;
           delete(tree);
           return temp;
       }
       if(!tree->right){
           temp=tree->left;
           delete(tree);
           return temp;
       }
       temp=findmin(tree->right);
       tree->key=temp->key;
       deletenode(tree->right,temp->key);
   }
   else
       cout<<"key not found\n";
  
  
   return tree;
}

bool search(struct node* Node, int key)
{
if(Node == NULL)return false;
if (Node->key == key) return true;
  
if (Node->key < key)
return search(Node->right, key);
  
return search(Node->left, key);
}

int size(struct node*Node){
   if(!Node)return 0;
   return 1+size(Node->left)+size(Node->right);
}

void Recpreorder(struct node*Node ){
   if(!Node)return;
   cout<<Node->key<<" ";
   Recpreorder(Node->left );
   Recpreorder(Node->right);
}
int getLeafCount(struct node* node)
{
if(node == NULL)
return 0;
if(node->left == NULL && node->right == NULL)
return 1;   
else
return getLeafCount(node->left)+ getLeafCount(node->right);
}

public:
   BST(){
   root=NULL;
}
  
void Insert(int item)
   {
struct node *temp = new struct node;
temp->key = item;
temp->left =NULL;
   temp->right = NULL;
root= insert(root,temp);
   }
  
   void Delete(int item){
       root= deletenode(root,item);
   }
  
   bool Search(int item){
       return search(root,item);
   }
   int nodeCount(){
       return size(root);
   }
   void preorder(){
       Recpreorder(root);
   }
   int leafCount(){
      return getLeafCount(root);
   }
  
};

int main(){
   BST bst;
   bst.Insert(12);
   bst.Insert(38);
   bst.Insert(25);
   bst.Insert(5);
   bst.Insert(15);
   bst.Insert(8);
   bst.Insert(55);
  
   cout<<"Preorder Traversal\n";
   bst.preorder();
   cout<<"\n\n";
   if(bst.Search(38))cout<<"found in binary search tree\n";
   else cout<<"Not found in binary search tree\n";
   cout<<"\nNumber of total nodes : "<<bst.nodeCount()<<"\n\n";
   cout<<"deleting 5 .........\n";
   bst.Delete(5);
   bst.preorder();
  
   cout<<"\nNumber of total nodes : "<<bst.nodeCount()<<"\n\n";
   cout<<"Number of leaf nodes : "<<bst.leafCount()<<"\n";
  
   return 0;
}
//sample output

Add a comment
Know the answer?
Add Answer to:
Write in C++ Build and run your project. The program should compile without any errors, but...
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
  • A) Fix any errors to get the following program to run in your environment.               B)...

    A) Fix any errors to get the following program to run in your environment.               B) Document each line of code with comments and describe any changes you had to make to the original code to get it to work. C) Write a summary of what your final version of the program does. You may also add white space or reorder the code to suit your own style as long as the intended function does not change. Program 3 #include...

  • Assignment on Java programing 1. Write programs for the following exercises in Java. Each file should...

    Assignment on Java programing 1. Write programs for the following exercises in Java. Each file should have short description of the implemented class and for files with main method the problem it is solving. Make sure your files have appropriate names. Programs should write output to the Console. b) BST: Implement Binary Search Tree ADT with insert(int key), delete(int key), Node find(int key), and in-order traverse() where it prints the value of the key. Your operations should use recursion. The...

  • Homework Edit the following file and save it as Cxxxxxxxx.java where xxxxxxxx is replaced by your...

    Homework Edit the following file and save it as Cxxxxxxxx.java where xxxxxxxx is replaced by your 8 digit ID number. Remove any initial package declaration that might be added to your file in case you edit it in eclipse. The goal of the homework is to add an Euler tour traversal (see page 348 of the text) to the implementation of the class BinaryTree that we have considered.   Your class Cxxxxxxxx should extend the class BinaryTree and implement just one...

  • **Program must compile under Ubuntu! (35 pts) Write a C++ program A4p2.cpp with a class of...

    **Program must compile under Ubuntu! (35 pts) Write a C++ program A4p2.cpp with a class of your own design. The class should contain a protected int member variable var, which is initialized with an integer value between 1 and 50 in a constructor that takes an integer parameter. The class should contain a public member function called playthat should print out a sequence of integers as a result of iteratively applying a math function f to the member variable var...

  • Please show that the code is working at the end. Your program should read from the standard input...

    Please show that the code is working at the end. Your program should read from the standard input a sequence of integer values, with each value separated by a space. Your task is to: Build a binary search tree using these values in the order they are entered. Print 3 traversals: pre-, in-, and post-order. Allow the user to insert/delete a value. Once a new tree is generated, print it in-order. Find predecessor of a given value. The predecessor is...

  • Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate ins...

    Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate insert operation in binary search tree. Please fill in the missing part for the insert method to make the program work. The expected output should be as follows. 20 30 40 50 60 70 80 Part B: Find Lowest Common Ancestor (LCA) of a Binary Search Tree. According to WikiPedia definition , The lowest common ancestor is defined between two nodes v and...

  • If your program does not compile, you will not receive any points! You will write a program to keep up with a Jewelry...

    If your program does not compile, you will not receive any points! You will write a program to keep up with a Jewelry store and some of the Inventory (rings/necklaces/earrings) purchased there (The Jewelry store can be real or not) using Object-Oriented Programming (OOP). The important aspect of object-oriented programming is modular development and testing of reusable software modules. You love selling rings, necklaces, and earrings as well as programming and have decided to open a Jewelry store. To save...

  • Part B (BI). Implement a Red-Black tree with only operation Insert(). Your program should read from...

    Part B (BI). Implement a Red-Black tree with only operation Insert(). Your program should read from a file that contain positive integers and should insert those numbers into the RB tree in that order. Note that the input file will only contain distinct integers. Print your tree by level using positive values for Black color and negative values for Red color Do not print out null nodes. Format for a node: <Node_value>, <Parent_value>). For example, the following tree is represented...

  • For this computer assignment, you are to write a C++ program to implement a class for...

    For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. The definition of the class for a binary tree (as a template) is given as follows: template < class T > class binTree { public: binTree ( ); // default constructor unsigned height ( ) const; // returns height of tree virtual void insert ( const T& ); //...

  • Using robot basic Enter and run the following new program: You should recognize the resulting display...

    Using robot basic Enter and run the following new program: You should recognize the resulting display as the "9 dots" problem. Note how this program generates this display by using an inner column loop "nested" inside an outer row loop. Without changing any of the above given code, program your robot to solve the 9 dots problem by using a single rPen command and exactly four rForward commands (you will also need a rLocate and some rTurn commands). Start by...

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