Question

You are going to implement Treesort algorithm in C++ to sort string data. Here are the steps to complete the homework 1) Use the following class definition for binary search tree nodes. Its constructor is incomplete you should first complete the constructor. class TreeNode t public: string data; / this is the string stored in the node TreeNode left: TreeNode right; TreeNode (string element, TreeNode 1t, TreeNode rt //your code here 2) Write a function that will insert a string into a binary search tree. Use the following function header void insertl int x, TreeNode t 3) Write the function treesort using the following header: TreeNode * treesort (string arr[], int n The function treesort should update the array so that the array elements are in increasing order. Your function should implement the algorithm given above. Any other sorting algorithm will not get any credits. Note that the function return type is a pointer to a TreeNode. The function should return a pointer to the root of the binary search tree that is created in the treesort function. I will use that pointer to test the correctness of your function. 4) Write a main function to test your functions. The main function should first read the size N of the array. Then it should prompt the user to enter a data file name. N strings should be read from the file into the array. It should then call the treeSort) to sort the read data. The sorted data should be printed as a result. Data file should include one data element per line. It may include any information like names of people, abbreviations, barcodes ete. Here is a sample run of the program:

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

// Example program
#include <iostream>
#include <string>
using namespace std;


class TreeNode {
public:
string data;
TreeNode *left;
TreeNode *right;
TreeNode(string element, TreeNode *lt, TreeNode *rt) {
data=element;
left=lt;
right=rt;
}
void insert(string x, TreeNode* &root) {
if(root==NULL) {
root = new TreeNode(x, NULL, NULL);
return;
}   
else if(x< root->data)
insert(x, root->left);
else
insert(x, root->right);
}
  
void printOrder(TreeNode *node) {
if(node==NULL)
return;
else {
printOrder(node->left);
cout<<node->data<<"\n";
printOrder(node->right);
}
}
  
TreeNode * treeSort(string *arr, int& n) {
TreeNode *root=NULL;
for(int i=0; i<n; i++) {
insert(arr[i], root);   
}
printOrder(root);
}
};


int main() {
TreeNode bst("", NULL, NULL);
int N;
cout<<"Enter number of elements to insert: ";
cin>>N;
string* list=new string[N];
for(int i=0; i<N; i++) {
cin>>list[i];
  
}
bst.treeSort(list, N);
// system("pause");
return 0;
}

Online C++ Compiler-online ec X ← → https://www.onlinegdb.com/online-c++-compiler Run DebugStop Share H Save Beautify Language C++ main.cpp 26 27 28 void printOrder (TreeNode node) t if (node NULL) 30 31 32 return else t printOrder(node->left); cout< <node-data<n; printOrder(node-right); 34 35 36 37 38 TreeNode treeSort (string *arr, int& n) input Enter number of elements to insert: 5 abcd jgf adf abcd adf jgf Program finished with exit code 0 ss ENTER to exit console. 146 KiB/s 21.15 KiB/s ENG 2:37 PM US 11/18/2018 尸 D:

Add a comment
Know the answer?
Add Answer to:
You are going to implement Treesort algorithm in C++ to sort string data. Here are the...
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
  • Data Structures and Algorithm Analysis – Cop 3530 Module 3 – Programming Assignment This assignment will...

    Data Structures and Algorithm Analysis – Cop 3530 Module 3 – Programming Assignment This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a copy constructor, (6) overload the assignment operator, (7) overload the insertion...

  • Need this in C++ Goals: Your task is to implement a binary search tree of linked...

    Need this in C++ Goals: Your task is to implement a binary search tree of linked lists of movies. Tree nodes will contain a letter of the alphabet and a linked list. The linked list will be an alphabetically sorted list of movies which start with that letter. MovieTree() ➔ Constructor: Initialize any member variables of the class to default ~MovieTree() ➔ Destructor: Free all memory that was allocated void printMovieInventory() ➔ Print every movie in the data structure in...

  • Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over...

    Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over an array of integer numbers and size n. The function should return the index of the search key if the search key exists and return - 1 if the search key doesn't exist. [10 Points] Q2] Write a C function to implement the selection sort algorithm, to sort an array of float values and size n. The function should sort the array in ascending...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • In C only Please! This lab is to write a program that will sort an array...

    In C only Please! This lab is to write a program that will sort an array of structs. Use the functions.h header file with your program. Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like though it is recommended that you use bubble sort, insertion sort, or selection sort...

  • Implement the bubble sort algorithm described here: While the array is not sorted For each adjacent...

    Implement the bubble sort algorithm described here: While the array is not sorted For each adjacent pair of elements If the pair is not sorted Swap the elements Use the BubbleSorter class to fill in the code and make it run with the BubbleSorterDemo class. BubbleSorter.java public class BubbleSorter {    /** Sort an integer array using the bubble sort algorithm. @param arr array of integers to sort    */    public static void sort(int[] arr)    { // Your...

  • Consider a binary search tree of positive integers without duplicates. Implement (write out) a program to...

    Consider a binary search tree of positive integers without duplicates. Implement (write out) a program to find the second greatest element in the tree. The Second function is a member function of class BinarySearchTree. The function returns zero if the tree is empty or has only one element, otherwise it returns the value of the second greatest element. Based on the classes provided below, write the implementation of Second in the solution box, including any helper functions (if any) that...

  • C++ (b) Consider a binary search tree of positive integers without duplicates. Implement (write out) a...

    C++ (b) Consider a binary search tree of positive integers without duplicates. Implement (write out) a program to find the second greatest element in the tree. The Second function is a member function of class BinarySearchTree. The function returns zero if the tree is empty or has only one element, otherwise it returns the value of the second greatest element. Based on the classes provided below, write the implementation of Second in the solution box, including any helper functions (if...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

  • Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template...

    Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template of the solution, if you need Write a program that asks users to input 10 integers into an array, write a function that takes the array as its argument, use bubble sort to sort the array and output the sorted array in increasing order. #include <iostream> using namespace std; C:IWINDOWSSystems2cmd.exe lease input 10 integers: void bubblesort(int a[10]) int help; int b[10]; for (int 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