You are currently viewing Easy Level Order Traversal of Binary Tree Code

Easy Level Order Traversal of Binary Tree Code

Level Order Traversal of Binary Tree Code

This code is for showing Level Order Traversal of a Binary tree creation using queue. In this code first created node will be treated as root node. After that another function insert() will be called for adding the child nodes. Every time one node created and sent to queue for keeping track of that node. In this code along with In-Order Traversal, Level Order Traversal is also used for traversing the tree level wise. Again queue is used for keeping track of all the nodes for displaying in Level Order Traversal mode.

For Such more codes click here and for video tutorial click here.

				
					//Level Order Traversal of Binary Tree
#include<stdio.h>

#include<stdlib.h>

typedef struct tree {
  int val;
  struct tree * lch;
  struct tree * rch;
}
tr;

typedef struct queue {
  tr * node;
  struct queue * next;
}
qu;

void enqueue(qu ** , tr * );
tr * del(qu ** );
int isempty(qu * );
tr * createroot();
void insert(qu * );
void inorder(tr * );
void levelorder(tr *);

void main() {
  qu * h = NULL;
  tr * ptr;

  ptr = createroot();
  enqueue( & h, ptr);
  insert(h);
  printf("\n\nInorder traversal form of the tree is : ");
  inorder(ptr);
  printf("\n\nLevel-order traversal form of the tree is : ");
  levelorder(ptr);  
}

tr * createroot() {
  int v;
  tr * ptr;
  printf("\nEnter the values : ");
  scanf("%d", & v);
  ptr = (tr * ) malloc(sizeof(tr));
  ptr -> val = v;
  ptr -> lch = ptr -> rch = NULL;
  return (ptr);
}

void insert(qu * h) {
  tr * ptr, * temp;
  char ch;
  int v;
  while (!isempty(h)) {
    ptr = del( & h);
    printf("\n%d has left child?(y/n) : ", ptr -> val);

    scanf(" %c", & ch);
    if (ch == 'y' || ch == 'Y') {
      printf("\nEnter the value : ");
      scanf("%d", & v);
      temp = (tr * ) malloc(sizeof(tr));
      temp -> val = v;
      temp -> lch = temp -> rch = NULL;
      ptr -> lch = temp;
      enqueue( & h, temp);
    }
    printf("\n%d has right child?(y/n) : ", ptr -> val);

    scanf(" %c", & ch);
    if (ch == 'y' || ch == 'Y') {
      printf("\nEnter the value : ");
      scanf("%d", & v);
      temp = (tr * ) malloc(sizeof(tr));
      temp -> val = v;
      temp -> lch = temp -> rch = NULL;
      ptr -> rch = temp;
      enqueue( & h, temp);
    }
  }
}

void enqueue(qu ** h, tr * ptr) {
  qu * temp, * qptr;
  temp = (qu * ) malloc(sizeof(qu));
  temp -> node = ptr;
  temp -> next = NULL;
  if ( * h == NULL)
    *
    h = temp;
  else {
    qptr = * h;

    while (qptr -> next != NULL)
      qptr = qptr -> next;

    qptr -> next = temp;
  }
}

tr * del(qu ** h) {
  tr * ptr;
  ptr = ( * h) -> node;
  * h = ( * h) -> next;
  return ptr;
}

int isempty(qu * h) {
  if (h == NULL)
    return 1;
  else
    return 0;
}

void inorder(tr * h) {
  if (h != NULL) {
    inorder(h -> lch);
    printf("%d,", h -> val);
    inorder(h -> rch);
  }
}
void levelorder(tr * root) {
  qu * p = NULL;
  tr * ptr;
  enqueue( & p, root);
  while (!isempty(p)) {
    ptr = del( & p);
    printf("%d,", ptr -> val);
    if (ptr -> lch != NULL)
      enqueue( & p, ptr -> lch);
    if (ptr -> rch != NULL)
      enqueue( & p, ptr -> rch);
  }
}
				
			

Leave a Reply