Binary Tree Ready
nodes 8
height 3
leaves 3
  • Current
  • In progress
  • Visited / done
  • New

Ready

Comparisons 0 Shifts 0
Ready

Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode

How it works

Every operation, in plain steps

These are exactly the steps the visualizer animates.

Words to know

The top node is the ROOT. A node's children hang below it; the node above is its PARENT. A node with no children is a LEAF. The DEPTH of a node is how many edges it is below the root, and the HEIGHT of the tree is the depth of its deepest leaf. In this lab you type a tree in level order, with - for an empty spot.

struct Node {
    char data;
    struct Node *left;
    struct Node *right;
};

//        A          level order:
//       / \         A, B, C, D, E, -, F
//      B   C
//     / \   \      (- = no node here)
//    D   E   F

Inorder

  1. Traverse the left subtree.
  2. Visit the node.
  3. Traverse the right subtree.
  4. For a BST this gives sorted order.

Preorder

  1. Visit the node first.
  2. Then the whole left subtree.
  3. Then the whole right subtree.
  4. The root is always the FIRST value.

Postorder

  1. Traverse the left subtree.
  2. Traverse the right subtree.
  3. Visit the node last.
  4. The root is always the LAST value.

Level order

  1. Put the root in a queue.
  2. Take the front node and visit it.
  3. Add its children at the rear.
  4. Repeat until the queue is empty.

Build: Preorder + Inorder

  1. The next value of preorder is the root.
  2. Find it in inorder.
  3. Values left of it form the left subtree, values right of it the right subtree.
  4. Repeat for the left part, then the right part.

Build: Postorder + Inorder

  1. Take roots from the END of postorder.
  2. Find each root in inorder to split it.
  3. Build the RIGHT part first…
  4. …because postorder read backwards is root, right, left.
Algorithm

Pseudocode

Language-independent steps. The visualizer shows the same logic in C, Python and Java.

1 INORDER(node)
2 if node = NULL then return
3 INORDER(node.left)
4 visit node.data
5 INORDER(node.right)
Complexity

How fast is each operation?

n = number of nodes, h = height. Recursive traversals use O(h) stack space; level order needs a queue as wide as the widest level. Building scans inorder for each root — storing inorder positions in a hash map makes it O(n).

Operation Best Average Worst Speed
Inorder / Preorder / Postorder O(n) O(n) O(n) Grows with n
Level order O(n) O(n) O(n) Grows with n
Build from traversals (scan) O(n log n) O(n log n) O(n²) Grows with n
Build from traversals (hash map) O(n) O(n) O(n) Grows with n

Which two traversals rebuild a tree?

Traversals you have Unique tree? Why
Preorder + Inorder Yes Preorder gives the root, inorder splits left from right
Postorder + Inorder Yes Postorder gives the root (last), inorder splits left from right
Level order + Inorder Yes The first level-order value in each part is its root
Preorder + Postorder Only for FULL trees Without inorder, a lone child could be left or right
Just one traversal No Many different trees share it

Every value must be different — with repeated values you cannot tell which copy is the root.

Special binary trees

Type Rule Example use
Full Every node has 0 or 2 children Expression trees, Huffman trees
Complete Every level full except the last, which fills from the left Heaps (stored in an array)
Perfect Every level completely full: 2^(h+1) − 1 nodes Balanced reference shape
Skewed Every node has only one child Worst case: it is really a linked list
Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 6 correct
Question 1

Preorder is A B D E C F. Which node is the root?

Question 2

Postorder is D E B F C A. Which node is the root?

Question 3

Inorder is D B E A F C and the root is A. Which values are in the LEFT subtree?

Question 4

When building from postorder + inorder, why is the RIGHT subtree built first?

Question 5

Which pair of traversals can NOT always identify a unique binary tree?

Question 6

Which data structure does a level order traversal use?

Assessment

Trees quiz

Test yourself: 5 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.