his assignment requires you to write a non-verbose input-driven java program for maintaining a binary search tree (BST) of integer elements (negative, zero or positive)

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
icon
Related questions
Question

This assignment requires you to write a non-verbose input-driven java program for maintaining a binary search tree (BST) of integer elements (negative, zero or positive). The speciality of your BST will be that it
will record the frequency of occurrence for each integer element in it.


Actions provisioned on the BST. When your program is running, a user should be able to select one of the following actions to be performed on the BST.


1. Insert an element into the BST
2. Search for an element in the BST
3. Find the maximum element from the BST
4. Find the minimum element from the BST
5. Print the elements in the BST in preorder
6. Print the elements in the BST in postorder
7. Print the elements in the BST in inorder
8. Delete an element


Anything else to exit the program. The user will provide their choice of action on the BST, by entering one of the numbers between 1 to 8 that
correspond to their choice. For example, if the user wishes to insert an element, they will enter the number 1.


If the user wishes to print the inorder traversal of the tree, they will enter the number 7, and so on. Record the frequency for each element. A user may insert the same integer element into the BST more than once. An element will be recorded in a unique node in the BST. That node will also store the frequency of occurrence for the element. When a user asks to insert an element that is already present in the BST, its frequency will be incremented by 1 and no new node will be created in the BST. When an element with frequency more than 1 is asked to be deleted, its frequency will be decremented by 1.

At any point of time, the frequency of an element in the BST would denote the difference between the number of times it has been inserted into the BST and the number of times it has been deleted from the BST. When the number of times an element has been deleted equals the number of times it has been inserted, the frequency of occurrence of the element will become 0. That is when the BST node of the element would have to be deleted altogether from the BST. This will ensure that unused memory is freed by the program. User inputs. Your program will not receive any command-line input when you execute the program. However,
it will accept inputs from the user at run-time. Your program will ask for the following two types of inputs.


1. An action (any one out of the eight described above) to be performed on the BST


2. An integer element to be inserted to, searched for or deleted from the BST


When the program asks for any kind of input from the user, it will be non-verbose. In the rst case, there will be no menu printed before the user is prompted for the input. In the second case, there will be no description
printed about the input it is expecting. The user of the program will always know what input to provide when the program is expecting one.
[Hint: You may initially write the program in a verbose manner. Once you have tested the program and it is working well, you may turn o the verbose feature, particularly before submission. You may then comment
out all those System.out.print function calls that pertain to the verbose outputs.

Alternatively, you may consider including a feature that will turn o the verbose outputs from a single point in your code. This can be done using a boolean (static or member) flag variable such that all the user-friendly System.out.print function calls would be executed only if the flag is true.]
1


Printing an element. An element from the BST will always be printed along with its frequency.

For example,
if there is a node in the BST that contains the element −7, and its frequency is presently recorded as 2, the data in the node will be printed as
−7(2)


If the element (say 122) is not present in the BST, that will be reported as having the frequency 0. The output will look as follows.
122(0)


Searching an element. An element will be searched following the usual searching algorithm for a BST. If found, it will be reported along with its frequency as described above. If not found, it will be reported with
frequency 0.

Inserting an element. An element to be inserted in a BST may or may not already be present in the BST.


If it is not present in the BST, a new leaf node will be created in the BST that will store the element with frequency 1. If the element is already in a node in the BST, inserting it again will only increment the currently
recorded frequency of the element by 1.


Deleting an element. An element to be deleted from a BST must have frequency ≥ 1. Otherwise, the deletion operation will fail. On deleting the element, the currently recorded frequency of the element will be
decremented by 1. If that brings down the frequency of the element to 0, the node for the element will then be deleted altogether from the BST.

Library or package allowed. You are only allowed to use the Scanner class of the java.util package.

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Stack
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning