DATA STRUCTURES/Student Engineering Notebook
Page 03·notes
TREE FUNDAMENTALS, TRAVERSALS & BINARY SEARCH TREES—~34 MIN READ

Binary Tree Axioms, Degree Theorems & Traversal Reconstruction

“Handshaking lemma on trees, n0 = n2 + 1 proof, Catalan structural count, and unique tree reconstruction from traversals.”

1.The Fundamental Degree Theorem: n0 = n2 + 1

Let T be an arbitrary binary tree with n nodes and E edges. Partition the nodes by their out-degree (number of children):

- n_0: Nodes with 0 children (Leaves).

- n_1: Nodes with 1 child.

- n_2: Nodes with 2 children.

Total nodes: n = n_0 + n_1 + n_2.

Proof of Degree Relation:

1. Every node in a tree except the root has exactly 1 incoming edge. Therefore: E = n - 1.

2. Each degree-1 node contributes 1 outgoing edge, and each degree-2 node contributes 2 outgoing edges. Therefore: E = n_1 + 2 * n_2.

3. Equating the two expressions for E:

n - 1 = n_1 + 2 * n_2

(n_0 + n_1 + n_2) - 1 = n_1 + 2 * n_2

n_0 - 1 = n_2 implies n_0 = n_2 + 1.

This fundamental law holds unconditionally for every binary tree.

2.Tree Traversals & Unique Reconstruction Theorems

There are four standard systematic traversal strategies:

1. Pre-Order (N-L-R): Visit Node, traverse Left subtree, traverse Right subtree.

2. In-Order (L-N-R): Traverse Left subtree, visit Node, traverse Right subtree.

3. Post-Order (L-R-N): Traverse Left subtree, traverse Right subtree, visit Node.

4. Level-Order: Breadth-First traversal using a FIFO queue.

Reconstruction Theorem:

- Pre-order's first element is always the root. Locate this root in the In-order traversal. All elements to the left of the root in In-order belong to the left subtree; all elements to the right belong to the right subtree. Recursively reconstruct.

- Why Pre-order + Post-order fails: If a node has a single child, Pre-order [A, B] and Post-order [B, A] cannot determine whether B is a left child or a right child.

WORKED EXAM ANALYSIS · STEP-BY-STEP DERIVATION

3. Worked Problem: Node Count & Traversal Reconstruction

⚠️ Pitfall Warning:

Do not confuse internal nodes with degree-2 nodes. Degree-1 nodes are internal nodes that do NOT count toward n2.

Problem: A binary tree has 14 leaf nodes and a total of 35 nodes. How many nodes have exactly 1 child? Also determine the total number of edges.

Step 1: Identify given parameters:

n_0 = 14 (leaves), Total nodes n = 35.

Step 2: Apply leaf vs degree-2 theorem:

n_0 = n_2 + 1

14 = n_2 + 1 => n_2 = 13 nodes.

Step 3: Solve for degree-1 nodes n_1:

Total nodes n = n_0 + n_1 + n_2

35 = 14 + n_1 + 13 = 27 + n_1

n_1 = 35 - 27 = 8 nodes.

Step 4: Compute total edges E:

E = n - 1 = 35 - 1 = 34 edges.

Check with outgoing edges: n_1 + 2n_2 = 8 + 213 = 8 + 26 = 34 edges. Verified!

Final Answer: 8 nodes have exactly 1 child, and the tree has 34 edges.

WORKED NUMERICAL · STUDENT DERIVATION✎ Key Takeaway: Inorder plus preorder (or postorder) fixes a binary tree uniquely. Preorder plus postorder alone does not.

Worked example: rebuild a tree from two traversals

Problem Statement:
Preorder: A B D E C F. Inorder: D B E A F C. Find the postorder.
Step 1: The root

The first preorder symbol is the root: A.

Step 2: Split the inorder

Left of A in the inorder is D B E (left subtree); right is F C (right subtree).

Step 3: Left subtree

Its preorder is B D E: root B, with D on its left and E on its right.

Step 4: Right subtree

Its preorder is C F: root C; F is left of C in the inorder, so F is C's left child.

Step 5: Postorder

Left, right, root, recursively.

↳ Result: D E B F C A
Final Answer:Postorder: D E B F C A
STUDENT PRACTICE · CONCEPT DRILL

Practice: test yourself

Original practice questions in the GATE style, with worked explanations. Not past-paper questions.

Problem 1 · FOUNDATION

A binary tree has 10 nodes with two children. How many leaves does it have?

Problem 2 · GATE-LEVEL

How many structurally different binary trees have 3 nodes?

Problem 3 · FOUNDATION

What is the largest number of nodes in a binary tree of height 4 (a single node has height 0)?

SOURCE TRANSPARENCY & CITATIONS

Primary Academic References & Exam Sources

Every concept in Notes From a B.Tech Brain is synthesized from authoritative academic texts, seminal peer-reviewed research, verified exam archives, and renowned university lectures.

Curated University Lecture Series
Introduction to Algorithms (4th Edition) ·
LECTURE SERIES
RECOMMENDED YOUTUBE EDUCATORS & FULL-COURSE PLAYLISTS

Curated for clarity, rigorous numerical solving, and semester excellence:

Abdul Bari · Data Structures & Algorithms↗Instructor: Abdul Bari · Covers: Trees, graphs, hashing, sorting — with visual algorithm animations

Visual thinkers who want to see each algorithm step animated clearly.

Open YouTube↗
Gate Smashers · Data Structures Full Course↗Instructor: Varun Singla · Covers: Arrays, linked lists, trees, BST, AVL, heaps, hashing — GATE numericals

GATE-targeted coverage with step-by-step GATE PYQ walkthroughs.

Open YouTube↗