.ctions pr OV isioned on the W hen your ne following actions to be performed on the BST. 1. Insert an element into the BST 2. Sear ch 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 progr am

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
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
W hen the program asks for any kind of input from the user, it will be non-verbose. In the first case, there will
be no menu printed before the user is prompted for the input. In the second case, there will be no de scription
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 off the verbose feature, particularly before submission. You may then comment
out all those System. out. print function calls that pertain to the verbose output s. Alternatively, you may
consider including a feature that will turn off 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-frie ndly Sy st em. out. print
function calls would be executed only if the flag is true.]
Printing an element. Anelement 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)
Inserting an element. An element to be inserted in a BST may or may not already be prese nt 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.
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
freque ncy 0 as described above.
Maximum and minimum elements. If the tree is empty, in both cases, the program will print 0(0).
Otherwise, the program will print the maximum and minimum elements from the tree. We may recollect at
this point that the maximum element in a BST is the rightmost node starting from the root node. Similarly,
the minimum element in a BST is the leftmost node starting from the root node.
Preorder, postorder and inorder traversals. The code for the three traversals should follow the principles
detailed in the lectures. Here is a very brief sum mary:
Preorder (node)
Postorder (node)
Inorder (пode)
print node.da ta
postorder (node.left)
inorder (node.left)
preorder (node left)
postorder (node.right) print node.data
inorder (node.right)
preorder (node.right) | print node.data
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 Sc anner cla ss of the java. ut il package.
Transcribed Image Text: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 W hen the program asks for any kind of input from the user, it will be non-verbose. In the first case, there will be no menu printed before the user is prompted for the input. In the second case, there will be no de scription 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 off the verbose feature, particularly before submission. You may then comment out all those System. out. print function calls that pertain to the verbose output s. Alternatively, you may consider including a feature that will turn off 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-frie ndly Sy st em. out. print function calls would be executed only if the flag is true.] Printing an element. Anelement 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) Inserting an element. An element to be inserted in a BST may or may not already be prese nt 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. 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 freque ncy 0 as described above. Maximum and minimum elements. If the tree is empty, in both cases, the program will print 0(0). Otherwise, the program will print the maximum and minimum elements from the tree. We may recollect at this point that the maximum element in a BST is the rightmost node starting from the root node. Similarly, the minimum element in a BST is the leftmost node starting from the root node. Preorder, postorder and inorder traversals. The code for the three traversals should follow the principles detailed in the lectures. Here is a very brief sum mary: Preorder (node) Postorder (node) Inorder (пode) print node.da ta postorder (node.left) inorder (node.left) preorder (node left) postorder (node.right) print node.data inorder (node.right) preorder (node.right) | print node.data 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 Sc anner cla ss of the java. ut il package.
The Task
This assignment requires you to write a non-verbose input-driven java program for m aint aining 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. Sear ch 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
When the progr am is executed, the user will provide their choice of action on the BST, by entering one of the
numbers between 1 to 8 that corre spond 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 inor der 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 uni que node in the BST. That node will al so 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 frequen cy of occurren ce of the
element will be come 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.
Transcribed Image Text:The Task This assignment requires you to write a non-verbose input-driven java program for m aint aining 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. Sear ch 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 When the progr am is executed, the user will provide their choice of action on the BST, by entering one of the numbers between 1 to 8 that corre spond 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 inor der 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 uni que node in the BST. That node will al so 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 frequen cy of occurren ce of the element will be come 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.
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Array
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