question archive make a program using c + + (data structures) that reads a text file which contain characters(numerical or alphabetic) and then make a binary Tree from these characters
Subject:Computer SciencePrice:2.86 Bought15
make a program using c + + (data structures) that reads a text file which contain characters(numerical or alphabetic) and then make a binary Tree from these characters. Print the characters in all the traversing orders.
#include <iostream>
#include<fstream.h>
#include<conio.h>
using namespace std;
struct node{
char value;
node *left;
node *right;
};
class btree{
public:
btree();
~btree();
void insert(char key);
node *search(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);
node *search(char key, node *leaf);
void inorder_print(node *leaf);
void postorder_print(node *leaf);
void preorder_print(node *leaf);
node *root;
};
btree::btree(){
root = NULL;
}
btree::~btree(){
destroy_tree();
}
void btree::destroy_tree(node *leaf){
if(leaf != NULL){
destroy_tree(leaf->left);
destroy_tree(leaf->right);
delete leaf;
}
}
void btree::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 btree::insert(char key){
if(root != NULL){
insert(key, root);
}else{
root = new node;
root->value = key;
root->left = NULL;
root->right = NULL;
}
}
node *btree::search(chart key, node *leaf){
if(leaf != NULL){
if(key == leaf->value){
return leaf;
}
if(key < leaf->value){
return search(key, leaf->left);
}else{
return search(key, leaf->right);
}
}else{
return NULL;
}
}
node *btree::search(char key){
return search(key, root);
}
void btree::destroy_tree(){
destroy_tree(root);
}
void btree::inorder_print(){
inorder_print(root);
cout << "\n";
}
void btree::inorder_print(node *leaf){
if(leaf != NULL){
inorder_print(leaf->left);
cout << leaf->value << ",";
inorder_print(leaf->right);
}
}
void btree::postorder_print(){
postorder_print(root);
cout << "\n";
}
void btree::postorder_print(node *leaf){
if(leaf != NULL){
inorder_print(leaf->left);
inorder_print(leaf->right);
cout << leaf->value << ",";
}
}
void btree::preorder_print(){
preorder_print(root);
cout << "\n";
}
void btree::preorder_print(node *leaf){
if(leaf != NULL){
cout << leaf->value << ",";
inorder_print(leaf->left);
inorder_print(leaf->right);
}
}
int main(){
//btree tree;
btree *tree = new btree();
ifstream fin;
char ch;
fin.open("demo.txt");
/*This is the name of the file to be opened , this file must be placed in the same directory as of this source file */
while(!fin.eof())
{
fin.get(ch);
tree->insert(ch);
}
fin.close();
tree->preorder_print();
tree->inorder_print();
tree->postorder_print();
delete tree;
}