Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Based on Problem P13.10 » Submit the solution as hmw-6-4.cpp. . Write a program

ID: 3727989 • Letter: B

Question

Based on Problem P13.10 » 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. » Implement a traversal function void inorder (Action & a); rch tree that carries out an action for inorder traversal of a binary sea n 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) f) . 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 preorder (Action & a) and postorder (Action & a)

Explanation / Answer

template class BinaryTree { private: struct TreeNode { TreeNode *left; TreeNode *right; T data; }; TreeNode *root; public: BinaryTree() { root = NULL; } void Inorder(TreeNode *n) { if(n != NULL) { Inorder(n -> left); cout right); } } void PrintInorder() { Inorder(root); } void Preorder(TreeNode *n) { if(n != NULL) { cout left); Preorder(n -> right); } } void PrintPreorder() { Preorder(root); } void Postorder(TreeNode *n) { if(n != NULL) { Postorder(n -> left); Postorder(n -> right); cout right); cout left); } } void PrintReverseInorder() { ReverseInorder(root); } void PrintTree(TreeNode* n, int lev) { if (n != NULL) { PrintTree(n -> right, lev+1); for (int i=0; i data > curr -> data) curr = curr -> right; else curr = curr -> left; } if(t -> data data) parent -> left = t; else parent -> right =t; } } void PrintPrintTree() { PrintTree(root, 0); } bool isEmpty() { return (root == NULL); } }; int main() { vector BT; int iteration = 0; string line; ifstream myfile("input.txt"); if (myfile.is_open()) { while(getline (myfile, line)) { BinaryTree temptree; BT.push_back(temptree); BT[iteration].InsertData(line); cout
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote