THINK FIRST·CODE LATER

← All labs

A live leaderboard on an AVL tree with subtree sizes

Problem

Write AvlRanking: an AVL tree whose nodes store their height and the size of their subtree, used as a game leaderboard of distinct scores. Commands until the end of input:

  • add S — insert score S (ignore duplicates);
  • top K — print the K-th best score (no such rank if K is out of range);
  • better S — print how many scores are strictly greater than S.

Finish with the number of scores, the tree height and the median score (the ⌈n/2⌉-th smallest). top and better must run in O(log n) using the sizes.

For the input

add 520 add 480 add 610 add 550 add 430 top 1 top 3 better 500 top 9

the program prints

top 1: 610
top 3: 520
scores above 500: 3
top 9: no such rank
Scores: 5, tree height: 2
Median score: 520

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

d13-5-data-1.txt

add 520 add 480 add 610 add 550 add 430 top 1 top 3 better 500 top 9
What your program must print for d13-5-data-1.txt
top 1: 610
top 3: 520
scores above 500: 3
top 9: no such rank
Scores: 5, tree height: 2
Median score: 520

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < d13-5-data-1.txt

Windows PowerShell does not understand <:

Get-Content d13-5-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.