Binary Tree Variants Ready
nodes 5
height 2
leaves 3
  • Strict–
  • Complete–
  • Perfect–
  • Current
  • In progress
  • Checked / done
  • New
  • Breaks the rule

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.

Same nodes, special shapes

The variants are rules about the SHAPE of a binary tree: how many children each node has and how full the levels are. Strict, complete and perfect trees use the ordinary node. An extended tree adds placeholder (external) nodes for the empty spots. A threaded tree keeps the same shape but gives each node two flags, so a NULL pointer can be reused as a "thread" to the next node in inorder.

// An ordinary binary tree node
struct Node {
    int data;
    struct Node *left, *right;
};

// A threaded node: two flags say whether a pointer is a REAL child or a thread
struct TNode {
    int data;
    struct TNode *left, *right;
    int lthread;   // 1: left is a thread to the inorder predecessor
    int rthread;   // 1: right is a thread to the inorder successor
};

// Extended tree: every empty spot (NULL) becomes an external node
// (the squares in the picture). Internal nodes keep the data.

Strict (full / proper) tree

  1. Look at every node and count its children.
  2. Allowed: 0 children (a leaf) or 2 children.
  3. One node with exactly 1 child breaks the rule.
  4. Bonus: a strict tree has one more leaf than internal nodes (L = I + 1).

Complete tree

  1. Walk level by level with a queue; queue the empty spots too.
  2. The first empty spot sets gap = 1.
  3. After a gap, any real node means NOT complete.
  4. Complete trees fit in an array with no holes — that is how heaps work.

Perfect tree

  1. Count the nodes on every level.
  2. Level d must hold exactly 2^d nodes.
  3. All levels full means n = 2^(h+1) − 1 and 2^h leaves.
  4. Every perfect tree is also strict and complete.

Extended tree (2-tree)

  1. Walk the tree; wherever a child is missing, put an external node (a square).
  2. Now every real (internal) node has exactly 2 children.
  3. External nodes E = internal nodes I + 1.
  4. External path length = internal path length + 2I.

One-way (right) threaded tree

  1. Walk the tree in inorder and remember the previous node.
  2. If the previous node has no right child, its right pointer becomes a thread to the current node.
  3. Traverse: go to the leftmost node, then follow a thread or step into the right subtree.
  4. No recursion and no stack: O(1) extra space.

Two-way threaded tree

  1. Do the same, and ALSO reuse the null LEFT pointers.
  2. A null left pointer becomes a thread to the inorder predecessor.
  3. Now the tree can be walked forwards AND backwards in inorder.
  4. Cost: both flags (lthread, rthread) in every node.

How the shapes relate

  1. Perfect ⊂ complete, and perfect ⊂ strict.
  2. A complete tree may have ONE node with a single (left) child, so it need not be strict.
  3. A strict tree need not be complete: its leaves can sit on different levels.
  4. An extended tree is always strict (every internal node has 2 children).
Algorithm

Pseudocode

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

1 IS-STRICT(node)
2 if node = NULL then return true
3 if node.left = NULL and node.right = NULL then return true (a leaf)
4 if node.left = NULL or node.right = NULL then return false (exactly one child)
5 return IS-STRICT(node.left) and IS-STRICT(node.right)
Complexity

How fast is each operation?

n = number of nodes, h = height. A recursive traversal needs O(h) stack space; a threaded traversal needs O(1).

Operation Best Average Worst Speed
Check strict O(1) O(n) O(n) Grows with n
Check complete O(1) O(n) O(n) Grows with n
Check perfect O(n) O(n) O(n) Grows with n
Extend with external nodes O(n) O(n) O(n) Grows with n
Thread a tree (one-way or two-way) O(n) O(n) O(n) Grows with n
Inorder of a threaded tree (time) O(n) O(n) O(n) Grows with n
Inorder of a threaded tree (extra space) O(1) O(1) O(1) Instant

The variants side by side

Strict (full) Complete Perfect Extended
Rule Every node has 0 or 2 children Every level full except the last, which fills from the left Every level completely full Every empty spot is an external node
Nodes with exactly 1 child None At most one (the last internal node) None None
Number of nodes Odd: n = 2I + 1, between 2h + 1 and 2^(h+1) − 1 Between 2^h and 2^(h+1) − 1 Exactly 2^(h+1) − 1 I internal + (I + 1) external = 2I + 1
Leaves L = I + 1 ⌈n / 2⌉ 2^h E = I + 1 (the external nodes)
Typical use Expression trees, Huffman trees Heaps, stored in an array The ideal balanced shape Analysis: path lengths, counting

I = internal nodes, L = leaves, E = external nodes, h = height (a single node has h = 0).

One-way vs two-way threaded trees

One-way (right) Two-way (both)
What a thread replaces A NULL right pointer A NULL right AND a NULL left pointer
A thread points to The inorder successor The inorder successor and the inorder predecessor
Walk forwards (inorder) Yes — no stack Yes — no stack
Walk backwards No (a stack is needed) Yes — no stack
Flags per node 1 (rthread) 2 (lthread, rthread)

Recursive inorder vs threaded inorder

Recursive inorder Threaded inorder
Time O(n) O(n)
Extra space O(h) — the call stack O(1) — none
Needs thread flags No Yes
Inserting a node Simple Harder: the threads must be updated

Threading trades a little complexity (and a flag per pointer) for a traversal that needs no stack.

Practice

Check your understanding

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

0 / 9 correct
Question 1

Which binary tree has NO node with exactly one child?

Question 2

A strict binary tree has 7 leaves. How many internal nodes does it have?

Question 3

In a complete binary tree, how is the last level filled?

Question 4

A perfect binary tree has height 3 (the root is on level 0). How many nodes does it have?

Question 5

Is every perfect binary tree also a complete binary tree?

Question 6

An extended binary tree has 9 internal nodes. How many external nodes does it have?

Question 7

In a one-way (right) threaded binary tree, what does a thread replace?

Question 8

Why can a threaded tree be traversed in inorder without recursion or a stack?

Question 9

Which tree variant can be stored in an array with no wasted slots?

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.