THINK FIRST·CODE LATER

← All labs

Full, complete, perfect or degenerate?

Problem

Four words describe the shape of a binary tree:

  • full — every node has 0 or 2 children;
  • complete — every level is filled, except possibly the last, which is filled from the left;
  • perfect — every level is completely filled (2ʰ − 1 nodes for height h);
  • degenerate — no node has two children: the tree is a linked list in disguise.

Write TreeKinds: each line of the input is a list of keys to insert in an empty BST. For each line, print the number of nodes, the height and the four answers. isFull and isDegenerate are recursive, isPerfect uses size and height, and isComplete uses a breadth-first traversal: in level order, no node may appear after a missing child. For the input

4 2 6 1 3 5 7
4 2 6 1 3
4 2 6 5 7
1 2 3 4
4 2 6 1
5
3 1 2

the program prints

Keys 4 2 6 1 3 5 7: 7 nodes, height 3
  full true, complete true, perfect true, degenerate false
Keys 4 2 6 1 3: 5 nodes, height 3
  full true, complete true, perfect false, degenerate false
Keys 4 2 6 5 7: 5 nodes, height 3
  full true, complete false, perfect false, degenerate false
Keys 1 2 3 4: 4 nodes, height 4
  full false, complete false, perfect false, degenerate true
Keys 4 2 6 1: 4 nodes, height 3
  full false, complete true, perfect false, degenerate false
Keys 5: 1 nodes, height 1
  full true, complete true, perfect true, degenerate true
Keys 3 1 2: 3 nodes, height 3
  full false, complete false, perfect false, degenerate true

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.

d12-8-data-1.txt

4 2 6 1 3 5 7
4 2 6 1 3
4 2 6 5 7
1 2 3 4
4 2 6 1
5
3 1 2
What your program must print for d12-8-data-1.txt
Keys 4 2 6 1 3 5 7: 7 nodes, height 3
  full true, complete true, perfect true, degenerate false
Keys 4 2 6 1 3: 5 nodes, height 3
  full true, complete true, perfect false, degenerate false
Keys 4 2 6 5 7: 5 nodes, height 3
  full true, complete false, perfect false, degenerate false
Keys 1 2 3 4: 4 nodes, height 4
  full false, complete false, perfect false, degenerate true
Keys 4 2 6 1: 4 nodes, height 3
  full false, complete true, perfect false, degenerate false
Keys 5: 1 nodes, height 1
  full true, complete true, perfect true, degenerate true
Keys 3 1 2: 3 nodes, height 3
  full false, complete false, perfect false, degenerate true

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

Windows PowerShell does not understand <:

Get-Content d12-8-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.