This assignment requires you to write a non- verbo se input-driven java program for ma inta ining a binary sear tree (BST ) of integer elements (neg ative, zero or positive). The speciality of your B ST will be that it will reco the frequeney of oce urre nee for each integer element in it. Actions provisioned on the BST. When your program is running, a user should be a ble to select one the following actio ns to be per formed on the BST. 1. Ins ert an element into the BST 2. Sen rch for an edement in the BST 3. Find the maximum dement from the BST 4. Find the minimum edement from the BST 5. Print the dements in the BST in preor der 6. Print the dements in the BST in postor der 7. Print the dements in the BST in inor der 8. Delete an element

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
The Task
This assignment requires you to write a non-verbose input-driven java program for maintaining a binary searth
tree (BST ) of integer elements (neg ative, zero or positive). The speciality of your BST will be that it will record
the frequeney of occ urre nce for each integer element in it.
Actions provis ioned on the BST. When your program is running, a user should be a ble to select one of
the following actio ns to be per formed on the B ST.
1. Insert an element into the BST
2. Sea rch for an dement 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 preor der
6. Print the elements in the B ST in postor der
7. Print the elements in the BST in inor der
8. Delete an element
Any thing dse to exit the progra m
When the program is execut ed, 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 traver sal of the tre, they will enter the
number 7, and so on.
Record the frequency for each element. A user may insert the sa me integer element into the BST more
than once. An element will be rec orded in a unique node in the BST. That node will a lso store the frequency
of occurre nce 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 deno te the difference between the number of times it has been
inserted into the BST and the mumber of times it has been delet ed from the BST. When the number of times
element has been deleted equals the number of times it has been inser ted, the frequency of occurrence of the
element will become 0. That is when the BST node of the element would have to be deleted altog ether from
the BST. This will ensure that unused memor y is freed by the progra m.
Transcribed Image Text:The Task This assignment requires you to write a non-verbose input-driven java program for maintaining a binary searth tree (BST ) of integer elements (neg ative, zero or positive). The speciality of your BST will be that it will record the frequeney of occ urre nce for each integer element in it. Actions provis ioned on the BST. When your program is running, a user should be a ble to select one of the following actio ns to be per formed on the B ST. 1. Insert an element into the BST 2. Sea rch for an dement 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 preor der 6. Print the elements in the B ST in postor der 7. Print the elements in the BST in inor der 8. Delete an element Any thing dse to exit the progra m When the program is execut ed, 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 traver sal of the tre, they will enter the number 7, and so on. Record the frequency for each element. A user may insert the sa me integer element into the BST more than once. An element will be rec orded in a unique node in the BST. That node will a lso store the frequency of occurre nce 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 deno te the difference between the number of times it has been inserted into the BST and the mumber of times it has been delet ed from the BST. When the number of times element has been deleted equals the number of times it has been inser ted, the frequency of occurrence of the element will become 0. That is when the BST node of the element would have to be deleted altog ether from the BST. This will ensure that unused memor y is freed by the progra m.
U ser inputs. Your program will not receive any comma nd-line input when you exer ute the program. However,
it will accept inputs from the us er at run-time. Your program wil 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, sea rched for or deleted from the BST
When the program asks for any kind of input from the user, it will be non-rer bose. In the first case, there will
be no mens printed before the user is prompted for the input. In the ser ond ca se, there will be no description
printed about the input it is expect ing. 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 rer bose manner. Once you have tested the prog ram and it
is working well, you may turn off the ver bose feature, partic ula rly befor e submission. You may then co mment
out all those Sy st em.out.print function calls that pertain to the verbose outputs. Alternatively, y ou may
consider including a fea ture that will turn off the rerbose 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 Sy st em. out.print
func tion calls would be executed only if the flag is true.
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 rerorded as 2, the
data in the node will be printexl 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 present in the BST.
If it is not present in the BST, a new leaf node will be crea ted in the BST that will store the element with
frequency 1. If the element is already in a node in the B ST , inserting it again will only increment the currently
recorded frequency of the element by 1.
Searching an ele ment. An edement 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
frequene y 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 recollert at
this point that the ma ximum 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 r oot node.
Preorder, post order and inorder traversals. The code for the three traversa ls sho uld follow the principles
detailed in the lectures. Here is a very brief summary :
Preor der (node)
Post order (node)
Inorder (no de)
inorder (node.left)
postorder (node.right) print node.data
inorder (node.right)
print node.da ta
postorder (node.left)
preorder (node.left)
preorder (node.right) print node.data
Deleting an element. An edement to be deleted from a B ST must have frequency > 1. Other wise, the
deletion operation will fail. On deleting the edement, 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 altog et her from the B ST.
Library or package allowed.
You are only allowed to use the Scanner class of the jara.util packa ge.
Transcribed Image Text:U ser inputs. Your program will not receive any comma nd-line input when you exer ute the program. However, it will accept inputs from the us er at run-time. Your program wil 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, sea rched for or deleted from the BST When the program asks for any kind of input from the user, it will be non-rer bose. In the first case, there will be no mens printed before the user is prompted for the input. In the ser ond ca se, there will be no description printed about the input it is expect ing. 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 rer bose manner. Once you have tested the prog ram and it is working well, you may turn off the ver bose feature, partic ula rly befor e submission. You may then co mment out all those Sy st em.out.print function calls that pertain to the verbose outputs. Alternatively, y ou may consider including a fea ture that will turn off the rerbose 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 Sy st em. out.print func tion calls would be executed only if the flag is true. 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 rerorded as 2, the data in the node will be printexl 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 present in the BST. If it is not present in the BST, a new leaf node will be crea ted in the BST that will store the element with frequency 1. If the element is already in a node in the B ST , inserting it again will only increment the currently recorded frequency of the element by 1. Searching an ele ment. An edement 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 frequene y 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 recollert at this point that the ma ximum 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 r oot node. Preorder, post order and inorder traversals. The code for the three traversa ls sho uld follow the principles detailed in the lectures. Here is a very brief summary : Preor der (node) Post order (node) Inorder (no de) inorder (node.left) postorder (node.right) print node.data inorder (node.right) print node.da ta postorder (node.left) preorder (node.left) preorder (node.right) print node.data Deleting an element. An edement to be deleted from a B ST must have frequency > 1. Other wise, the deletion operation will fail. On deleting the edement, 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 altog et her from the B ST. Library or package allowed. You are only allowed to use the Scanner class of the jara.util packa ge.
Expert Solution
steps

Step by step

Solved in 2 steps with 9 images

Blurred answer
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY