Question

C++ Generate 100 random numbers of the values 1-20 in an input.txt. Now create a binary...

C++

Generate 100 random numbers of the values 1-20 in an input.txt. Now create a binary search tree using the numbers of the sequence. The tree set should not have any nodes with same values and all repeated numbers of the random sequence must be stored in the node as a counter variable. For example, if there are five 20s’ in the random sequence then the tree node having data 20 has counter variable equal to 5.

Sort the sequence of numbers including repeat numbers using Binary Tree traversal.

Homework Answers

Answer #1

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU

#include <iostream>
#include <iomanip>

using namespace std;

struct Node
{
        int val;
        int count;
        Node *left;
        Node *right;
};
int index = 0; // to store elements in sorted array


Node *make_new_node(int val)
{
        Node *child = new Node();
        child->val = val;
        child->count = 1;
        child->left = NULL;
        child->right = NULL;
        return child;
}

//Construct the Binary search tree using the Array generated and return the root of tree
Node *makeTree(int arr[])
{
        Node *root = NULL;
        for (int i = 0; i < 100; i++)
        {
                if (root == NULL)
                {
                        Node *node = make_new_node(arr[i]);
                        root = node;
                }
                else
                {
                        Node *temp = root;
                        while (true)
                        {
                                if (temp != NULL)
                                {
                                        if (arr[i] == temp->val)
                                        {
                                                (temp->count)++;
                                                break;
                                        }
                                        else if (arr[i] < temp->val)
                                        {
                                                if (temp->left == NULL)
                                                {
                                                        Node *newNode = make_new_node(arr[i]);
                                                        temp->left = newNode;
                                                        break;
                                                }
                                                else
                                                {
                                                        temp = temp->left;
                                                }
                                        }
                                        else if (arr[i] > temp->val)
                                        {
                                                if (temp->right == NULL)
                                                {
                                                        Node *newNode = make_new_node(arr[i]);
                                                        temp->right = newNode;
                                                        break;
                                                }
                                                else
                                                {
                                                        temp = temp->right;
                                                }
                                        }
                                }
                        }
                }
        }
        return root;
}


void SortUsingBinaryTraversal(Node *root, int *arr)
{
        if (root->left != NULL)
                SortUsingBinaryTraversal(root->left, arr);

        for (int i = 0; i < root->count; i++)
        {
                //insert all occurences of value on current node
                arr[index++] = root->val;
        }
        if (root->right != NULL)
                SortUsingBinaryTraversal(root->right, arr);
}

int main()
{
        int arr[100];
        //generate the array of random 100 integers each having value from 1 to 20
        srand(time(0));
        for (int i = 0; i < 100; i++)
        {
                int num = (rand() % 20) + 1;
                arr[i] = num;
        }

        //Print the randomly generated array in 10 by 10 matrix form
        cout << "Randomly Generated array is:" << endl
             << endl;
        for (int i = 0; i < 10; i++)
        {
                for (int j = 0; j < 10; j++)
                {
                        cout << left << setw(5) << arr[i * 10 + j];
                }
                cout << endl;
        }
        cout << endl;

        //Construct Binary Search tree on the generated array
        Node *root = makeTree(arr);

        //Sort the array using Binary Tree Traversal
        int sortedArray[100];
        SortUsingBinaryTraversal(root, sortedArray);

        //Print Sorted array in 10 by 10 matrix form
        cout << "Sorted Array is:" << endl
             << endl;
        for (int i = 0; i < 10; i++)
        {
                for (int j = 0; j < 10; j++)
                {
                        cout << left << setw(5) << sortedArray[i * 10 + j];
                }
                cout << endl;
        }
}
Know the answer?
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
Similar Questions
In this lab, you will write a program that creates a binary search tree based on...
In this lab, you will write a program that creates a binary search tree based on user input. Then, the user will indicate what order to print the values in. **Please write in C code** Start with the bst.h and bst.c base code provided to you. You will need to modify the source and header file to complete this lab. bst.h: #ifndef BST_H #define BST_H typedef struct BSTNode { int value; struct BSTNode* left; struct BSTNode* right; } BSTNode; BSTNode*...
Create an add method for the BST (Binary Search Tree) class. add(self, new_value: object) -> None:...
Create an add method for the BST (Binary Search Tree) class. add(self, new_value: object) -> None: """This method adds new value to the tree, maintaining BST property. Duplicates must be allowed and placed in the right subtree.""" Example #1: tree = BST() print(tree) tree.add(10) tree.add(15) tree.add(5) print(tree) tree.add(15) tree.add(15) print(tree) tree.add(5) print(tree) Output: TREE in order { } TREE in order { 5, 10, 15 } TREE in order { 5, 10, 15, 15, 15 } TREE in order {...
1. Create 20 random numbers from a binomial distribution of 500 trials and the p-value of...
1. Create 20 random numbers from a binomial distribution of 500 trials and the p-value of 0.4. Draw a distribution graph of these numbers. How does the distribution look like? Is the central limit theorem held true in this case? Why or why not? Now, increase the sample size to 100 and repeat the same process. Is the central limit theorem held true in this case? Why or why not? Explain using evidence.
Generate at least 20 random numbers within the range of 1 to 900 using linear congruent...
Generate at least 20 random numbers within the range of 1 to 900 using linear congruent method. Xi = (aXo + C) mod m using a = 87, c = 29 and Xo = 19
Generate 100 random numbers using the RAND function and create a frequency distribution and a histogram...
Generate 100 random numbers using the RAND function and create a frequency distribution and a histogram with bins of width 0.1. Apply the chi-square goodness of fit test (see Chapter 5) to test the hypothesis that the data are uniformly distributed. This question is from Business Analytics 3rd Edition by James R Evans and from Chapter 12 and question 1 The question is from following book and from Chapter 12 question 1 Textbook: James Evans, Business Analytics, 3nd edition, 2019,...
1. Calculate the mean and standard deviation for the numbers 1 – 20. ​i.e.) 1, 2,...
1. Calculate the mean and standard deviation for the numbers 1 – 20. ​i.e.) 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ​Note: The set of numbers will be considered the population. ​µ = _______​​​σ = ________ 2. With your calculator, randomly generate 5 numbers from the numbers 1 – 20, 30 times. ​Use:​[MATH]>>>PRB #5 ​​RandInt(1,20,5)​[ENTER]​​Note: You cannothave the same number repeated in the group of 5. ​​​​​​For...
import java.util.ArrayList; /* Rules:         1. Allow Tester to iterate through all nodes using the...
import java.util.ArrayList; /* Rules:         1. Allow Tester to iterate through all nodes using the in-order traversal as the default.             This means, in Tester the following code should work for an instance of this class             called bst that is storing Student objects for the data:                 BinarySearchTree_Lab08<String> bst = new BinarySearchTree_Lab08<String>();                 bst.add("Man");       bst.add("Soda");   bst.add("Flag");                 bst.add("Home");   bst.add("Today");   bst.add("Jack");                ...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary rivals? How will the acquisition of Reebok by Adidas impact the structure of the athletic shoe industry? Is this likely to be favorable or unfavorable for New Balance? 2- What issues does New Balance management need to address? 3-What recommendations would you make to New Balance Management? What does New Balance need to do to continue to be successful? Should management continue to invest...
What tools could AA leaders have used to increase their awareness of internal and external issues?...
What tools could AA leaders have used to increase their awareness of internal and external issues? ???ALASKA AIRLINES: NAVIGATING CHANGE In the autumn of 2007, Alaska Airlines executives adjourned at the end of a long and stressful day in the midst of a multi-day strategic planning session. Most headed outside to relax, unwind and enjoy a bonfire on the shore of Semiahmoo Spit, outside the meeting venue in Blaine, a seaport town in northwest Washington state. Meanwhile, several members of...