Skip to content
Unit 4

Linked Lists & Binary Trees

Unit 4 Learning Hub

Linked Lists & Binary Trees

Core Definitions & Key Concepts

Core Definitions & Key Concepts

Compare Arrays and Linked Lists across Memory Allocation, Access Time, and Insertion/Deletion

FeatureArrayLinked List
AllocationStatic contiguous memoryDynamic non-contiguous heap nodes
Access TimeDirect O(1) by indexSequential O(n) traversal
Insertion/DeletionCostly O(n) due to shiftingFast O(1) if pointer is known (no shifting)
Memory OverheadZero pointer overheadOverhead of 4–8 bytes per pointer per node

A Doubly Linked List (Two-Way List) and write its node structure in C

A Doubly Linked List node has two pointers: one pointing to the preceding node (prev) and one to the succeeding node (next), allowing bi-directional traversal.

C
1struct DNode {
2 int data;
3 struct DNode *prev;
4 struct DNode *next;
5};

What is a Circular Linked List? State its distinguishing advantage

A linked list where the next pointer of the last node points back to the head node rather than NULL. Advantage: Any node can be designated as the starting point to traverse the entire list.

What is a Header Linked List? Differentiate Grounded and Circular Header lists

A list containing a special dummy header node at the beginning holding metadata (e.g., node count).

  • Grounded Header List: The last node's next pointer is NULL.
  • Circular Header List: The last node's next pointer points back to the header node.

Root, Leaf, Degree of a Node, and Height of a Tree

  • Root: The topmost unique node with no incoming parent edge.
  • Leaf (Terminal): A node with degree 0 (no children).
  • Degree of Node: The total number of subtrees (children) attached to it.
  • Height: The length of the longest path from root to any leaf node.

Differentiate Full Binary Tree, Complete Binary Tree, and Strictly Binary Tree

  • Strictly (Extended) Binary Tree: Every node has either exactly 0 or 2 children.
  • Full Binary Tree: All interior nodes have 2 children and all leaf nodes are at the identical maximum depth.
  • Complete Binary Tree: All levels are completely filled except possibly the last, which is filled from strictly left to right.

State the order of node visits in Inorder, Preorder, and Postorder traversals

  • Preorder (NLR): Node (Visit) -> Left Subtree -> Right Subtree.
  • Inorder (LNR): Left Subtree -> Node (Visit) -> Right Subtree.
  • Postorder (LRN): Left Subtree -> Right Subtree -> Node (Visit).

If a binary tree has N leaf nodes in a strict binary tree, how many non-leaf (internal) nodes does it have

A strictly binary tree with N leaf nodes always contains exactly N - 1 internal nodes, yielding total nodes 2N - 1.

✓Topic 1 covered: Core Definitions & Key Concepts
Next: Conceptual & Architectural Analysis↓

Conceptual & Architectural Analysis

Conceptual & Architectural Analysis

Given the Inorder and Preorder traversals, construct the unique Binary Tree

Inorder: D B E A F C Preorder: A B D E C F.

Step-by-Step Construction

  1. From Preorder, the first element is the Root: `A`.
  2. In Inorder, elements to the left of A are in the Left Subtree: {D, B, E}. Elements to the right are in the Right Subtree: {F, C}.
  3. Left Subtree `{D, B, E}`: In Preorder, B appears first, so B is the root of the left subtree. In Inorder, D is left of B (left child) and E is right of B (right child).
  4. Right Subtree `{F, C}`: In Preorder, C appears before F, so C is the root of the right subtree. In Inorder, F is to the left of C (left child).
CODE
1 A
2 / \
3 B C
4 / \ /
5 D E F

Verification: Inorder traversal is D, B, E, A, F, C (matches).

Write recursive C functions to implement Preorder, Inorder, and Postorder tree traversals

System Architecture / Concept DiagramHorizontal Scroll Enabled
#include <stdio.h>

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

void preorder(struct Node* root) {
    if (root != NULL) {
        printf("%d ", root->data); // N
        preorder(root->left);       // L
        preorder(root->right);      // R
    }
}

void inorder(struct Node* root) {
    if (root != NULL) {
        inorder(root->left);        // L
        printf("%d ", root->data); // N
        inorder(root->right);       // R
    }
}

void postorder(struct Node* root) {
    if (root != NULL) {
        postorder(root->left);      // L
        postorder(root->right);     // R
        printf("%d ", root->data); // N
    }
}
✓Topic 2 covered: Conceptual & Architectural Analysis
Next: In-Depth Analysis & Implementation↓

In-Depth Analysis & Implementation

In-Depth Analysis & Implementation

Write a complete C program to create a Singly Linked List and implement: (1) Insertion at Beginning, (2) Insertion at End, (3) Deletion of a specific key, (4) Traversal

System Architecture / Concept DiagramHorizontal Scroll Enabled
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node *next;
};

struct Node* head = NULL;

void insertBeginning(int val) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = val;
    newNode->next = head;
    head = newNode;
    printf("Inserted %d at beginning\n", val);
}

void insertEnd(int val) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = val;
    newNode->next = NULL;
    if (head == NULL) {
        head = newNode;
        return;
    }
    struct Node* temp = head;
    while (temp->next != NULL) temp = temp->next;
    temp->next = newNode;
    printf("Inserted %d at end\n", val);
}

void deleteKey(int key) {
    struct Node *temp = head, *prev = NULL;
    if (temp != NULL && temp->data == key) {
        head = temp->next;
        free(temp);
        printf("Deleted key %d from head\n", key);
        return;
    }
    while (temp != NULL && temp->data != key) {
        prev = temp;
        temp = temp->next;
    }
    if (temp == NULL) {
        printf("Key %d not found in list!\n", key);
        return;
    }
    prev->next = temp->next;
    free(temp);
    printf("Deleted key %d\n", key);
}

void display() {
    struct Node* temp = head;
    printf("List: ");
    while (temp != NULL) {
        printf("%d -> ", temp->data);
        temp = temp->next;
    }
    printf("NULL\n");
}

int main() {
    insertBeginning(20);
    insertBeginning(10);
    insertEnd(30);
    insertEnd(40);
    display(); // 10 -> 20 -> 30 -> 40 -> NULL
    deleteKey(20);
    display(); // 10 -> 30 -> 40 -> NULL
    return 0;
}

Doubly Linked Lists in detail. Write algorithms and draw pointer diagrams for inserting a node before a given node and deleting a specified node

1. Pointer Mechanics of Doubly Linked List

In a Doubly Linked List, each node maintains two pointer links (prev and next), enabling bidirectional movement.

2. Insertion Before a Given Node curr

Let new node be newNode.

CODE
1 [ prevNode ] <===> [ curr ]
2 \ /
3 [ newNode ]

Algorithm:

  1. newNode->data = val;
  2. newNode->next = curr;
  3. newNode->prev = curr->prev;
  4. if (curr->prev != NULL) curr->prev->next = newNode; else head = newNode;
  5. curr->prev = newNode;

3. Deletion of a Node delNode

  1. If delNode == head: head = delNode->next;
  2. If delNode->next != NULL: delNode->next->prev = delNode->prev;
  3. If delNode->prev != NULL: delNode->prev->next = delNode->next;
  4. free(delNode);

Both operations execute in strictly O(1) time without requiring traversal to locate the previous node.

✓Topic 3 covered: In-Depth Analysis & Implementation
Next: Important Exam Points & Quick Revision↓

Important Exam Points & Quick Revision

Important Exam Points & Quick Revision

In BCA-102N Unit 4, tree traversal construction (reconstructing a binary tree from Inorder + Preorder sequences) is an examiner favorite. Always show the step-by-step subtree partitioning and draw the final verified tree diagram.

Practice Ready

Ready to practice Unit 4?

You've read the core concepts. Take the balanced 10-question practice set to test your mastery!

Save progress