Question

(4) Based on Problem P13.10 e Submit the solution as hmw-6-4.cpp. . Write a program that reads a list of strings (a string per line) from the file data4.txt and inserts them into a binary search tree. You can use the implementation of the class BinarySearchTree introduced in the textbook or rhe one posted on CCLE Implement a traversal function void inorder (Action & a) for inorder traversal of a binary search tree that carries out an action other than just printing the node data. The action should be supplied as a derived class of the class class Actionf public: void act (string str) () ) i . Use the inorder function, and a suitable class derived from Action, to compute the sum of all lengths of the strings stored in a tree and then display it. Similarly, implement traversal functions nreorder (Act ion & al and rost.order (Action & a)

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

Code in C++(14):

#include <iostream> #include <fstream> #include <stack> using namespace std; // node to store string class node public: 2 7 string data; node* left; node* right; 10 12 13 14 15 16 17 18 19 20 21 // base class class Actionf public: node* root=nullptr; void act (string data); // derived class class derived:public Action public: void getLength (Action a) 23 2 4 25 26 27 28 / creates binary search tree void Action: :act (string data) f node* new node-new node // new node to store a string new node->data-data; new node->left nullptr // left child of the node

Output:

//------------------------ Code starts------------------------

#include <iostream>

#include <fstream>

#include <stack>

using namespace std;

// node to store string

class node{

public:

string data;

node* left;

node* right;

};

// base class

class Action{

public:

node* root=nullptr;

void act(string data);

};

// derived class

class derived:public Action{

public:

void getLength(Action& a);

};

// creates binary search tree.

void Action::act(string data){

node* new_node=new node(); // new node to store a string

new_node->data=data;

new_node->left=nullptr; // left child of the node

new_node->right=nullptr; // right child of the node

if(root==nullptr){

// initially when root is null, then root

// is set to current node.

root=new_node;

}else{

node* temp_node=root;

node* past_temp_root=root;

// left child and right child of root are visited depending upon

// whether data is alphabetically greater than (or less) than root->data.

while(temp_node!=nullptr){

past_temp_root=temp_node;

if(data<temp_node->data){

temp_node=temp_node->left;

}else{

temp_node=temp_node->right;

}

}

if(data<past_temp_root->data){

past_temp_root->left=new_node;

}else{

past_temp_root->right=new_node;

}

}

}

// inorder traversal :

/*

left elements are visited first and then right elements.

current element is pushed until current element is not null

and then current element is popped out and right element is visited.

*/

void inorder(Action& a){

stack<node*> st;

node* current=a.root;

while(!st.empty() or current!=nullptr){

// left element is visited first

if(current!=nullptr){

st.push(current);

current=current->left;

}else{

node* nd=st.top();

st.pop();

cout << nd->data << endl;

current=nd->right;

}

}

}

// takes Action object as parameter.

// prints out length of all strings in the binary tree

// uses inorder traversal

void derived::getLength(Action& a){

stack<node*> st;

node* current=a.root;

int sum=0;

while(!st.empty() or current!=nullptr){

if(current!=nullptr){

st.push(current);

current=current->left;

}else{

node* nd=st.top();

st.pop();

sum+=nd->data.length();

current=nd->right;

}

}

cout << "Sum of length of all strings: "<< sum << endl;

}

int main(){

Action a;

ifstream file("H:/data4.txt"); // opens file containing names (one string in each line)

string line; // stores string from text file

while(getline(file, line)!=nullptr){

a.act(line); // each string from text file is stored in binary tree

}

cout << "-----Inorder traversal-----" << endl;

inorder(a); // inorder traversal

derived d;

d.getLength(a); // calculates length of all strings in the binary search tree

return 0;

}

//------------------------Code ends--------------------------

Add a comment
Know the answer?
Add Answer to:
(4) Based on Problem P13.10 e Submit the solution as hmw-6-4.cpp. . Write a program that...
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
  • Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate ins...

    Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate insert operation in binary search tree. Please fill in the missing part for the insert method to make the program work. The expected output should be as follows. 20 30 40 50 60 70 80 Part B: Find Lowest Common Ancestor (LCA) of a Binary Search Tree. According to WikiPedia definition , The lowest common ancestor is defined between two nodes v and...

  • For this computer assignment, you are to write a C++ program to implement a class for...

    For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. The definition of the class for a binary tree (as a template) is given as follows: template < class T > class binTree { public: binTree ( ); // default constructor unsigned height ( ) const; // returns height of tree virtual void insert ( const T& ); //...

  • 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);...

  • 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...

  • In this assignment, you will add several methods to the Binary Search Tree. You should have compl...

    In this assignment, you will add several methods to the Binary Search Tree. You should have completed the following three methods in the lab: public void insert(Key key, Value value) public Value get(Key key) public void inorder(Node root) For this assignment, you will implement the following: public void remove(Node root, Key key) public Key getMin(Node n) public Key getMax(Node n) public int height(Node n) The main method contains the statements to check whether your implementation works. You need to change...

  • Overview: file you have to complete is WordTree.h, WordTree.cpp, main.cpp Write a program in C++ that...

    Overview: file you have to complete is WordTree.h, WordTree.cpp, main.cpp Write a program in C++ that reads an input text file and counts the occurrence of individual words in the file. You will see a binary tree to keep track of words and their counts. Project description: The program should open and read an input file (named input.txt) in turn, and build a binary search tree of the words and their counts. The words will be stored in alphabetical order...

  • TreeNode.java public class TreeNode {    public int key;    public TreeNode p;    public TreeNode...

    TreeNode.java public class TreeNode {    public int key;    public TreeNode p;    public TreeNode left;    public TreeNode right;       public TreeNode () {        p = left = right = null;    }       public TreeNode (int k) {        key = k;        p = left = right = null;    } } BinarySearchTree.java public class BinarySearchTree {    public TreeNode root;       public BinarySearchTree () {        root...

  • 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...

  • *****************************In Java***************************************In Java***********************************In Java************************* In this problem, you will implement various algorithms operating on binary search...

    *****************************In Java***************************************In Java***********************************In Java************************* In this problem, you will implement various algorithms operating on binary search trees. We have provided with you a standard implementation of a generic BST in BinarySearchTree.java. Note that this class is an abstract class, which means that some of its methods are not implemented. In previous assignments, you have implemented interfaces which specified methods that you needed to write. Very similarly, an abstract class is a class with some unimplemented methods (it can be thought...

  • in c++ please program for this code #include <iostream> #include <fstream> #include <string> #include <cstring> //...

    in c++ please program for this code #include <iostream> #include <fstream> #include <string> #include <cstring> // for string tokenizer and c-style string processing #include <algorithm> // max function #include <stdlib.h> #include <time.h> using namespace std; // Extend the code here as needed class BTNode{ private: int nodeid; int data; int levelNum; BTNode* leftChildPtr; BTNode* rightChildPtr; public: BTNode(){} void setNodeId(int id){ nodeid = id; } int getNodeId(){ return nodeid; } void setData(int d){ data = d; } int getData(){ return data;...

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