Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Could you help me to add a level order traversal to my C++ program. please write the code base on my code. Here is my

Could you help me to add a level order traversal to my C++ program.

please write the code 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

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

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Oracle Database 19c DBA By Examples Installation And Administration

Authors: Ravinder Gupta

1st Edition

B09FC7TQJ6, 979-8469226970

More Books

Students also viewed these Databases questions

Question

6.81 Intro. Psychology Students 74

Answered: 1 week ago

Question

b. Will there be one assigned leader?

Answered: 1 week ago

Question

Do you currently have a team agreement?

Answered: 1 week ago