Linked complete binary tree & its creation | Program for linked implementation of complete binary tree ; For Queue Size ; A tree node ; A queue node ; A utility function to create a new tree node ; A utility function to create a new Queue ; Standard Queue Functions ; A utility function to check if a tree node has both left and right children ; Function to insert a new node in complete binary tree ; Create a new node for given data ; If the tree is empty , initialize the root with new node . ; get the front node of the queue . ; If the left child of this front node doesn t exist , set the left child as the new node ; If the right child of this front node doesn t exist , set the right child as the new node ; If the front node has both the left child and right child , Dequeue ( ) it . ; Enqueue ( ) the new node for later insertions ; Standard level order traversal to test above function ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define SIZE  50 NEW_LINE class node { public : int data ; node * right , * left ; } ; class Queue { public : int front , rear ; int size ; node * * array ; } ; node * newNode ( int data ) { node * temp = new node ( ) ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } Queue * createQueue ( int size ) { Queue * queue = new Queue ( ) ; queue -> front = queue -> rear = -1 ; queue -> size = size ; queue -> array = new node * [ queue -> size * sizeof ( node * ) ] ; int i ; for ( i = 0 ; i < size ; ++ i ) queue -> array [ i ] = NULL ; return queue ; } int isEmpty ( Queue * queue ) { return queue -> front == -1 ; } int isFull ( Queue * queue ) { return queue -> rear == queue -> size - 1 ; } int hasOnlyOneItem ( Queue * queue ) { return queue -> front == queue -> rear ; } void Enqueue ( node * root , Queue * queue ) { if ( isFull ( queue ) ) return ; queue -> array [ ++ queue -> rear ] = root ; if ( isEmpty ( queue ) ) ++ queue -> front ; } node * Dequeue ( Queue * queue ) { if ( isEmpty ( queue ) ) return NULL ; node * temp = queue -> array [ queue -> front ] ; if ( hasOnlyOneItem ( queue ) ) queue -> front = queue -> rear = -1 ; else ++ queue -> front ; return temp ; } node * getFront ( Queue * queue ) { return queue -> array [ queue -> front ] ; } int hasBothChild ( node * temp ) { return temp && temp -> left && temp -> right ; } void insert ( node * * root , int data , Queue * queue ) { node * temp = newNode ( data ) ; if ( ! * root ) * root = temp ; else { node * front = getFront ( queue ) ; if ( ! front -> left ) front -> left = temp ; else if ( ! front -> right ) front -> right = temp ; if ( hasBothChild ( front ) ) Dequeue ( queue ) ; } Enqueue ( temp , queue ) ; } void levelOrder ( node * root ) { Queue * queue = createQueue ( SIZE ) ; Enqueue ( root , queue ) ; while ( ! isEmpty ( queue ) ) { node * temp = Dequeue ( queue ) ; cout << temp -> data << " ▁ " ; if ( temp -> left ) Enqueue ( temp -> left , queue ) ; if ( temp -> right ) Enqueue ( temp -> right , queue ) ; } } int main ( ) { node * root = NULL ; Queue * queue = createQueue ( SIZE ) ; int i ; for ( i = 1 ; i <= 12 ; ++ i ) insert ( & root , i , queue ) ; levelOrder ( root ) ; return 0 ; }