Question

c# prograaming language

1. write a program that generates 10,000 random integers in the range of 0-9 and them in binary search tree.

2. use any sort algorithm (insertion, bubble, quick...) to display a list of each of the integers and how many times they appear.

3. at last, add ruction to the BinarySearchTree class that count the number of edges in a tree.


Write a program that generates 10,000 random integers in the range of 0-9 and store them in a binary search tree. Using one o

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

#include<stdio.h>
#include<stdlib.h>

static int arr[10000], i=0;

struct node
{
   int key;
   struct node *left, *right;
};

// A function to create a new BST node
struct node *newNode(int item)
{
   struct node *tmp = (struct node *)malloc(sizeof(struct node));
   tmp->key = item;
   tmp->left = tmp->right = NULL;
   return tmp;
}

// A function to do inorder traversal of BST
int inorder(struct node *root)
{
   if (root != NULL)
   {
       inorder(root->left);
       arr[i]=root->key;
       i++;
       inorder(root->right);
   }
}

/* A function to insert a new node with given key in BST */
struct node* insert(struct node* node, int key)
{
  
   if (node == NULL) return newNode(key);

  
   if (key <= node->key)
       node->left = insert(node->left, key);
   else if (key > node->key)
       node->right = insert(node->right, key);

  
   return node;
}

void counteach(int arr[])
{
  
   int v=0;
   int count[10];
   int c=1;
   for(int j=0;j<10000-1;j++)
   {
       if(arr[j]==arr[j+1])
       {
           c+=1;
       }
       else
       {
           count[v]=c;
           c=1;
           v+=1;
       }
   }
   count[v]=c;
   for(int k = 0; k<10; k++)
   {
       printf("\n%d\t%d",k, count[k]);
   }
}

void printtree(int arr[])
{
   for(int j=0;j<10000;j++)
       printf("%d\t",arr[j]);
}


int main() {
int c, n;
struct node *root = NULL;
root = insert(root, 5);
printf("\n\n%s","inserted numbers");
for (c = 1; c < 10000; c++) {
n = rand() % 10;
insert(root, n);
printf("%d\t", n);
}
  
   inorder(root);
   printf("\n\n\nAfter insertion into the tree\n");
   printtree(arr);
   printf("\n\n\nCount of each element inserted\n");
   counteach(arr);
}

Add a comment
Know the answer?
Add Answer to:
C# prograaming language 1. write a program that generates 10,000 random integers in the range of ...
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
  • Java Programming - Write a program that generates two random integers, both in the range 25...

    Java Programming - Write a program that generates two random integers, both in the range 25 to 75, inclusive. Use the Math class. Print both integers and then display the positive difference between the two integers, but use a selection. Do not use the absolute value method of the Math class.

  • *IN PYTHON* (Count single digits) Write a program that generates 1,000 random integers between 0 and...

    *IN PYTHON* (Count single digits) Write a program that generates 1,000 random integers between 0 and 9 and displays the count for each number. (Hint: Use a list of ten integers, say counts, to store the counts for the number of 0s, 1s, ..., 9s.) Rubric 5 pts: Proper use of list to count occurrences of numbers 5 pts: Proper calculations of counts 5 pts: Proper output Note: No software dev plan or UML required

  • DESCRIPTION Implement a program in C++ that generates a specified number of random integers, records them...

    DESCRIPTION Implement a program in C++ that generates a specified number of random integers, records them in three arrays, then sorts the arrays with Insertion Sort, Merge Sort, and Quick Sort, respectively. Augment the three sorting algorithms with counters and report the number of characteristic operations each performs in sorting the (same) random values. INPUT The program reads from the terminal the number of random integers to generate, a seed value for the pseudo-random number generator, and a character that...

  • Write a C++ program that generates twenty five random integers between 0 and 24 and display the sum of even integers.

    (C++)Write a program that generates twenty five random integers between 0 and 24 and display the sum of even integers. (Hint: Use rand()%25 to generate a random integer between 0 and 24. Use an array of 25 integers, say num , to store the random integers generated between 0 and 24.)

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

  • WİTH C LANGUAGE Write a program that counts the leaves of a given binary tree. Hint: You will make a small (and smar...

    WİTH C LANGUAGE Write a program that counts the leaves of a given binary tree. Hint: You will make a small (and smart) update to the add function we discussed in class. a) 125 pointsl Create the following binary trees by creating the nodes (malloc) and setting the related pointers (leftChild, rightChild). Make content random. 60 50 40 60 70 30 45 42 b) 125 points Display the trees on screen the way we did in slide 9 using listAll...

  • Write a program that prompts the user to enter a number within the range of 1...

    Write a program that prompts the user to enter a number within the range of 1 to 10 inclusive. The program then generates a random number using the random class: If the users guess is out of range use a validation loop (DO) to repeat the prompt for a valid number, If the users guess matches the random number tell them they win! If the users guess does not match the random number tell them they are wrong. Use a...

  • C++ language Write a program that 1. generates a random integer between 100 and 1000, call...

    C++ language Write a program that 1. generates a random integer between 100 and 1000, call the number N 2. reads a file name from the keyboard 3. opens a file for output using the file name from step 2 4. generates N random numbers between 1 and 100 and writes them to the file from step 3 5. closes the file 6. opens the file from steps 3, and 4 for input 7. reads the numbers from the file...

  • IN C LANGUAGE Write a program that counts the leaves of a given binary tree. Hint: You will make a small (and...

    IN C LANGUAGE Write a program that counts the leaves of a given binary tree. Hint: You will make a small (and smart) update to the add function we discussed in class. a) 25 points Create the following binary trees by creating the nodes (malloc) and setting the related pointers (leftChild, right Child). Make content random. 50 40 60 100 70 45 30 120 47 b) 25 points Display the trees on screen the way we did in slide 9...

  • A. Create a java program that generates 1000 random integers. Write the 1000 random integers to...

    A. Create a java program that generates 1000 random integers. Write the 1000 random integers to a file using the Formatter class. B. Create a second java program that opens the file with the 1000 integers using the Scanner class. Read in the integers and find and print out the largest, smallest and the average of all 1000 numbers. C. Repeat A from above but use the BufferedWriter class B. Repeat B from above but use the BufferedReader class.

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