COMP1020 2023 Lab13
COMP1020 2023 Lab13
COMP1020: OOP and Data Structures Spring 2023 - Week 15, June 5, 2023
1. No submission by the deadline will incur 0.25 point deduction for each problem (most of the
problems are due at the end of the lab session).
2. The instructor will deduct an additional 0.25 point per problem for each day past the deadline.
3. The penalty will be deducted until the maximum possible score for the lab practice reaches
zero (0%) unless otherwise specified by the instructor.
• Iterate over the list of words and for each word, update its frequency in the data structure. If the
word is already present in the data structure, increment its frequency by 1. If the word is not
present, add it to the data structure with a frequency of 1.
After iterating over all the words, return the data structure containing the word frequencies in increasing
order based on the word. In case-sensitive sorting, capital letters are considered smaller than non-capital
letters.
Output: The word frequencies are printed in separate lines, with each line showing the word followed by
its frequency in the form of <word: frequency>.
Test cases:
Input Output
programming is fun and challenging programming is creative and: 1
challenging: 1
creative: 1
fun: 1
is: 2
programming: 2
• Split the expression string into individual tokens using space as the delimiter.
• Initialize an empty stack to store the tree nodes.
• Iterate through each token in the expression:
– If the token is an operator (+, -, *, /), pop two nodes from the stack to be the left and
right children of a new operator node. Push the operator node back onto the stack.
– If the token is an operand, create a new operand node and push it onto the stack.
• The final node remaining on the stack will be the root of the constructed binary tree.
3. In the main method, you should read the postfix expression from the input and then output the infix
expression and the result of the expression.
Please note that the implementation assumes valid postfix expressions and does not handle error cases
such as invalid expressions or division by zero.
Input: The postfix expression as a string, each character separated by space.
Output: The output should include two lines: the first line is the infix expression and the second line is
the result of the expression.
Test Cases:
Table 2: Sample input and output for the Expression Tree program.
Input Output
23*45*+6- (((2*3)+(4*5))-6)
20
class Node {
String data ;
Node left ;
Node right ;
Node ( String data ) {
this . data = data ;
this . left = null ;
scanner . close () ;
}
}
Input:
– The first line contains two integers, N and k, representing the number of family members and the po-
sition of the kth wealthiest person, respectively.
– The next N lines contain the name and property value of each family member separated by a semicolon.
Output: The name and property value of the k-th wealthiest person, separated by space. If k is not valid,
print "Invalid". In case of a tie in property value, the person with the lexicographically smallest name
should be selected.
Test Cases:
Input Output
66
Adam;100000
Eve;200000
Sam;300000 Adam 100000
Julia;400000
Max;500000
Sophia;600000
64
Alice;800000
Charlie;600000
Bob;600000 Bob 600000
David;700000
Eve;800000
Frank;500000
class FamilyMember {
String name ;
int propertyValue ;
class HeapSort {
// Implement the heapify method
public static void heapify ( FamilyMember [] arr , int n , int i ) {
// TODO : Implement heapify logic
}
// TODO : Print the name and property value of the kth wealthiest ←-
person .
scanner . close () ;
}
}