Question

Please help me with this C++ program, thank you!!! You are to develop some binary tree...

Please help me with this C++ program, thank you!!!

You are to develop some binary tree routines that will handle single words. The binary tree is to be maintain as an ordered tree.
The routines you need are:

ADD (add a new word to tree, do not allow duplicates),

DELETE ( deletes a word out of the tree),

SEARCH (look up a word in the tree and indicate the word is in the structure or not)

TRAVERSE ( inorder, preorder, postorder, )


Use the following sequence of words to test your program. Execute the commands in the same order given, and insert and delete the words in the order listed below.

add                     kindness rascal structures man forest mice manoman until rascals yahoo jack

add                     jammers kindman help freedom rock ill hope free boy rocknroll kelvin jimbo

add                     bubba rockback kindhearted mankind rooster manup kinny

inorder

preorder

search                kindness kindman kin rocknroll

delete                 hope yahoo

inorder

delete                 manoman rascals

delete                 mom kindness kindman

delete                 bubba

search                rascal rascals mankind

inoder

postorder

add                     kindness kindman kinny rocknroll hope mom

delete                 jack rascal jammers kindman help freedom rock ill

delete                 hope free boy rocknroll kelvin jimbo

inorder

delete                 kindness forest mice manoman until rascals yahoo hope free boy rocknroll

delete                 kelvin jimbo mankind rooster manup kinny

inorder

add                     kindness jack rascal

inorder

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

#include <cstdlib>

#include<cstring>

using namespace std;

struct tree {

string info;

tree *Left, *Right;

};

tree* root;

class Binary_tree {

public:

Binary_tree();

void insert1(string);

tree* search(string);

tree* insert2(tree*, tree*);

tree* search(string,tree*);

void Delete(string);

void pretrav(tree*);

void intrav(tree*);

void posttrav(tree*);

};

Binary_tree::Binary_tree()

{

root = NULL;

}

tree* Binary_tree::insert2(tree* temp, tree* newnode)

{

if (temp == NULL) {

temp = newnode;

}

else if (temp->info < newnode->info) {

insert2(temp->Right, newnode);

if (temp->Right == NULL)

temp->Right = newnode;

}

else {

insert2(temp->Left, newnode);

if (temp->Left == NULL)

temp->Left = newnode;

}

return temp;

}

void Binary_tree::insert1(string n)

{

tree *temp = root, *newnode;

newnode = new tree;

newnode->Left = NULL;

newnode->Right = NULL;

newnode->info = n;

root = insert2(temp, newnode);

}

tree *Binary_tree ::search(string key, tree *leaf){

if(leaf != NULL){

if(key == leaf->info){

cout<<"Element found"<<endl;

return leaf;

}

if(key < leaf->info){

return search(key, leaf->Left);

}else{

return search(key, leaf->Right);

}

}else{

return NULL;

}

}

tree *Binary_tree ::search(string key){

return search(key, root);

}

void Binary_tree::pretrav(tree* t = root)

{

if (root == NULL) {

cout << "Nothing to display";

}

else if (t != NULL) {

cout << t->info << " ";

pretrav(t->Left);

pretrav(t->Right);

}

}

void Binary_tree::intrav(tree* t = root)

{

if (root == NULL) {

cout << "Nothing to display";

}

else if (t != NULL) {

intrav(t->Left);

cout << t->info << " ";

intrav(t->Right);

}

}

void Binary_tree::posttrav(tree* t = root)

{

if (root == NULL) {

cout << "Nothing to display";

}

else if (t != NULL) {

posttrav(t->Left);

posttrav(t->Right);

cout << t->info << " ";

}

}

void Binary_tree::Delete(string key)

{

tree *temp = root, *parent = root, *marker;

if (temp == NULL)

cout << "The tree is empty" << endl;

else {

while (temp != NULL && temp->info != key) {

parent = temp;

if (temp->info < key) {

temp = temp->Right;

}

else {

temp = temp->Left;

}

}

}

marker = temp;

if (temp == NULL)

cout << "No node present";

else if (temp == root) {

if (temp->Right == NULL && temp->Left == NULL) {

root = NULL;

}

else if (temp->Left == NULL) {

root = temp->Right;

}

else if (temp->Right == NULL) {

root = temp->Left;

}

else {

tree* temp1;

temp1 = temp->Right;

while (temp1->Left != NULL) {

temp = temp1;

temp1 = temp1->Left;

}

if (temp1 != temp->Right) {

temp->Left = temp1->Right;

temp1->Right = root->Right;

}

temp1->Left = root->Left;

root = temp1;

}

}

else {

if (temp->Right == NULL && temp->Left == NULL) {

if (parent->Right == temp)

parent->Right = NULL;

else

parent->Left = NULL;

}

else if (temp->Left == NULL) {

if (parent->Right == temp)

parent->Right = temp->Right;

else

parent->Left = temp->Right;

}

else if (temp->Right == NULL) {

if (parent->Right == temp)

parent->Right = temp->Left;

else

parent->Left = temp->Left;

}

else {

tree* temp1;

parent = temp;

temp1 = temp->Right;

while (temp1->Left != NULL) {

parent = temp1;

temp1 = temp1->Left;

}

if (temp1 != temp->Right) {

temp->Left = temp1->Right;

temp1->Right = parent->Right;

}

temp1->Left = parent->Left;

parent = temp1;

}

}

delete marker;

}

int main()

{

Binary_tree bt;

  

int choice;

string n, key,s;

while (1) {

cout << "\n\t1. Insert\n\t2. Delete\n\t3. Preorder Traversal\n\t4. Inorder Treversal\n\t5. Postorder Traversal\n\t6. search\n\t7.Exit"<<endl;;

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case 1:

cout << "Enter item: ";

cin >> n;

bt.insert1(n);

break;

case 2:

cout << "Enter element to delete: ";

cin >> key;

bt.Delete(key);

break;

case 3:

cout << endl;

bt.pretrav();

break;

case 4:

cout << endl;

bt.intrav();

break;

case 5:

cout << endl;

bt.posttrav();

break;

case 6:

cout<<"enter the element to search\n";

cin>>s;

bt.search(key);

break;

case 7:

exit(0);

}

}

return 0;

}

4G LTE 11:30 PM binarytree.cpp 6 1 #include <iostream> 2 #include <cstdlib> 3 #include<cstring> 4 using namespace std; 5 stru4G LTE 11:30 PM binarytree.cpp 6 1 #include <iostream> 2 #include <cstdlib> 3 #include<cstring> 4 using namespace std; 5 stru4G LTE 11:30 PM == } } } binarytree.cpp 27 { 28 if (temp NULL) { 29 temp = newnode; 30 31 else if (temp->info < newnode->info4G LTE 11:30 PM == } } } binarytree.cpp 27 { 28 if (temp NULL) { 29 temp = newnode; 30 31 else if (temp->info < newnode->info4G LTE 11:30 PM 19 } 60 } } binarytree.cpp 57 58 59 if(key < leaf->info) { return search(key, leaf->Left); 61 }else{ 62 retur4G LTE 11:30 PM 19 } 60 } } binarytree.cpp 57 58 59 if(key < leaf->info) { return search(key, leaf->Left); 61 }else{ 62 retur4G LTE 11:30 PM } mm Il } binarytree.cpp 91 92 } 93 void Binary_tree::posttrav(tree* t = root) 94 { 95 if (root == NULL) { 964G LTE 11:30 PM } mm Il } binarytree.cpp 91 92 } 93 void Binary_tree::posttrav(tree* t = root) 94 { 95 if (root == NULL) { 964G LTE 11:31 PM binarytree.cpp nVue р состо 123 124 == else if (temp == root) { if (temp->Right == NULL && temp->Left NULL) {4G LTE 11:31 PM binarytree.cpp nVue р состо 123 124 == else if (temp == root) { if (temp->Right == NULL && temp->Left NULL) {4G LTE 11:31 PM — } == } == } binarytree.cpp 153 parent-lent NULL; 154 155 else if (temp->Left == NULL) { 156 if (parent->Rig4G LTE 11:31 PM — } == } == } binarytree.cpp 153 parent-lent NULL; 154 155 else if (temp->Left == NULL) { 156 if (parent->Rig4G LTE 11:31 PM — } == } == } binarytree.cpp 153 parent-lent NULL; 154 155 else if (temp->Left == NULL) { 156 if (parent->Rig4G LTE 11:31 PM — } == } == } binarytree.cpp 153 parent-lent NULL; 154 155 else if (temp->Left == NULL) { 156 if (parent->Rig4G LTE 11:31 PM binarytree.cpp 185 int main() 186 { 187 Binary_tree bt; 188 189 int choice; 190 string n, key,s; 191 while (14G LTE 11:31 PM binarytree.cpp 185 int main() 186 { 187 Binary_tree bt; 188 189 int choice; 190 string n, key,s; 191 while (14G LTE 11:31 PM binarytree.cpp CUCC CTut) 212 213 214 215 216 217 218 219 bt.intrav(); break; case 5: cout << endl; bt.posttr4G LTE 11:31 PM binarytree.cpp CUCC CTut) 212 213 214 215 216 217 218 219 bt.intrav(); break; case 5: cout << endl; bt.posttr4G LTE 11:29 PM binarytree 1. Insert 2. Delete 3. Preorder Traversal 4. Inorder Treversal 5. Postorder Traversal 6. search 7.4G LTE 11:29 PM binarytree 1. Insert 2. Delete 3. Preorder Traversal 4. Inorder Treversal 5. Postorder Traversal 6. search 7.11:30 PM * @ 4G LTE binarytree 3. Preorder Traversat 4. Inorder Treversal 5. Postorder Traversal 6. search 7. Exit Enter your11:30 PM * @ 4G LTE binarytree 3. Preorder Traversat 4. Inorder Treversal 5. Postorder Traversal 6. search 7. Exit Enter your4G LTE 11:30 PM binarytree 1. Insert 2. Delete 3. Preorder Traversal 4. Inorder Treversal 5. Postorder Traversal 6. search 7.4G LTE 11:30 PM binarytree 1. Insert 2. Delete 3. Preorder Traversal 4. Inorder Treversal 5. Postorder Traversal 6. search 7.

Add a comment
Know the answer?
Add Answer to:
Please help me with this C++ program, thank you!!! You are to develop some binary tree...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello guys can you t help me with writing a c++ program using classes to for...

    Hello guys can you t help me with writing a c++ program using classes to for doing a program about BINARY SEARCH TREES (BST) which would be capable of these things : 1.insert data to the current place in the tree with the ability to add same data many times in the tree 2.delete data from the tree 3.list the data according to *Preorder*-*Inorder*-Postorder* ways 4.count the data in the tree in two ways  , first with counting the data that...

  • in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree...

    in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree You will also need to implement a Node class. This class will not be tested, but is needed to implement the BST. Your BST must implement the following methods. You are free to implement additional helper methods. It is recommended you create your own helper methods Constructor: Creates an Empty Tree String Method: Returns the string "Empty Tree" for an empty tree. Otherwise, returns...

  • Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to com...

    Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...

  • using java to write,show me the output. please write some common. You CAN NOT use inbuild...

    using java to write,show me the output. please write some common. You CAN NOT use inbuild functions for Tree ADT operations. using code below to finsih public class Main {    public static void main(String[] args) {        BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.root.right.left = new Node(6); tree.root.right.right = new Node(7); tree.root.left.left.left = new Node(8); tree.root.left.left .right= new Node(9);...

  • I need help in C++ implementing binary search tree. I have the .h file for the...

    I need help in C++ implementing binary search tree. I have the .h file for the binary search tree class. I have 4 classic texts, and 2 different dictionaries. Classic Texts: Alice In Wonderland.txt A Tale of Two Cities.txt Pride And Prejudice.txt War and Peace.txt 2 different dictionaries: Dictionary.txt Dictionary-brit.txt The data structures from the standard template library can not be used.The main program should open the text file, read in the words, remove the punctuation and change all the...

  • Hi there, I am working on a binary search tree code in c++. The program must...

    Hi there, I am working on a binary search tree code in c++. The program must store and update students' academic records, each node includes the student name, credits attempted, credits earned and GPA. I have made some progress with the code and written most of the functions in the .cpp file (already did the .h file) but i am struggling with what to put in the main and how to put an update part in the insert function. I...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT