Monday, March 18, 2013

C Program to create Tree using Menus


C Program to create Tree using Menus

#include<stdio.h>
#include<math.h>
#include<conio.h>
#define MAX 100
struct node
{
   struct node *lchild;
   char data;
   struct node *rchild;
};
struct node *s[MAX];

int top,top1,st[MAX];

void push1(int p)
{
   if (top1==MAX-1)
     printf("Stack is Full\n");
   else
   {
      top1++;
      st[top1]=p;
   }
}

int pop1()
{
   int p;
   if (top1<0)
     printf("Stack is Empty\n");
   else
   {
     p=st[top1];
     top1--;
   }
   return p;
}

void push(struct node *p)
{
   if (top==MAX-1)
     printf("Stack is Full\n");
   else
   {
      top++;
      s[top]=p;
   }
}

struct node * pop()
{
  struct node *p;
   if (top<0)
     printf("Stack is Empty\n");
   else
   {
     p=s[top];
     top--;
   }
   return p;
}
void displaytree(struct node *root)
{
   int x,y,h,k;
   struct node *p;
   x=40;y=5;top1=0;h=0;top=0;
   p=root;
   s[top]=NULL;
   while(p!=NULL)
   {
     gotoxy(x,y);
     printf("%c", p->data);
     if (p->rchild!=NULL)
     {
       push(p->rchild);
       k=x+ceil(40.0/exp((h+1)*log(2)));
       push1(k);
       k=y+3;
       push1(k);
       push1(h+1);
     }
     if (p->lchild!=NULL)
     {
       p=p->lchild;
       h++;
       x=x-ceil(40.0/exp((h)*log(2)));
       y=y+3;
       gotoxy(x,y);
     }
     else
     {
       p=pop();
       if (top1!=0)
       {
             h=pop1();
             y=pop1();
             x=pop1();
             gotoxy(x,y);
       }
     }
   }

}
struct node * getnode()
{
   struct node *p;
   p= (struct node *) malloc(sizeof(struct node));
   printf("Enter data into a new node\n");
   fflush(stdin);
   scanf("%c",&p->data);
   p->lchild=NULL;
   p->rchild=NULL;
   return p;
}
struct node * createtree()
{
   struct node *root,*p,*t;
   char ch=0;
   top=0;
   s[top]=NULL;
   root=getnode();
   p=root;
   while(p!=NULL)
   {
      printf("Is the right child is existing for %c :  ",p->data);
      fflush(stdin);
      ch=getchar();
      if ((ch=='Y') ||(ch=='y'))
      {
            t=getnode();
            p->rchild=t;
            push(t);
      }
      printf("Is the left child is existing for %c :  ",p->data);
      fflush(stdin);
      ch=getchar();
      if ((ch=='Y') || (ch=='y'))
      {
            t=getnode();
            p->lchild=t;
            p=t;
      }
      else
      {
            p=pop();
      }
   }
   return root;
}

void rpreorder(struct node *p)
{
   if (p!=NULL)
   {
      printf("%c ",p->data);
      rpreorder(p->lchild);
      rpreorder(p->rchild);
   }
}
void rinorder(struct node *p)
{
   if (p!=NULL)
   {
      rinorder(p->lchild);
      printf("%c ",p->data);
      rinorder(p->rchild);
   }
}
void rpostorder(struct node *p)
{
   if (p!=NULL)
   {
      rpostorder(p->lchild);
      rpostorder(p->rchild);
      printf("%c ",p->data);
   }
}

void ipreorder(struct node *root)
{
   struct node *p;
   top=0;
   s[top]=NULL;
   p=root;
   while(p!=NULL)
   {
      if(p->rchild!=NULL)
       push(p->rchild);
      printf("%c ", p->data);
      if (p->lchild!=NULL)
       p=p->lchild;
      else
       p=pop();
   }
}
void iinorder(struct node *root)
{
   struct node *p;
   top=0;
   s[top]=NULL;
   p=root;
   while(p->lchild!=NULL)
   {
     push(p);
     p=p->lchild;
   }
   while(p!=NULL)
   {
     printf("%c ", p->data);
     if (p->rchild!=NULL)
     {
       p=p->rchild;
       while(p->lchild!=NULL)
       {
             push(p);
             p=p->lchild;
       }
     }
     else
     {
       p=pop();
     }
    }
}
void ipostorder(struct node *root)
{
   struct node *p,*temp;
   top=0;
   s[top]=NULL;
   p=root;
   temp=NULL;
   while(p->lchild!=NULL)
   {
     push(p);
     p=p->lchild;
   }
   while(p!=NULL)
   {
     if ((p->rchild==NULL)||(p->rchild==temp))
     {
       printf("%c ", p->data);
       temp=p;
       p=pop();
     }
     else
     {
            push(p);
            p=p->rchild;
            while(p->lchild!=NULL)
            {
              push(p);
              p=p->lchild;
            }
     }
   }
}

void menudisplay(int n)
{
      int i,row=10,col=30;
      char opt[9][20]={"Create Tree","Display Tree","Recursive Preorder",
                               "Recursive Inorder","Recursive Postorder",
                               "Iterative Preorder","Iterative Inorder",
                               "Iterative Postorder","Exit"};
      clrscr();
      textattr(177);
      gotoxy(23,8);
      cprintf("TREES Demonstration\n");

      for (i=0;i<9;i++)
      {
            if (i==n)
            {
             textattr(66);
             gotoxy(col,row);
             cprintf("%s\n",opt[i]);
             row++;
            }
            else
            {
             textattr(7);
             gotoxy(col,row);
             cprintf("%s\n",opt[i]);
             row++;
            }
      }
}
int  selectoption()
{
      char ch=0;
      int curr=0;
      menudisplay(curr);

      while(ch!=13)
      {
            ch=getch();
            textattr(7);
            if (ch==27) return 27;
            if (ch==0) ch=getch();
            if (ch==72)
            {
              curr--;
              if (curr<0) { curr=8; }
              menudisplay(curr);
            }
            if (ch==80)
            {
              curr++;
              if (curr==9) { curr=0; }
              menudisplay(curr);
            }
      }
      return curr;
}

void main()
{
   int opt;
   struct node *root;
   clrscr();
   while (1)
   {
     opt=selectoption();
     if ((opt==27) ||( opt==8)) { break; }
     clrscr();
     switch(opt)
     {
            case 0:{ root= createtree();                  break;   }
            case 1:{ displaytree(root);                   break;   }
            case 2:{ displaytree(root);
                         gotoxy(30,21);
                         printf("Preorder Traversal : ");
                         rpreorder(root);
                         break;
                   }
            case 3:{ displaytree(root);
                         gotoxy(30,21);
                         printf("Inorder Traversal : ");
                         rinorder(root);
                         break;
                   }
            case 4:{ displaytree(root);
                         gotoxy(30,21);
                         printf("Postorder Traversal : ");
                         rpostorder(root);
                         break;
                   }
            case 5:{ displaytree(root);
                         gotoxy(30,21);
                         printf("Preorder Traversal : ");
                         ipreorder(root);
                         break;
                   }
            case 6:{ displaytree(root);
                         gotoxy(30,21);
                         printf("Inorder Traversal : ");
                         iinorder(root);
                         break;
                   }
            case 7:{ displaytree(root);
                         gotoxy(30,21);
                         printf("Postorder Traversal : ");
                         ipostorder(root);
                         break;
                   }
            case 8:{ exit(0);      }
     }
     gotoxy(30,22);
     printf("Press any Key........\n");
     getch();
   }
  textattr(177);
  gotoxy(20,20);
  cprintf(" Tree Demonstration is Over.   Press any key....");
  getch();
}

C Program to Create Tree, Display Tree, Iterative Preorder, Inorder and Postorder


C Program to Create Tree, Display Tree, Iterative Preorder, Inorder and Postorder

 #include<stdio.h>
#include<math.h>
#include<conio.h>
#define MAX 100
struct node
{
   struct node *lchild;
   char data;
   struct node *rchild;
};
struct node *s[MAX];

int top,top1,st[MAX];

void push1(int p)
{
   if (top1==MAX-1)
     printf("Stack is Full\n");
   else
   {
      top1++;
      st[top1]=p;
   }
}

int pop1()
{
   int p;
   if (top1<0)
     printf("Stack is Empty\n");
   else
   {
     p=st[top1];
     top1--;
   }
   return p;
}

void push(struct node *p)
{
   if (top==MAX-1)
     printf("Stack is Full\n");
   else
   {
      top++;
      s[top]=p;
   }
}
struct node * pop()
{
  struct node *p;
   if (top<0)
     printf("Stack is Empty\n");
   else
   {
     p=s[top];
     top--;
   }
   return p;
}


void displaytree(struct node *root)
{
   int x,y,h,k;
   struct node *p;
   x=40;y=5;top1=0;h=0;top=0;
   p=root;
   s[top]=NULL;
   while(p!=NULL)
   {
     gotoxy(x,y);
     printf("%c", p->data);
     if (p->rchild!=NULL)
     {
       push(p->rchild);
       k=x+ceil(40.0/exp((h+1)*log(2)));
       push1(k);
       k=y+3;
       push1(k);
       push1(h+1);
     }
     if (p->lchild!=NULL)
     {
       p=p->lchild;
       h++;
       x=x-ceil(40.0/exp((h)*log(2)));
       y=y+3;
       gotoxy(x,y);
     }
     else
     {
       p=pop();
       if (top1!=0)
       {
             h=pop1();
             y=pop1();
             x=pop1();
             gotoxy(x,y);
       }
     }
   }

}


struct node * getnode()
{
   struct node *p;
   p= (struct node *) malloc(sizeof(struct node));
   printf("Enter data into a new node\n");
   fflush(stdin);
   scanf("%c",&p->data);
   p->lchild=NULL;
   p->rchild=NULL;
   return p;
}


struct node * createtree()
{
   struct node *root,*p,*t;
   char ch=0;
   top=0;
   s[top]=NULL;
   root=getnode();
   p=root;
   while(p!=NULL)
   {
      printf("Is the right child is existing for %c :  ",p->data);
      fflush(stdin);
      ch=getchar();
      if ((ch=='Y') ||(ch=='y'))
      {
            t=getnode();
            p->rchild=t;
            push(t);
      }
      printf("Is the left child is existing for %c :  ",p->data);
      fflush(stdin);
      ch=getchar();
      if ((ch=='Y') || (ch=='y'))
      {
            t=getnode();
            p->lchild=t;
            p=t;
      }
      else
      {
            p=pop();
      }
   }
   return root;
}

void ipreorder(struct node *root)
{
   struct node *p;
   top=0;
   s[top]=NULL;
   p=root;
   while(p!=NULL)
   {
      if(p->rchild!=NULL)
       push(p->rchild);
      printf("%c ", p->data);
      if (p->lchild!=NULL)
       p=p->lchild;
      else
       p=pop();
   }
}


void iinorder(struct node *root)
{
   struct node *p;
   top=0;
   s[top]=NULL;
   p=root;
   while(p->lchild!=NULL)
   {
     push(p);
     p=p->lchild;
   }
   while(p!=NULL)
   {
     printf("%c ", p->data);
     if (p->rchild!=NULL)
     {
       p=p->rchild;
       while(p->lchild!=NULL)
       {
             push(p);
             p=p->lchild;
       }
     }
     else
     {
       p=pop();
     }
    }
}


void ipostorder(struct node *root)
{
   struct node *p,*temp;
   top=0;
   s[top]=NULL;
   p=root;
   temp=NULL;
   while(p->lchild!=NULL)
   {
     push(p);
     p=p->lchild;
   }
   while(p!=NULL)
   {
     if ((p->rchild==NULL)||(p->rchild==temp))
     {
       printf("%c ", p->data);
       temp=p;
       p=pop();
     }
     else
     {
            push(p);
            p=p->rchild;
            while(p->lchild!=NULL)
            {
              push(p);
              p=p->lchild;
            }
     }
   }
}

  


void main()
{
   int opt;
   struct node *root;
   clrscr();
   while (1)
   {
     clrscr();
     printf("Demonstration of Tree Traversal using Iterative method\n");
     printf("1. Create Tree\n");
     printf("2. Display Tree\n");
     printf("3. Preorder\n");
     printf("4. Inorder\n");
     printf("5. Postorder\n");
     printf("6. Exit\n");
     printf("Enter Choice: ");
     scanf("%d",&opt);
     switch(opt)
     {
            case 1:{ root= createtree();    break;   }
            case 2:{ clrscr(); displaytree(root);     break;   }
            case 3:{ clrscr(); displaytree(root);
                         gotoxy(30,21);
                         printf("Preorder Traversal : ");
                         ipreorder(root);
                         break;
                   }
            case 4:{ clrscr(); displaytree(root);
                         gotoxy(30,21);
                         printf("Inorder Traversal : ");
                         iinorder(root);
                         break;
                   }
            case 5:{ clrscr(); displaytree(root);
                         gotoxy(30,21);
                         printf("Postorder Traversal : ");
                         ipostorder(root);
                         break;
                   }
            case 6:{ exit(0);      }
     }

     gotoxy(30,22);
     printf("Press any Key........\n");
     getch();
   }
}

C Program to Create Tree, Display Tree, Recursive Preorder, Inorder and Postorder


C Program to Create Tree, Display Tree, Recursive Preorder, Inorder and Postorder
    
 #include<stdio.h>
#include<math.h>
#include<conio.h>
#define MAX 100
struct node
{
   struct node *lchild;
   char data;
   struct node *rchild;
};
struct node *s[MAX];

int top,top1,st[MAX];

void push1(int p)
{
   if (top1==MAX-1)
     printf("Stack is Full\n");
   else
   {
      top1++;
      st[top1]=p;
   }
}

int pop1()
{
   int p;
   if (top1<0)
     printf("Stack is Empty\n");
   else
   {
     p=st[top1];
     top1--;
   }
   return p;
}

void push(struct node *p)
{
   if (top==MAX-1)
     printf("Stack is Full\n");
   else
   {
      top++;
      s[top]=p;
   }
}
struct node * pop()
{
  struct node *p;
   if (top<0)
     printf("Stack is Empty\n");
   else
   {
     p=s[top];
     top--;
   }
   return p;
}

void displaytree(struct node *root)
{
   int x,y,h,k;
   struct node *p;
   x=40;y=5;top1=0;h=0;top=0;
   p=root;
   s[top]=NULL;
   while(p!=NULL)
   {
     gotoxy(x,y);
     printf("%c", p->data);
     if (p->rchild!=NULL)
     {
       push(p->rchild);
       k=x+ceil(40.0/exp((h+1)*log(2)));
       push1(k);
       k=y+3;
       push1(k);
       push1(h+1);
     }
     if (p->lchild!=NULL)
     {
       p=p->lchild;
       h++;
       x=x-ceil(40.0/exp((h)*log(2)));
       y=y+3;
       gotoxy(x,y);
     }
     else
     {
       p=pop();
       if (top1!=0)
       {
             h=pop1();
             y=pop1();
             x=pop1();
             gotoxy(x,y);
       }
     }
   }

}

struct node * getnode()
{
   struct node *p;
   p= (struct node *) malloc(sizeof(struct node));
   printf("Enter data into a new node\n");
   fflush(stdin);
   scanf("%c",&p->data);
   p->lchild=NULL;
   p->rchild=NULL;
   return p;
}


struct node * createtree()
{
   struct node *root,*p,*t;
   char ch=0;
   top=0;
   s[top]=NULL;
   root=getnode();
   p=root;
   while(p!=NULL)
   {
      printf("Is the right child is existing for %c :  ",p->data);
      fflush(stdin);
      ch=getchar();
      if ((ch=='Y') ||(ch=='y'))
      {
            t=getnode();
            p->rchild=t;
            push(t);
      }
      printf("Is the left child is existing for %c :  ",p->data);
      fflush(stdin);
      ch=getchar();
      if ((ch=='Y') || (ch=='y'))
      {
            t=getnode();
            p->lchild=t;
            p=t;
      }
      else
      {
            p=pop();
      }
   }
   return root;
}


void rpreorder(struct node *p)
{
   if (p!=NULL)
   {
      printf("%c ",p->data);
      rpreorder(p->lchild);
      rpreorder(p->rchild);
   }
}


void rinorder(struct node *p)
{
   if (p!=NULL)
   {
      rinorder(p->lchild);
      printf("%c ",p->data);
      rinorder(p->rchild);
   }
}


void rpostorder(struct node *p)
{
   if (p!=NULL)
   {
      rpostorder(p->lchild);
      rpostorder(p->rchild);
      printf("%c ",p->data);
   }
}



void main()
{
   int opt;
   struct node *root;
   clrscr();
   while (1)
   {
     clrscr();
     printf("Demonstration of Tree Traversal using Recursive Method\n");
     printf("1. Create Tree\n");
     printf("2. Display Tree\n");
     printf("3. Preorder\n");
     printf("4. Inorder\n");
     printf("5. Postorder\n");
     printf("6. Exit\n");
     printf("Enter Choice: ");
     scanf("%d",&opt);
     switch(opt)
     {
            case 1:{ root= createtree();    break;   }
            case 2:{ clrscr(); displaytree(root);     break;   }
            case 3:{ clrscr(); displaytree(root);
                         gotoxy(30,21);
                         printf("Preorder Traversal : ");
                         rpreorder(root);
                         break;
                   }
            case 4:{ clrscr(); displaytree(root);
                         gotoxy(30,21);
                         printf("Inorder Traversal : ");
                         rinorder(root);
                         break;
                   }
            case 5:{ clrscr(); displaytree(root);
                         gotoxy(30,21);
                         printf("Postorder Traversal : ");
                         rpostorder(root);
                         break;
                   }
            case 6:{ exit(0);      }
     }

     gotoxy(30,22);
     printf("Press any Key........\n");
     getch();
   }
}

Recursive Procedure for Tree Traversals


Type Declaration of Tree Node

struct node
{
   struct treenode *lchild;
   char data;
   struct treenode *rchild;
};

Procedure for Recursive Preorder

void rpreorder(struct node *p)
{
   if (p!=NULL)
   {
      printf("%c ",p->data);
      rpreorder(p->lchild);
      rpreorder(p->rchild);
   }
}

Procedure for Recursive Inorder

void rinorder(struct treenode *p)
{
   if (p!=NULL)
   {
      rinorder(p->lchild);
      printf("%c ",p->data);
      rinorder(p->rchild);
   }
}

Procedure for Recursive Postorder

void rpostorder(struct node *p)
{
   if (p!=NULL)
   {
      rpostorder(p->lchild);
      rpostorder(p->rchild);
      printf("%c ",p->data);
   }
}

Tree Traversal


Binary Tree Traversal
   A binary tree can be traversed in three ways.

            1.  Preorder Traversal (root, left, right)
            2.  Inorder Traversal (left, root, right)
            3.  Postorder Traversal (left, right root)

Preorder Traversal
          Here the traversal is done recursively in the following Manner
1.      Process Root Node
2.      Traverse left subtree in Preorder
3.      Traverse right subtree in preorder

Inorder Traversal
          Here the traversal is done recursively in the following Manner
1.      Traverse left subtree in Inorder
2.      Process Root Node
3.      Traverse right subtree in Inorder
Postorder Traversal
          Here the traversal is done recursively in the following Manner
1.      Traverse left subtree in Postorder
2.      Traverse right subtree in Postorder
3.      Process Root Node

Ex1:
Preorder Traversal:  Formula (Root, Left, Right)
            + * / A ** B C D + E F

Inorder Traversal: Formula (Left, Root, Right)
            A / B **  C * D + E + F

Postorder Traversal: Formula(Left, Right, Root)
            A B C ** / D * E F + +


Ex2:


Preorder Traversal:  Formula (Root, Left, Right)
            A B D K L M E H C F I N G

Inorder Traversal: Formula (Left, Root, Right)
            K L M D B H E A F  N I C G

Postorder Traversal: Formula(Left, Right, Root)
            M L K D H E B N I F G C A



Thursday, March 14, 2013

Representation of Binary Tree


Representation of Binary Tree
          Binary tree can be stored in memories in three ways

  1. Sequential Storage representation
  2. Linked Storage representation
  3. Thread storeage representation
1.  Sequential Storage Representation
         Here we represent thre trees using arrays. Consider the binary tree shown below:

The Sequential Storage representation of above tree is

The Storage leads to a large amount of memory wastage.  Sequential representation is good only for both complete binary tree and full binary trees.  Consider a full binary tree or complete binary tree.


 Sequential Representation


Insertion and Deletion of nodes from sequential representation of trees is tedious.


Trees


Trees
            A tree is a finite set of nodes such that
            1.  there is a specially designated node called root node.
            2.  the remaining nodes are partitioned into n>=0 disjoint sets T1, T2, T3 ---Tn
                 where each of these sets is a tree.  T1, T2, T3 ---Tn are called sub trees.


            The above tree has 12 nodes.  A node is nothing but information along with its branches.  The root node of the above tree is A.
Ex:


Degree of a Node:
             The no. of sub-trees of a node is called degree of node.  In otherwords, the no. of children of a node is called degree.  In the above figure, degree of A is 3,  Degree of B is 2,  Degree of H is 1 and degree of K is 0.

Terminal Nodes and Non-Terminal Nodes :
            If the degree of node is zero, then it is called terminal node or leaf node.  if the degree of node is not equal to zero then it is called non-terminal node.  In the above figure, terminal nodes are J, F, L, K.  The remaining nodes are non-terminal nodes.

Parent and Children
            If there is a branch(edge)from U to V then U is called parent of V and V is called child of U.  In the above tree, A’s children are B,C,D.  B’s childrens are E, F.  I child is j etc., Parent of G is C, parent of K is G etc.,

Siblings
            Children of the same parent are said to be siblings or brothers or sisters.  In the above tree B, C, D are siblings, E, F are siblings but G and H are cusions but not siblings.


Level of a Node
            Level of a node is defined as distance of a node from root node +1.  In the above tree, the node at level one is A, nodes at level-2 are B,C,D, nodes at level B are E,F,H,G.

Height of the tree
            Height of the tree is defined as largest level number in the tree.  The height of above tree is 5.  Since the largest level number is 5, where node J is present.

Degree of a Tree
            It is defined as maximum degree of the nodes in the tree.  The degree of the above tree is 3.  since maximum degree is 3 for node A.

Binary tree
            A Binary tree is a tree, where each should have maximum of two children i.e., degree of each node is less than or equal to 2.


A binary tree of zero node is said to be empty tree.

Complete Binary Tree
            A binary tree with n – nodes is said to be complete if and only if the no. of nodes ranging from 1 to n.  if the no. of nodes i, then left child no. is 2i and right child number is 2i+1.

Ex

Invalid Node Example

 Full Binary Tree
            A Binary tree is said to be full binary tree if each level contain 2i-1 nodes where i is level no. i.e., level 1 should contain 20 nodes, level 2 contains 21 nodes, level 3 contain 22 nodes and so on.

 Invalid Example

Every full binary tree is a complete binary tree but the reverse is not true.