THINK FIRST·CODE LATER

← All labs

BST versus AVL on the same keys

Problem

Write AvlVersusBst. Read n and n keys. Insert them, in order, into a plain BST and into an AVL tree (store each node's height; after inserting, rebalance with the LL / LR / RR / RL rotations on the way back up; count every single rotation). Print both heights, the AVL root and rotation count, and the average number of key comparisons needed to find each of the n keys in each tree.

For the input

7
1 2 3 4 5 6 7

the program prints

Keys: 7
BST height: 6, AVL height: 2
AVL root: 4, rotations: 4
Average comparisons per search: BST 4.00, AVL 2.43

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-4-data-1.txt

7
1 2 3 4 5 6 7
What your program must print for d13-4-data-1.txt
Keys: 7
BST height: 6, AVL height: 2
AVL root: 4, rotations: 4
Average comparisons per search: BST 4.00, AVL 2.43

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-4-data-1.txt

Windows PowerShell does not understand <:

Get-Content d13-4-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.