Core Definitions & Key Concepts
Core Definitions & Key Concepts
Compare Arrays and Linked Lists across Memory Allocation, Access Time, and Insertion/Deletion
| Feature | Array | Linked List |
|---|---|---|
| Allocation | Static contiguous memory | Dynamic non-contiguous heap nodes |
| Access Time | Direct O(1) by index | Sequential O(n) traversal |
| Insertion/Deletion | Costly O(n) due to shifting | Fast O(1) if pointer is known (no shifting) |
| Memory Overhead | Zero pointer overhead | Overhead 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.
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
nextpointer is NULL. - Circular Header List: The last node's
nextpointer 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.
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
- From Preorder, the first element is the Root: `A`.
- In Inorder, elements to the left of
Aare in the Left Subtree:{D, B, E}. Elements to the right are in the Right Subtree:{F, C}. - Left Subtree `{D, B, E}`: In Preorder,
Bappears first, soBis the root of the left subtree. In Inorder,Dis left ofB(left child) andEis right ofB(right child). - Right Subtree `{F, C}`: In Preorder,
Cappears beforeF, soCis the root of the right subtree. In Inorder,Fis to the left ofC(left child).
1 A2 / \3 B C4 / \ /5 D E FVerification: Inorder traversal is D, B, E, A, F, C (matches).
Write recursive C functions to implement Preorder, Inorder, and Postorder tree traversals
#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
}
}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
#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.
1 [ prevNode ] <===> [ curr ]2 \ /3 [ newNode ]Algorithm:
newNode->data = val;newNode->next = curr;newNode->prev = curr->prev;if (curr->prev != NULL) curr->prev->next = newNode; else head = newNode;curr->prev = newNode;
3. Deletion of a Node delNode
- If
delNode == head:head = delNode->next; - If
delNode->next != NULL:delNode->next->prev = delNode->prev; - If
delNode->prev != NULL:delNode->prev->next = delNode->next; free(delNode);
Both operations execute in strictly O(1) time without requiring traversal to locate the previous node.
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.
Ready to practice Unit 4?
You've read the core concepts. Take the balanced 10-question practice set to test your mastery!