Skip to content
Azhimathi Mayam logoDSA Study HubPCCST303 · KTU · S3
Module 3 · syllabusAsked 2× in KTU papersLesson live

Binary tree representation

Store a binary tree in an array (children at 2i and 2i + 1) or with linked nodes — and convert between drawing and array.

Syllabus says: “Binary Tree Representation”

On this page · 11 sections
ormakkurippu ✎
Never-forget card

1-based array: LEFT = 2i, RIGHT = 2i + 1, PARENT = ⌊i/2⌋. (0-based: 2i + 1, 2i + 2, ⌊(i − 1)/2⌋.)

Picture it

House numbers on a street that branches: house i's two children live at 2i and 2i + 1 — you can find anyone without asking for directions.

Write this in the exam

In the sequential representation, the node numbered i (1-based, level order) is stored at index i, its children at 2i and 2i + 1 and its parent at ⌊i/2⌋; in the linked representation each node has left, data and right fields.

What you will be able to do

  • Store a binary tree in an array and draw the tree from an array (May 2026 Q5, model paper Q6).
  • Write the linked node structure.
  • Compare the two representations.

Linked representation

C
struct node {
    struct node *left;
    int data;
    struct node *right;
};
/* a tree with n nodes has 2n links, of which n + 1 are NULL */

Sequential (array) representation

Number the positions level by level, left to right, starting at 1 for the root — as if the tree were complete. Store each node at its number; missing positions stay empty.

DBEACF
Tree to store.
A
1
B
2
C
3
D
4
E
5
6
F
7
Array form. Position 6 (C's left child) is empty. Check: F is at 7 = 2·3 + 1, the right child of C.
Exam tipArray → tree (how to answer ‘construct the tree from the array’)

Index 1 is the root. For every filled index i, draw a[2i] as its left child and a[2i + 1] as its right child. Skip empty cells — and remember an empty cell's children must also be empty.

Now watch it run

Every frame comes from really running the algorithm on your input — change it and break it! Tap ▶ Play, or step one frame at a time with ◀ ▶.

Loading visualizer…

C implementation

binary_heap.ctested ✓ 199/199 cases
C
/* Binary heap and priority queue (array) — KTU PCCST303, Module 3 (KTU May 2026 Q16b)
 * Root at index 0; children of i are 2i+1 and 2i+2; parent is (i-1)/2.
 * First token: MAX or MIN. Commands:
 *   B n k1 .. kn   build a heap bottom-up from the keys (O(n))
 *   I x            insert: put x at the end and sift it up
 *   D              delete the root: move the last key to the root and sift it down
 * After every command: optional result line, then "HEAP: a[0] a[1] ...".                 */
#include <stdio.h>
#include <string.h>

#define CAP 100
int a[CAP], n = 0, isMax = 1;

int beats(int x, int y) { return isMax ? x > y : x < y; }   /* should x be above y? */
void swap(int i, int j) { int t = a[i]; a[i] = a[j]; a[j] = t; }

void siftUp(int i) {
    while (i > 0 && beats(a[i], a[(i - 1) / 2])) { swap(i, (i - 1) / 2); i = (i - 1) / 2; }
}

void siftDown(int i) {
    for (;;) {
        int l = 2 * i + 1, r = 2 * i + 2, b = i;
        if (l < n && beats(a[l], a[b])) b = l;
        if (r < n && beats(a[r], a[b])) b = r;
        if (b == i) return;
        swap(i, b);
        i = b;
    }
}

void insert(int x) {
    if (n == CAP) { printf("OVERFLOW\n"); return; }
    a[n] = x;
    siftUp(n++);
}

void deleteRoot(void) {
    if (n == 0) { printf("UNDERFLOW\n"); return; }
    printf("removed %d\n", a[0]);
    a[0] = a[--n];
    siftDown(0);
}

void build(void) { int i; for (i = n / 2 - 1; i >= 0; i--) siftDown(i); }

int main(void) {
    char kind[4], cmd[4];
    int i, k, x;
    if (scanf("%3s", kind) != 1) return 1;
    isMax = strcmp(kind, "MIN") != 0;
    while (scanf("%3s", cmd) == 1) {
        if (cmd[0] == 'B' && scanf("%d", &k) == 1 && k >= 0 && k <= CAP) {
            n = 0;
            for (i = 0; i < k && scanf("%d", &x) == 1; i++) a[n++] = x;
            build();
        } else if (cmd[0] == 'I' && scanf("%d", &x) == 1) insert(x);
        else if (cmd[0] == 'D') deleteRoot();
        else { printf("BAD COMMAND\n"); break; }
        printf("HEAP:");
        for (i = 0; i < n; i++) printf(" %d", a[i]);
        printf("\n");
    }
    return 0;
}
Sample run · May 2026 Q16b · real output from gcc

Input (stdin)

MAX
B 7 20 12 30 25 18 8 24
D

Output (stdout)

HEAP: 30 25 24 12 18 8 20
removed 30
HEAP: 25 20 24 12 18 8

Open in the Code Lab →

Array vs linked

ArrayLinked
Find parent / childO(1) by formulachild O(1); parent needs an extra pointer
Memory for complete treesexcellent (no pointers)2 pointers per node
Memory for skewed treesterrible (2^k − 1 slots)fine
Insert / delete in the middlehardeasy
Typical useheapsBSTs, expression trees

Asked in KTU exams (2)

Quick check — can you get them all?

Question 1

Q1.1-based array: node at index 5. Its right child is at…

Question 2

Q2.A right-skewed tree of depth k in an array needs…

Question 3

Q3.Linked node for a binary tree has…

Practice

Every visualizer has a Practice mode: predict the next state before the animation reveals it. You can also work through the topic's question set on the practice page.

Exam-ready summary

  • Array (1-based): children 2i, 2i + 1; parent ⌊i/2⌋.
  • Linked: left, data, right; n + 1 NULL links.
  • Arrays suit complete trees (heaps); links suit everything else.

References

  • Horowitz, Sahni & Anderson-Freed, Fundamentals of Data Structures in C, 2/e — §5.2.3 (Binary tree representations)

Explanations on this page are original. Textbook references point to where the standard treatment can be read; no text is reproduced from them.

Prefer revising from notes? Module-wise notes and question papers on Azhimathi Mayam (opens in a new tab)