C Notes
Learn tree data structure in C with BST implementation covering insertion, deletion, searching, inorder/preorder/postorder traversals, and tree operations.
A tree is a hierarchical data structure consisting of nodes connected by edges. Unlike linear structures like arrays and linked lists, trees allow us to represent hierarchical relationships and enable efficient searching, sorting, and insertion operations. The Binary Search Tree (BST) is the most commonly used tree variant.
Tree Terminology
| Term | Definition |
|---|---|
| Root | Top node with no parent |
| Node | Element containing data and child pointers |
| Leaf | Node with no children |
| Height | Longest path from root to leaf |
| Depth | Distance from root to a node |
| Subtree | A node and all its descendants |
| Degree | Number of children a node has |
Binary Search Tree Properties
A BST maintains this invariant: for every node, all values in the left subtree are smaller, and all values in the right subtree are larger. This allows O(log n) search, insertion, and deletion on average.
Complete BST Implementation
Inorder (sorted): 10 20 30 35 40 50 60 70 80 Preorder: 50 30 20 10 40 35 70 60 80 Postorder: 10 20 35 40 30 60 80 70 50 Height: 3 Total nodes: 9 Leaf nodes: 4 Min: 10, Max: 80 Search 40: Found Search 45: Not Found After deleting 30 (node with 2 children): Inorder: 10 20 35 40 50 60 70 80
Level Order Traversal (BFS)
#include <stdio.h>
#include <stdlib.h>
typedef struct TreeNode {
int data;
struct TreeNode *left, *right;
} TreeNode;
TreeNode* createNode(int data) {
TreeNode *n = malloc(sizeof(TreeNode));
n->data = data; n->left = n->right = NULL;
return n;
}
// Queue for BFS
typedef struct QNode {
TreeNode *treeNode;
struct QNode *next;
} QNode;
typedef struct { QNode *front, *rear; } Queue;
void enqueue(Queue *q, TreeNode *node) {
QNode *n = malloc(sizeof(QNode));
n->treeNode = node; n->next = NULL;
if (!q->rear) { q->front = q->rear = n; return; }
q->rear->next = n;
q->rear = n;
}
TreeNode* dequeue(Queue *q) {
if (!q->front) return NULL;
QNode *temp = q->front;
TreeNode *node = temp->treeNode;
q->front = temp->next;
if (!q->front) q->rear = NULL;
free(temp);
return node;
}
void levelOrder(TreeNode *root) {
if (!root) return;
Queue q = {NULL, NULL};
enqueue(&q, root);
while (q.front) {
TreeNode *curr = dequeue(&q);
printf("%d ", curr->data);
if (curr->left) enqueue(&q, curr->left);
if (curr->right) enqueue(&q, curr->right);
}
printf("\n");
}
int main() {
TreeNode *root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
root->right->left = createNode(6);
root->right->right = createNode(7);
printf("Level Order: ");
levelOrder(root);
return 0;
}Level Order: 1 2 3 4 5 6 7
Traversal Comparison
| Traversal | Order | Use Case |
|---|---|---|
| Inorder | Left → Root → Right | Get sorted elements from BST |
| Preorder | Root → Left → Right | Create a copy of tree, serialize |
| Postorder | Left → Right → Root | Delete tree, evaluate expressions |
| Level Order | Level by level (BFS) | Find shortest path, level-wise ops |
Interview Questions on Trees
Q1: How do you check if a binary tree is a valid BST? Use inorder traversal and verify that each element is greater than the previous. Or use min/max bounds recursively.
Q2: What's the time complexity of BST operations? Average case: O(log n) for search, insert, delete. Worst case (skewed tree): O(n). Use balanced trees (AVL, Red-Black) for guaranteed O(log n).
Q3: How do you find the Lowest Common Ancestor (LCA) in a BST? Starting from root, if both values are smaller go left, if both are larger go right. When they split, the current node is the LCA.
Q4: Can you convert a sorted array to a balanced BST? Yes – use the middle element as root, recursively build left subtree from the left half and right subtree from the right half.
Q5: What is the difference between a complete and a full binary tree? Complete: all levels are fully filled except possibly the last, which is filled from left. Full: every node has either 0 or 2 children.
Summary
Trees are hierarchical structures that enable efficient O(log n) operations. Binary Search Trees maintain sorted order through their left-smaller, right-larger property. Master the three recursive traversals (inorder, preorder, postorder) and level-order BFS. Understanding deletion with three cases (leaf, one child, two children) is essential for interviews. For production code, consider self-balancing variants like AVL or Red-Black trees to avoid worst-case O(n) behavior.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Trees in C – Binary Search Tree Implementation with Traversals.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, data, structures, trees, trees in c – binary search tree implementation with traversals
Related C Programming Topics