edited by
441 views
1 votes
1 votes

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

  1.   fun(m, j*2 + 1, i);
    fun(m, j*2 + 2, j);
    printf("%d ", m[i]);
  2.   fun(m, i+ 1, j);
    fun(m, i+ 2, j); 
    printf("%d ", m[i]);
  3.   fun(m, j + 2, j);
    printf("%d ", m[j]);
    fun(m, i + 2, j);
  4. fun(m, i*2 + 1, j);
    printf("%d ", m[i]);
    fun(m, i*2 + 2, j);
edited by

1 Answer

Best answer
4 votes
4 votes
// m[10] = {1,2,3,4,5,6,7,8,9};
// binary tree stored as array in m[]
// 0 index based array
void F(int m[],int start,int end) {
    if(start > end) return;
    
    F(m,2*start+1,end);         // resurse to right
        printf("%d",m[start]);  //print middle
    F(m,2*start+2,end)          // recurse to right
}

Above function Or option D prints the whole tree in INORDER.

selected by

No related questions found