Question

C++ Binary tree, help pls #pragma once // include this library to use NULL, otherwise use...

C++ Binary tree, help pls

#pragma once

// include this library to use NULL, otherwise use nullptr instead

#include <cstddef>

#include <iostream>

#include "node.hpp"

template<class T>

class BST{

public:

// Constructor for the BST class, creates an empty tree

BST(void);

// Destructor for the BST class, destroys the tree

~BST(void);

// Inserts data into the tree

// param: The data to be inserted into the tree

void insert(T);

// Removes data from the tree

// param: The data to be removed from the tree

void remove(T);

// Performs an inorder traversal

// returns: pointer to a vector containing the tree traversal

std::vector<T> *inorder(void);

// Performs an postorder traversal

// returns: pointer to a vector containing the tree traversal

std::vector<T> *postorder(void);

// Performs an preorder traversal

// returns: pointer to a vector containing the tree traversal

std::vector<T> *preorder(void);

// Searches the tree for a given value

// param: the data to search for

// returns: a pointer to the node containing the data or NULL if the data

// was not found

Node<T> *search(T);

// Gets the current number of nodes in the tree

// returns: the number of nodes in the tree

int get_size(void);

private:

// the root node of the tree

Node<T> *root;

// the number of nodes in the tree

int node_count;

};

template<class T>

BST<T>::BST()

{

root = NULL;

node_count = 0;

}

template<class T>

BST<T>::~BST()

{

root = NULL;

while(root != NULL)

{

remove(root->get_data());

}

}

template<class T>

std::vector<T> * BST<T>::inorder()

{

std::vector<T> *vec = new std::vector<T>;

return vec;

}

template<class T>

std::vector<T> * BST<T>::preorder()

{

std::vector<T> *vec = new std::vector<T>;

return vec;

}

template<class T>

std::vector<T> * BST<T>::postorder()

{

std::vector<T> *vec = new std::vector<T>;

return vec;

}

template<class T>

void BST<T>::insert(T new_data)

{

}

template<class T>

Node<T> *BST<T>::search(T val)

{

}

template<class T>

void BST<T>::remove(T val)

{

}

template<class T>

int BST<T>::get_size()

{

}

node.hpp

#pragma once

#include <cstddef>

template<class T>

class Node{

private:

T data;

Node<T> *left;

Node<T> *right;

public:

Node(void);

Node(T &data);

void set_data(T &new_data);

void set_left(Node<T> *left_node);

void set_right(Node<T> *right_node);

T get_data(void);

Node<T> *get_left(void);

Node<T> *get_right(void);

};

template<class T>

Node<T>::Node()

{

data = 0;

left = NULL;

right = NULL;

}

template<class T>

Node<T>::Node(T &new_data)

{

data = new_data;

left = NULL;

right = NULL;

}

template<class T>

void Node<T>::set_data(T &new_data)

{

data = new_data;

}

template<class T>

T Node<T>::get_data()

{

return data;

}

template<class T>

void Node<T>::set_left(Node<T> *left_node_ptr)

{

left = left_node_ptr;

}

template<class T>

void Node<T>::set_right(Node<T> *right_node_ptr)

{

right = right_node_ptr;

}

template<class T>

Node<T> *Node<T>::get_left()

{

return left;

}

template<class T>

Node<T> *Node<T>::get_right()

{

return right;

}

main.cpp

#include <fstream> // Include to open files

#include <cstddef> // Include to use NULL, otherwise use nullptr

#include <iostream> // Include to print to the screen

#include <iomanip>

#include <vector>

#include "bst.hpp" // The header file for our custom linked list class

using namespace std;

int main(int argc, char** argv)

{

    BST<int> bst;

Node<int> *searchResult;

    ifstream input;

    int cmd, argument, ret, i;

vector<int> *traversal_data;

if(argc < 2)

{

cout << "useage: ./a1.out <cmd file>\n";

return 0;

}

    input.open(argv[1]);

// while there is something to read from the file, read

    while (input >> cmd)

    {

// switch on the command we read from the file

        switch (cmd)

        {

// if the cmd requires a parameter, read it from the file and call the

// associated function

        case 1:

            input >> argument;

            bst.insert(argument);

cout << "Inserted " << argument << endl;

            break;

        case 2:

            input >> argument;

            bst.remove(argument);

cout << "Removed " << argument << endl;

            break;

        case 3:

            traversal_data = bst.inorder();

if(traversal_data != NULL)

{

cout << "inorder traveral: ";

for (std::vector<int>::const_iterator i = traversal_data->begin();

i != traversal_data->end();

++i)

{

std::cout << *i << ' ';

}

cout << endl;

}

else cout << "tree appears to be empty\n";

            break;

        case 4:

            traversal_data = bst.postorder();

if(traversal_data != NULL)

{

cout << "postorder traveral: ";

for (std::vector<int>::const_iterator i = traversal_data->begin();

i != traversal_data->end();

++i)

{

std::cout << *i << ' ';

}

cout << endl;

}

else cout << "tree appears to be empty\n";

            break;

        case 5:

            traversal_data = bst.preorder();

if(traversal_data != NULL)

{

cout << "preorder traveral: ";

for (std::vector<int>::const_iterator i = traversal_data->begin();

i != traversal_data->end();

++i)

{

std::cout << *i << ' ';

}

cout << endl;

}

else cout << "tree appears to be empty\n";

            break;

        case 6:

input >> argument;

            searchResult = bst.search(argument);

if(searchResult == NULL)

{

cout << "Did not find " << argument << endl;

}

else

{

cout << "Found " << searchResult->get_data() << endl;

}

            break;

case 7:

i = bst.get_size();

cout << "The tree has " << i << "nodes\n";

break;

}

    }

    input.close();

    return 0;

}
cmd.txt​​​​​​​​​​​​​​

1 75

1 40

1 89

1 128

1 12

1 9

1 1

1 2

1 7

1 3

1 99

1 35

1 36

1 4

1 27

1 66

1 55

3

4

5

2 3

3

4

5

2 5

3

4

5

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

ANSWER:

PROGRAM:

#include <stdio.h>
#include <stdlib.h>
typedef struct tree{
   int data;
   struct tree *left;
   struct tree *right;
}tree;
tree *root = NULL;
tree *nn;
tree* insertion(tree *root,int value);
void inorder(tree *root);
void preorder(tree *root);
void postorder(tree *root);
tree *search(tree *root,int val);
int main(){
   int value;
   int n;
   do{
           printf("\t\t\tMenu\n");
           printf("\t1:Insert\n\t2:preorder\n\t3:postorder\n\t4:inorder\n\t5:search\n");
           printf("Enter your option:");
           scanf("%d",&n);  
           switch(n){
               case 1:
                   printf("Enter a value:");
                   scanf("%d",&value);
                   root = insertion(root,value);
                   break;
               case 2:
                   preorder(root);
                   break;
               case 3:
                   postorder(root);
                   break;
               case 4:
                   inorder(root);
                   break;
               case 5:
                   printf("Enter a data to search:");
                   scanf("%d",&value);
                   if(search(root,value) == NULL){
                       printf("Not found");
                   }else{
                       printf("Found");
                   }
                   break;
               default:
                   n=6;
                   break;
           }
   }while(n != 6);
   return 0;
}
tree *insertion(tree *root,int value){
   if(root == NULL){
       nn=(tree *)malloc(sizeof(tree));
       nn->data = value;
       nn->left = NULL;
       nn->right = NULL;
       root = nn;
   }else if(value < root->data){
       root->left = insertion(root->left,value);
   }else{
       root->right = insertion(root->right,value);
   }
   return root;
}
void inorder(tree *root){
   if(root != NULL){
       inorder(root->left);
       printf("%d\n",root->data);
       inorder(root->right);
   };
  
}
void preorder(tree *root){
   if(root != NULL){
       printf("%d\n",root->data);
       inorder(root->left);
       inorder(root->right);
   }
}
void postorder(tree *root){
   if(root != NULL){
       inorder(root->left);
       inorder(root->right);
       printf("%d\n",root->data);
   }
}
tree *search(tree *root,int val){
   if(root == NULL){
       return NULL;
   }else if(root->data == val){
       return root;
   }else if(root->data > val){
       search(root->left,val);
   }else{
       search(root->right,val);
   }
}

=========================END===============================

Add a comment
Know the answer?
Add Answer to:
C++ Binary tree, help pls #pragma once // include this library to use NULL, otherwise use...
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++ Help with Test #1 (at bottom) to work properly. May adjust code to work or...

    C++ Help with Test #1 (at bottom) to work properly. May adjust code to work or add any additional code. Do not use global variables. #include <iostream> #include <string> using std::endl; using std::cout; using std::cin; using std::string; void pressAnyKeyToContinue() { printf("Press any key to continue\n"); cin.get(); } //This helps with testing, do not modify. bool checkTest(string testName, int whatItShouldBe, int whatItIs) {    if (whatItShouldBe == whatItIs) { cout << "Passed " << testName << endl; return true; } else...

  • C++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please...

    C++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please help me figure out. Thanks. C++ BST implementation (using a struct) Enter the code below, and then compile and run the program. After the program runs successfully, add the following functions: postorder() This function is similar to the inorder() and preorder() functions, but demonstrates postorder tree traversal. displayParentsWithTwo() This function is similar to the displayParents WithOne() function, but displays nodes having only two children....

  • Need this in C++ Goals: Your task is to implement a binary search tree of linked...

    Need this in C++ Goals: Your task is to implement a binary search tree of linked lists of movies. Tree nodes will contain a letter of the alphabet and a linked list. The linked list will be an alphabetically sorted list of movies which start with that letter. MovieTree() ➔ Constructor: Initialize any member variables of the class to default ~MovieTree() ➔ Destructor: Free all memory that was allocated void printMovieInventory() ➔ Print every movie in the data structure in...

  • Binary Tree Template Write your own version of a class template that will create a binary...

    Binary Tree Template Write your own version of a class template that will create a binary tree that can hold values of any data type. Demonstrate the class with a driver program. Place your binary tree template in it's own header file, Btree.h. Include methods for the following: inserting new values into the tree removing nodes from the tree searching the tree returning the number of nodes in the tree displaying the contents of the tree using preorder traversal Your...

  • 1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the...

    1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the number of leaf nodes in the tree. 2) Extend the Binary Search Tree ADT to include a public method singleParent-Count that returns the number of nodes in the tree that have only one child. 3) The Binary search tree ADT is extended to include a boolean method similarTrees that receives references to two binary trees and determines whether the shapes of the trees are...

  • Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition: class TreeNode<T>...

    Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition: class TreeNode<T>       T data       TreeNode<T> left       TreeNode<T> right Since this TreeNode is a generic Template, use any data file we've used this quarter to store the data in the BinaryTree. To do this will likely require writing a compare function or operator. Hint: Think LEFT if the index is calculate (2n+1) and RIGHT if index is (2n+2). Source code: #include<iostream> using namespace std;...

  • there show an error in sample.cpp file that more than one instance of overloaded function find...

    there show an error in sample.cpp file that more than one instance of overloaded function find matches the argument list. can you please fix it. and rewrite the program and debug. thanks. I also wrote error below on which line in sample.cpp. it shows on find. #include #include #include "node1.cpp" using namespace main_savitch_5; // node1.h #ifndef MAIN_SAVITCH_NODE1_H #define MAIN_SAVITCH_NODE1_H #include <string> namespace main_savitch_5 {    template<class item>    class node    {    public:        typedef item value_type;   ...

  • Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to com...

    Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...

  • Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary...

    Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Add this function to the class binaryTreeType and create a program to test this function. #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct nodeType { elemType info; nodeType<elemType> *lLink; nodeType<elemType> *rLink; }; //Definition of the class template <class elemType> class binaryTreeType { public: //Overload the assignment operator. const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&) { if (this != &otherTree) //avoid self-copy...

  • Hi there, I am working on a binary search tree code in c++. The program must...

    Hi there, I am working on a binary search tree code in c++. The program must store and update students' academic records, each node includes the student name, credits attempted, credits earned and GPA. I have made some progress with the code and written most of the functions in the .cpp file (already did the .h file) but i am struggling with what to put in the main and how to put an update part in the insert function. I...

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