Question: please write the code base on my code. Please ADD LEVEL ORDER TREVERSAL to my c++ code pls make sure it's level order pls make

please write the code base on my code.

Please ADD LEVEL ORDER TREVERSAL to my c++ code

pls make sure it's level order

pls make sure its base on my code

Here is my code

main.cpp------------------------------------------------------------------

#include

#include "BST.h"

using namespace std;

int main(){

//btree tree;

BST *tree = new BST();

tree->insert('a');

tree->insert('b');

tree->insert('b');

tree->insert('r');

tree->insert('f');

tree->insert('d');

tree->insert('s');

tree->preorder_print();

tree->inorder_print();

tree->postorder_print();

delete tree;

}

BST.h--------------------------------------------------------------

struct node{

char value;

node *left;

node *right;

};

class BST{

public:

BST();

~BST();

void insert(char key);

void destroy_tree();

void inorder_print();

void postorder_print();

void preorder_print();

private:

void destroy_tree(node *leaf);

void insert(char key, node *leaf);

void inorder_print(node *leaf);

void postorder_print(node *leaf);

void preorder_print(node *leaf);

node *root;

};

BST.CPP--------------------------------------------------------------------------------------------

#include

#include "BST.h"

using namespace std;

BST::BST(){

root = NULL;

}

BST::~BST(){

destroy_tree();

}

void BST::destroy_tree(node *leaf){

if(leaf != NULL){

destroy_tree(leaf->left);

destroy_tree(leaf->right);

delete leaf;

}

}

void BST::insert(char key, node *leaf){

if(key < leaf->value){

if(leaf->left != NULL){

insert(key, leaf->left);

}else{

leaf->left = new node;

leaf->left->value = key;

leaf->left->left = NULL;

leaf->left->right = NULL;

}

}else if(key >= leaf->value){

if(leaf->right != NULL){

insert(key, leaf->right);

}else{

leaf->right = new node;

leaf->right->value = key;

leaf->right->right = NULL;

leaf->right->left = NULL;

}

}

}

void BST::insert(char key){

if(root != NULL){

insert(key, root);

}else{

root = new node;

root->value = key;

root->left = NULL;

root->right = NULL;

}

}

void BST::destroy_tree(){

destroy_tree(root);

}

void BST::inorder_print(){

inorder_print(root);

cout << " ";

}

void BST::inorder_print(node *leaf){

if(leaf != NULL){

inorder_print(leaf->left);

cout << leaf->value << ",";

inorder_print(leaf->right);

}

}

void BST::postorder_print(){

postorder_print(root);

cout << " ";

}

void BST::postorder_print(node *leaf){

if(leaf != NULL){

inorder_print(leaf->left);

inorder_print(leaf->right);

cout << leaf->value << ",";

}

}

void BST::preorder_print(){

preorder_print(root);

cout << " ";

}

void BST::preorder_print(node *leaf){

if(leaf != NULL){

cout << leaf->value << ",";

inorder_print(leaf->left);

inorder_print(leaf->right);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!