THINK FIRST·CODE LATER

← All labs

Build an AVL Tree

Problem

Read an integer n, then n integer keys. Insert them in order into an initially empty AVL tree (duplicates are ignored). Then print the pre-order traversal (keys separated by single spaces) and the height of the tree. The height of an empty tree is −1 and of a single node 0. If the tree is empty, print (empty) instead of the keys.

Because the rotations change the shape, the pre-order traversal shows whether you rebalanced correctly.

Input:

5 10 20 30 40 50

Output:

Pre-order: 20 10 40 30 50
Height: 2

Empty input (0):

Pre-order: (empty)
Height: -1

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.