NOIDATUT FREE E LEARNING PLATFORM • AKTU B.TECH

Binary Tree in Data Structure

A binary tree is a hierarchical data structure of nodes where each node has at most two children, called the left child and the right child. It supports tree traversals, search trees, and heaps, and can be represented using linked nodes or arrays.


Theory & Explanation

Binary Tree Representation: Binary Search Tree, Strictly Binary Tree, Complete Binary Tree & Extended Binary Trees

1. Binary Tree Representation

A Binary Tree is a hierarchical data structure where each node has at most two children—a left child and a right child. In memory, a binary tree is represented in two primary ways:

  • Linked (Pointer/Reference) Representation: Each node is dynamically allocated as a structure/object containing data, a left child pointer, and a right child pointer (NULL/None when absent). Flexible for arbitrary shapes and frequent insertions/deletions without shifting elements.
  • Sequential (Array) Representation: Stores tree nodes level by level in a 1D array. For 0-based indexing, a node at index i has its left child at 2i + 1, right child at 2i + 2, and parent at (i - 1) / 2 (for 1-based indexing, left is 2i, right is 2i + 1, and parent is i / 2). Compact and fast for complete binary trees/heaps, though wasteful on sparse or skewed trees.

Graphical Representation: Linked vs. Array Representation of a Binary Tree

Binary Tree [10, 20, 30, 40, 50] in Linked Form & 0-Indexed Array Form
10 (idx 0)
20 (idx 1)
40 (idx 3) 50 (idx 4)
30 (idx 2)
0-Based Array Representation:
idx 0: 10 idx 1: 20 idx 2: 30 idx 3: 40 idx 4: 50

2. Special Types of Binary Trees

A. Binary Search Tree (BST)

A Binary Search Tree is a binary tree in which every node satisfies the BST ordering property: all values in its left subtree are strictly less than the node's key, and all values in its right subtree are strictly greater than the node's key (with no duplicate keys in standard definitions).

  • Example: Root 50; left child 30 (with left 20, right 40); right child 70 (with left 60, right 80). An inorder traversal of a BST always produces keys in ascending order (20, 30, 40, 50, 60, 70, 80).
  • Complexity: Search, insertion, and deletion take O(log n) average time for balanced trees, and O(n) worst time if skewed.

B. Strictly Binary Tree (Full / Proper Binary Tree)

A Strictly Binary Tree (also called a Full or Proper Binary Tree) is a binary tree in which every non-leaf (internal) node has exactly two children—that is, every node has either 0 or 2 children (no node has only one child).

  • Example: Root 1 with children 2 and 3; node 2 has children 4 and 5; 3, 4, 5 are leaves (each has 0 children, while 1 and 2 each have 2 children).
  • Exam Formula: If a strictly binary tree has n leaf (external) nodes, it has n - 1 internal nodes and 2n - 1 total nodes.

C. Complete Binary Tree

A Complete Binary Tree is a binary tree in which all levels except possibly the last are completely filled, and all nodes in the last level are placed as far to the left as possible (no missing slots before the last node at the bottom level).

  • Example: A tree with 5 nodes: root 1 (level 0), children 2 and 3 (level 1, full), and children 4 and 5 under 2 (level 2, filled left-to-right).
  • Exam Note: Every perfect binary tree is complete, and every complete binary tree maps cleanly to a contiguous array without gaps; used as the underlying shape of binary heaps.

D. Extended Binary Trees (2-Tree)

An Extended Binary Tree (also called a 2-Tree) is obtained by taking any binary tree and replacing every NULL (empty) child link with a special dummy/external node (often drawn as a square or box), called an external node (or failure node), while original tree nodes become internal nodes.

  • Example: Suppose node 3 in a binary tree has no left or right child. In the extended binary tree, two external square nodes are attached as its left and right children so that every original node now has exactly two children.
  • Exam Formulas: If an extended binary tree has I internal nodes and E external nodes, then E = I + 1. Its Internal Path Length (IPL = sum of depths of all internal nodes) and External Path Length (EPL = sum of depths of all external nodes) satisfy: EPL = IPL + 2I.

Graphical Representation: BST, Strictly, Complete & Extended Binary Trees

Quick Visual Summary of the Four Binary Tree Types
Binary Search Tree
Left < Node < Right.
e.g., 50 → L: 30, R: 70.
Inorder is sorted.
Strictly Binary Tree
Every node has 0 or 2 children.
Leaves = L → Total = 2L - 1.
Complete Binary Tree
Levels full except last; last filled left-to-right.
Fits array indices 0..n-1.
Extended Binary Tree
Every NULL link gets a □ external node.
E = I + 1, EPL = IPL + 2I.

3. C Program: Binary Search Tree (Insert & Inorder Traversal)

#include <stdio.h>
#include <stdlib.h>

struct Node {
    int key;
    struct Node left;
    struct Node right;
};

struct Node createNode(int key) {
    struct Node node = (struct Node)malloc(sizeof(struct Node));
    node->key = key;
    node->left = NULL;
    node->right = NULL;
    return node;
}

struct Node insertBST(struct Node root, int key) {
    if (root == NULL) {
        return createNode(key);
    }
    if (key < root->key) {
        root->left = insertBST(root->left, key);
    } else if (key > root->key) {
        root->right = insertBST(root->right, key);
    }
    return root;
}

void inorder(struct Node root) {
    if (root == NULL) return;
    inorder(root->left);
    printf("%d ", root->key);
    inorder(root->right);
}

int main() {
    struct Node *root = NULL;
    int keys[] = {50, 30, 70, 20, 40, 60, 80};
    int n = sizeof(keys) / sizeof(keys[0]);

    for (int i = 0; i < n; i++) {
        root = insertBST(root, keys[i]);
    }

    printf("BST Inorder Traversal (Sorted): ");
    inorder(root);
    printf("\n");

    return 0;
}

Expected Output (C):

BST Inorder Traversal (Sorted): 20 30 40 50 60 70 80 

4. Python Program: Binary Search Tree (Insert & Inorder Traversal)

class BSTNode:
    def __init__(self, key):
        self.key = key
        self.left = None
        self.right = None


def insert_bst(root, key):
    if root is None:
        return BSTNode(key)
    if key < root.key:
        root.left = insert_bst(root.left, key)
    elif key > root.key:
        root.right = insert_bst(root.right, key)
    return root


def inorder(root):
    if root is None:
        return []
    return inorder(root.left) + [root.key] + inorder(root.right)


if __name__ == "__main__":
    keys = [50, 30, 70, 20, 40, 60, 80]
    root = None
    for k in keys:
        root = insert_bst(root, k)

    print("BST Inorder Traversal (Sorted):", inorder(root))

Expected Output (Python):

BST Inorder Traversal (Sorted): [20, 30, 40, 50, 60, 70, 80]

Diagrams

binary-tree-in-data-structure - Diagram

binary-tree-in-data-structure - Diagram

Advertisement
Advertisement