Question

In: Computer Science

Write C++ program (submit the .cpp,.h, .sln and .vcxproj files) Problem 1. Generate 100 random numbers...

Write C++ program (submit the .cpp,.h, .sln and .vcxproj files)

Problem 1. 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.

Problem 2. Build a binary tree using a binary tree class member function from the following array of preorder traversal 3,9,20,15,7 and inorder traversal 9,3,15,20,7. Implement the constructor, destructor and all member functions including print postorder traversal of the binary tree.

Solutions

Expert Solution

Solution to problem 1:-

Code is as following -

#include <iostream>
#include <iomanip>

using namespace std;

struct Node{
  int val;
  int count;
  Node *left;
  Node *right;
};


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;
  }

int index = 0; // to store elements in sorted array

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;
  }
}

Sample Output is as following:

Solution to problem (2)-

Code is as following----

#include <bits/stdc++.h> 
using namespace std; 
  
class node  
{  
    public: 
    int data;  
    node* left;  
    node* right;  
};  
  
/* Prototypes for utility functions */
int search(int arr[], int strt, int end, int value);  
node* newNode(int data); 

//Construct the tree using PreOrder and Inorder
node* buildTree(int in[], int pre[], int inStrt, int inEnd)  
{  
    static int preIndex = 0;  
    if (inStrt > inEnd)  
        return NULL;  
    node* tNode = newNode(pre[preIndex++]);  
    /* If this node has no children then return */
    if (inStrt == inEnd)  
        return tNode;  
    /* Else find the index of this node in Inorder traversal */
    int inIndex = search(in, inStrt, inEnd, tNode->data);  
    /* Using index in Inorder traversal, construct left and  
    right subtress */
    tNode->left = buildTree(in, pre, inStrt, inIndex - 1);  
    tNode->right = buildTree(in, pre, inIndex + 1, inEnd);  
    return tNode;  
}  
  
/* Function to find index of value in arr[start...end]  
The function assumes that value is present in in[] */
int search(int arr[], int strt, int end, int value)  
{  
    int i;  
    for (i = strt; i <= end; i++)  
    {  
        if (arr[i] == value)  
            return i;  
    }
    return 0;  
}  
  
/* Helper function that allocates a new node with the  
given data and NULL left and right pointers. */
node* newNode(int data)  
{  
    node* Node = new node(); 
    Node->data = data;  
    Node->left = NULL;  
    Node->right = NULL;  
    return (Node);  
}  
/* This funtcion is here just to test buildTree() */
void printPostorder(node* node)  
{  
    if (node == NULL)  
        return;  
    printPostorder(node->left); 
    printPostorder(node->right); 
    cout<<node->data<<" "; 
}  
  
/* Driver code */
int main()  
{  
    int inorder[]  = {9,3,15,20,7};  
    int preorder[] = {3,9,20,15,7};  
    int len = sizeof(inorder) / sizeof(inorder[0]);  
    node* root = buildTree(inorder, preorder, 0, len - 1);  
    cout << "PostOrder traversal of the constructed tree is \n";  
    printPostorder(root);  
}

Output is:


Related Solutions

i need an example of a program that uses muliple .h and .cpp files in c++...
i need an example of a program that uses muliple .h and .cpp files in c++ if possible.
WRITE ONLY THE TO DO'S   in FINAL program IN C++ (necessary cpp and header files are...
WRITE ONLY THE TO DO'S   in FINAL program IN C++ (necessary cpp and header files are witten below) "KINGSOM.h " program below: #ifndef WESTEROS_kINGDOM_H_INCLUDED #define WESTEROS_KINGDOM_H_INCLUDED #include <iostream> namespace westeros { class Kingdom{         public:                 char m_name[32];                 int m_population; };         void display(Kingdom&);                      } #endif } Kingdom.cpp Program below: #include <iostream> #include "kingdom.h" using namespace std; namespace westeros {         void display(Kingdom& pKingdom) {                 cout << pKingdom.m_name << ", population " << pKingdom.m_population << endl;                                                               FINAL:...
Write a Java program to generate random numbers in the following range a. 1 <=n <=...
Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5 JAVA PROGRAMMING
For this problem, you will write a program using two queues. Generate n random numbers between...
For this problem, you will write a program using two queues. Generate n random numbers between 10 and 100 (both inclusive), where n>9. The value of n should be taken as input from the user and n should be >9. The numbers could be duplicated. Enqueue all these numbers to the first queue. The objective is to find the numbers whose sum of digits is odd and enqueue them to the second queue. The remaining numbers (whose sum of digits...
Write a program in c++ that merges numbers from two files and writes all the numbers...
Write a program in c++ that merges numbers from two files and writes all the numbers into a third file in ascending order. Each input file contains a list of 50 sorted double floating-point numbers from the smallest to largest. After the program is run, the output file will contain all 100 numbers between in the two input files, also sorted from smallest to largest. Format the output into two columns – the first column contains the numbers 1-100, and...
To generate 100 random numbers between 1-100 in a randomData.txt file To read the 100 random...
To generate 100 random numbers between 1-100 in a randomData.txt file To read the 100 random numbers from randomData.txt and store them in an array Print the data in the array Find the smallest and the largest of the random numbers and their array position Insert an element of value100 in the 51th position of the array Delete all the elements of the array having values between 50-80 and print the residual array Sort the data in the final array(residual)...
java please 1. Write a Java program to generate random numbers in the following range a....
java please 1. Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5 2. Write statements that assign random integers to the variable n in the following ranges: a) 1 ≤ n ≤ 2 b) 1 ≤ n ≤ 100 c) 0 ≤ n ≤ 9 d) 1000 ≤...
Write the header and the implementation files (.h and .cpp separatelu) for a class called Course,...
Write the header and the implementation files (.h and .cpp separatelu) for a class called Course, and a simple program to test it, according to the following specifications:                    Your class has 3 member data: an integer pointer that will be used to create a dynamic variable to represent the number of students, an integer pointer that will be used to create a dynamic array representing students’ ids, and a double pointer that will be used to create a dynamic array...
Write the header and the implementation files (.h and .cpp separately) for a class called Course,...
Write the header and the implementation files (.h and .cpp separately) for a class called Course, and a simple program to test it (C++), according to the following specifications:                    Your class has 3 member data: an integer pointer that will be used to create a dynamic variable to represent the number of students, an integer pointer that will be used to create a dynamic array representing students’ ids, and a double pointer that will be used to create a dynamic...
Write an Arduino code that does the following. Generate 50 random numbers between the numbers 100...
Write an Arduino code that does the following. Generate 50 random numbers between the numbers 100 and 300. Pick a number at random out of these 50 random variables. a. Determine the probability of the chosen number being greater than 200. This may be achieved by counting the numbers that are greater than 200 and dividing the count by 50. Make sure you, i.Formulate the appropriate if-conditions to check for a number being greater than 200 ii. Use a for-loop...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT