Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Develop a C++ program that implements a binary tree #pragma once // include this library to use NULL, otherwise use nullptr instead #include #include #include

Develop a C++ program that implements a binary tree

#pragma once

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

#include

#include

#include "node.hpp"

template

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 *inorder(void);

// Performs an postorder traversal

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

std::vector *postorder(void);

// Performs an preorder traversal

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

std::vector *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 *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 *root;

// the number of nodes in the tree

int node_count;

};

template

BST::BST()

{

root = NULL;

node_count = 0;

}

template

BST::~BST()

{

root = NULL;

while(root != NULL)

{

remove(root->get_data());

}

}

template

std::vector * BST::inorder()

{

std::vector *vec = new std::vector;

return vec;

}

template

std::vector * BST::preorder()

{

std::vector *vec = new std::vector;

return vec;

}

template

std::vector * BST::postorder()

{

std::vector *vec = new std::vector;

return vec;

}

template

void BST::insert(T new_data)

{

}

template

Node *BST::search(T val)

{

}

template

void BST::remove(T val)

{

}

template

int BST::get_size()

{

}

node.hpp

#pragma once

#include

template

class Node{

private:

T data;

Node *left;

Node *right;

public:

Node(void);

Node(T &data);

void set_data(T &new_data);

void set_left(Node *left_node);

void set_right(Node *right_node);

T get_data(void);

Node *get_left(void);

Node *get_right(void);

};

template

Node::Node()

{

data = 0;

left = NULL;

right = NULL;

}

template

Node::Node(T &new_data)

{

data = new_data;

left = NULL;

right = NULL;

}

template

void Node::set_data(T &new_data)

{

data = new_data;

}

template

T Node::get_data()

{

return data;

}

template

void Node::set_left(Node *left_node_ptr)

{

left = left_node_ptr;

}

template

void Node::set_right(Node *right_node_ptr)

{

right = right_node_ptr;

}

template

Node *Node::get_left()

{

return left;

}

template

Node *Node::get_right()

{

return right;

}

main.cpp

#include // Include to open files

#include // Include to use NULL, otherwise use nullptr

#include // Include to print to the screen

#include

#include

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

using namespace std;

int main(int argc, char** argv)

{

BST bst;

Node *searchResult;

ifstream input;

int cmd, argument, ret, i;

vector *traversal_data;

if(argc < 2)

{

cout << "useage: ./a1.out ";

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::const_iterator i = traversal_data->begin();

i != traversal_data->end();

++i)

{

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

}

cout << endl;

}

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

break;

case 4:

traversal_data = bst.postorder();

if(traversal_data != NULL)

{

cout << "postorder traveral: ";

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

i != traversal_data->end();

++i)

{

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

}

cout << endl;

}

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

break;

case 5:

traversal_data = bst.preorder();

if(traversal_data != NULL)

{

cout << "preorder traveral: ";

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

i != traversal_data->end();

++i)

{

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

}

cout << endl;

}

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

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 ";

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

1. Outline the listening process and styles of listening

Answered: 1 week ago

Question

4. Explain key barriers to competent intercultural communication

Answered: 1 week ago