Consider the following c program which forms a complete binary tree , it is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
#include<stdio.h>
void fun(int m[], int i, int j) {
if(i > j) return;
I;
II;
III;
}
int main() {
int m[] = {4, 2, 5, 1, 3};
int size = sizeof(m)/sizeof(int);
fun(m, 0, size-1);
return 0;
}
The functions to print left sub tree and right sub tree
- fun(m, j*2 + 1, i);
fun(m, j*2 + 2, j);
printf("%d ", m[i]);
- fun(m, i+ 1, j);
fun(m, i+ 2, j);
printf("%d ", m[i]);
- fun(m, j + 2, j);
printf("%d ", m[j]);
fun(m, i + 2, j);
- fun(m, i*2 + 1, j);
printf("%d ", m[i]);
fun(m, i*2 + 2, j);