Question

3. Design a data structure D that can store integers. The data structure should be ale to store nent can appear multiple times. The da the following operations in O(log n) time, where n is the number of distinct integers stored in D . add(x): Adds/inserts integer a into D. Even if belongs to D, z should still be added .frequencyx) Nuber of times r appears in D search(x): Returns true if is in D order (y): Number of elements in D that are larger than y. The element y may or may not be in D If you are using BST and would like to balance the tree, you may simply write balance and assume that this operation will take O(log n) time. Analyze the worst-case run time of the operations.

Show steps. Please.

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

Consider a self-balancing binary tree which is represented as an AVL tree.

The tree become a skewed tree, if the assumed tree is a self-balancing binary tree.

add(x):

  • The insertion of the element in the BST, search for an empty space to keep the element there.
  • To insert the element in the tree, compare the element with the root node first.
  • If the value to be inserted is less than the root node, then move the insertion operation to the left tree of the root node.
  • Otherwise, go to the right subtree for insertion.
  • Keep doing the same operation, while getting the NULL value, place the value to be inserted in that place.

Since, the tree is a self-balancing tree which is an AVL tree whose height is O (log n).

Therefore, the time complexity of the insertion is O (Log n).

frequency(x):

The frequency count will be done by start the traversal from the left to the right and update the count whenever get the element.

The time complexity would be O (right – left + 1) or O (n).

A more efficient approach can be used which is unordered_map in c++.

This will take time of O (Log n).

search (x):

  • Compare the element with the root node and traverse the part according to the method discussed above in Add(x).
  • And when the element is found, return true.
  • If the value is returned as -1, then it means the element which is searched is not found in the tree. So, return false.
  • The time taken to search an element in the tree is log n.

Therefore, the complexity of the search is log n.

order (x):

Create a field as “less_nodes” to store the number of nodes which are less than y.

To count of the elements which are greater than the element y, three cases will be there:

Case 1: In this case, if the value y is greater than the root node. Then, traverse the right subtree.

Case 2: If the value of y is less than the root node, increase the count by the number of successor of the right child. The successor is those of the current nodes.

Also, increase the count by 2, one for itself and the other is due to the current node.

After that move to the left subtree.

Case 3: If the value of y is equal to the root node, add the value to the field less_nodes and add 1. Also, check whether the right child exist or not.

The time complexity would be O (Log n) as n represents the number of nodes in the tree.

Add a comment
Know the answer?
Add Answer to:
Show steps. Please. 3. Design a data structure D that can store integers. The data structure...
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
  • Problem 3 Suppose that you have a set of n large, orderable, objects, each of size...

    Problem 3 Suppose that you have a set of n large, orderable, objects, each of size q, so that it requires time e(a) to time to compute a hash function h(a) for any object and requires time e(g) to compare any two objects. Describe a compound data structure, built out of a heap and a hash table, that supports the following operations with the specified run times.. elt (x) Is x an element of the set? Expected run time O(g)....

  • A dynamic array is a data structure that can support an arbitrary number of append (add...

    A dynamic array is a data structure that can support an arbitrary number of append (add to the end) operations by allocating additional memory when the array becomes full. The standard process is to double (adds n more space) the size of the array each time it becomes full. You cannot assume that this additional space is available in the same block of memory as the original array, so the dynamic array must be copied into a new array of...

  • 5) Simulate data according to the following. Please provide a log file and any supporting exhibits:...

    5) Simulate data according to the following. Please provide a log file and any supporting exhibits: x = 12 ,50 Y=1+2X + e e N(0,1) a) Provide an estimate of the coefficients in y BB2x +E b) Are B1 B2 "near" the true values? ) Store the coefficients in a matrix 8 d) Repeat the simulation 100 times, estimating the equation and storing the coefficients each time e) What is the distribution of simulated coefficients B.B2? a. b. c. Mean...

  • PYTHON 3 - please show format Question 2. ) Use the Design Recipe to write a...

    PYTHON 3 - please show format Question 2. ) Use the Design Recipe to write a function yesOrNo which has no parameters. When called, it gets input from the user until the user types either 'yes' or 'no', at which point the function should return True if the user typed 'yes' and False if the user typed 'no'. Any other entries by the user are ignored and another value must be input. For example: Test Input Result print(yesOrNo()) hello blank...

  • must be coded in c++ without any STL libraries sadly :( so im struggling on this...

    must be coded in c++ without any STL libraries sadly :( so im struggling on this problem, any help would be greatly appreciated, thanks in advance! :) assignment is due tomorrow and im really struggling on this last question :( a. Begin by implementing a BST for integers. The underlying structure is a linked list. You need these methods: i. BST(); -- Constructor ii. void put (int) – Inserts a value into the BST. iii. Void put(int[] a) – Inserts...

  • Example 1.9: 1.23 "The median of an ordered set is an element such that the number...

    Example 1.9: 1.23 "The median of an ordered set is an element such that the number of elements less than the median is within one of the number that are greater, assuming no ties. a. Write an algorithm to find the median of three distinct integers a, b, and c. b. Describe D. the set of inputs for your algorithm. in light of the discussion in Sec- tion 1.4.3 following Example 1.9. c. How many comparisons does your algorithm do...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • Stacks and Java 1. Using Java design and implement a stack on an array. Implement the...

    Stacks and Java 1. Using Java design and implement a stack on an array. Implement the following operations: push, pop, top, size, isEmpty. Make sure that your program checks whether the stack is full in the push operation, and whether the stack is empty in the pop operation. None of the built-in classes/methods/functions of Java can be used and must be user implemented. Practical application 1: Arithmetic operations. (a) Design an algorithm that takes a string, which represents an arithmetic...

  • Question 1 An array is NOT: A - Made up of different data types. B - Subscripted by integers. C -...

    Question 1 An array is NOT: A - Made up of different data types. B - Subscripted by integers. C - A consecutive group of memory chunks. D - None of the choices. Question 2 How many times is the body of the loop executed? int i=1; while(true) { cout << i; if(++i==5) break; } A - Forever B - 4 C - 5 D - 6 E - 0 Question 3 What is wrong with the following piece of...

  • Consider a variation of Merge sort called 4-way Merge sort. Instead of splitting the array into...

    Consider a variation of Merge sort called 4-way Merge sort. Instead of splitting the array into two parts like Merge sort, 4-way Merge sort splits the array into four parts. 4-way Merge divides the input array into fourths, calls itself for each fourth and then merges the four sorted fourths. a)Implement 4-way Merge sort from Problem 4 to sort an array/vector of integers and name it merge4. Implement the algorithm in the same language you used for the sorting algorithms...

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