Question
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started