Question

[12, 21, 12, 26, 11, 13, 26, 30, 12, 2, 3, 13] (d - 7 pts)...

[12, 21, 12, 26, 11, 13, 26, 30, 12, 2, 3, 13]

(d - 7 pts) For the binary search tree obtained in (c), determine the average number of comparisons for a successful search and the average number of comparisons for an unsuccessful search. (e - 7 pts) Use the sorted array of (b) to construct a binary search tree. (f - 7 pts) For the binary search tree obtained in (e), determine the average number of comparisons for a successful search and the average number of comparisons for an unsuccessful search.

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

Hello,

The binary search tree obtained for unsorted array takes less time compared to binary search tree obtained for sorted array. I have created an program to demonstrate the same. Execute the program to get the average comparisons for each element required. Let me know if you need have any queries or changes required.

Code:

#include <iostream>
using namespace std;

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

// Function to insert a new Node with given value in Binary Search Tree
Node* insert(Node* node, int value)
{
   // If the tree is empty, return a new Node
   if (node == NULL)
   {
       struct Node *newNode = new Node;
       newNode->key = value;
       newNode->left = newNode->right = NULL;
       return newNode;
   }

   // Insert the value at correct position
   if (value <= node->key)
       node->left = insert(node->left, value);
   else if (value > node->key)
       node->right = insert(node->right, value);

   // Return the node
   return node;
}

// The function creates the binary search tree
Node* binarySearchTree(int list[], int n)
{
   struct Node *root = NULL;

   // Construct the Binary Search Tree
   for (int i = 0; i < n; i++)
       root = insert(root, list[i]);

   return root;
}

// The function sorts the array
void sort(int *list, int n)
{
   for (int i = 0; i < n - 1; i++)
   {
       for (int j = i + 1; j < n; j++)
       {
           // Swap if current value is greater than next value
           if (list[j] < list[i])
           {
               int temp = list[j];
               list[j] = list[i];
               list[i] = temp;
           }
       }
   }
}

// The fuction search's for the given value and returns the numer of traversals
int search(struct Node *node, int value)
{
   struct Node* head = node;

   if (head == NULL || head->key == value)
       return 1;

   if (head->key < value)
       return search(head->right, value) + 1;
   else
       return search(head->left, value) + 1;
}

void main()
{
   int list[] = { 12, 21, 12, 26, 11, 13, 26, 30, 12, 2, 3, 13 };   // Binary tree elements
   int successSearchList[] = { 26, 3, 2, 30, 13 };                   // List of keys for successful search
   int unsuccessSearchList[] = { 100, 1, 5, 56, 29 };               // List of keys for unsuccessful search

   // Length of each array list
   int n = sizeof(list) / sizeof(list[0]);
   int n1 = sizeof(successSearchList) / sizeof(successSearchList[0]);
   int n2 = sizeof(unsuccessSearchList) / sizeof(unsuccessSearchList[0]);
  
   int count = 0;   // Number of traversals
  
   // Create binary search tree
   struct Node *unsorted = binarySearchTree(list, n);
  
   // Calculate traversals for unsorted successful search list
   cout << "*** Unsorted list search result ***" << endl;
   for (int i = 0; i < n1; i++)
       count += search(unsorted, successSearchList[i]);
   cout << "Average number of successful search: " << count / n1 << endl;

   // Calcualte traversals for unsorted unsuccessful search list
   count = 0;
   for (int i = 0; i < n2; i++)
       count += search(unsorted, unsuccessSearchList[i]);
   cout << "Average number of unsuccessful search: " << count / n2 << endl << endl;

   cout << "*** Sorted list search result ***" << endl;
   sort(list, n);
   struct Node *sorted = binarySearchTree(list, n);

   // Calculate traversals for sorted successful search list
   count = 0;
   for (int i = 0; i < n1; i++)
       count += search(sorted, successSearchList[i]);
   cout << "Average number of successful search: " << count / n1 << endl;

   // Calcualte traversals for sorted unsuccessful search list
   count = 0;
   for (int i = 0; i < n2; i++)
       count += search(sorted, unsuccessSearchList[i]);
   cout << "Average number of unsuccessful search: " << count / n2 << endl;

   cout << "\n\n";
   system("pause");
}

Output:

Add a comment
Know the answer?
Add Answer to:
[12, 21, 12, 26, 11, 13, 26, 30, 12, 2, 3, 13] (d - 7 pts)...
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
  • JH: Student Name: 4) Given the following array, do the following (show all the work). A...

    JH: Student Name: 4) Given the following array, do the following (show all the work). A (56, 89, 23, 58, 22, 11, 45, 48, 90) (a - 5 pts) Construct a hash table for the given array using the hash function H(K)- K mod 5 (b- 4 pts) Determine the average number of comparisons for a successful search using the hash table of (c -3 pts) What is the worst case number of comparisons for an unsuccessful search in the...

  • (g - 6 pts) Construct a hash table of the given array using a hash function H(K) = K mod 5. (h - ...

    (g - 6 pts) Construct a hash table of the given array using a hash function H(K) = K mod 5. (h - 6 pts) For the hash table of (g), determine the average number of comparisons for a successful search and the worst case number of comparisons for an unsuccessful search. (i - 9 pts) Consider the elements of the array assigned to you are known only one at a time. Construct a sequence of priority queues (as max...

  • For the input 26, 56, 45, 17, 58, 80, 15. 16, 13, 39 and hash function...

    For the input 26, 56, 45, 17, 58, 80, 15. 16, 13, 39 and hash function h(k) = k mod 13 1) Construct the close hash table 2) Find the largest number of key comparisons in successful search in this table 3) Find the average number of key comparisons in successful search in this table

  • Question 10: Given an array of data: array1 10 5 15 2 7 21 Write the...

    Question 10: Given an array of data: array1 10 5 15 2 7 21 Write the code for the selection sort to sort the data and draw a table showing how the data is sorted using selection sort. Write the code for Binary search algorithm to find the value 15 from the sorted array. Draw the tree diagram showing the binary search of the list.

  • Binary Search Trees (a) 5 pointsl Insert 5, 12, 7, 1, 6, 3, 13, 2, 10,...

    Binary Search Trees (a) 5 pointsl Insert 5, 12, 7, 1, 6, 3, 13, 2, 10, 11 into an empty binary search tree in the given order. Show the resulting BST after every insertion. (b) 5 points) What are the preorder, inorder, and postorder traversals of the BST you have after (a)? (c) 5 points Delete 2, 7, 5, 6, 11 from the BST you have after (a) in the given order Show the resulting BST after every deletion.

  • C++ Sorting and Searching 1. Mark the following statements as true or false. a. A sequential...

    C++ Sorting and Searching 1. Mark the following statements as true or false. a. A sequential search of a list assumes that the list elements are sorted in ascending order. b. A binary search of a list assumes that the list is sorted. 2. Consider the following list: 63 45 32 98 46 57 28 100 Using a sequential search, how many comparisons are required to determine whether the following items are in the list or not? (Recall that comparisons...

  • Searching/sorting tasks and efficiency analysis - Binary Search Search for the character S using the binary...

    Searching/sorting tasks and efficiency analysis - Binary Search Search for the character S using the binary search algorithm on the following array of characters: A E G K M O R S Z. For each iteration of binary search use a table similar to the table below to list: (a) the left index and (b) the right index of the array that denote the region of the array that is still being searched, (c) the middle point of the array,...

  • Question 23 Not yet graded /0 pts Given a Binary Search Tree that is populated with...

    Question 23 Not yet graded /0 pts Given a Binary Search Tree that is populated with numerical data, write an algorithm that will determine the sum, average, least and greatest of the values maintained in the tree. Express your algorithm in Java-like pseudodode. You may use any of the operations supported by the Binary Search Tree implementation used for Programming Project #5. Your solution must be developed at the application level. Your Answer For the next two questions you must...

  • 14 18 11 10 19 16 23 28 15 21 Again, recall that in a Java...

    14 18 11 10 19 16 23 28 15 21 Again, recall that in a Java program, a binary heap is stored as an array with the first element in position [O] unused, in order to simplify the swim() and sink() operations. Let us denote the unused element of the array with the symbol "#". Therefore, the above complete binary tree will be represented by the following array: T[ 0..13 ] = [ #, 5, 6, 7, 14, 18, 11,...

  • Homework: Homework 8 (Module 12) Save Score: 1.5 of 3 pts 13 of 21 (21 complete)...

    Homework: Homework 8 (Module 12) Save Score: 1.5 of 3 pts 13 of 21 (21 complete) HW Score: 90.17%, 40.58 of 45 pt Problem 12.22 Question Help a) What is the optimal order quantity and the minimum annual cost for Bell Computers to order, purchase, and hold these integrated chips? The optimal order quantity after the change in pricing structure is 200 units (enter your response as a whole number). The total annual cost for Bell computers to order purchase,...

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