Saturday, May 14, 2011

Q. Write a program to count non leaf node in a tree?


/* Pass address of root in function argument
   count_non_leaft(root);
*/

count_nonleaf(struct bintree *t)
{
     int c1=0, c2=0;
     if(t!=NULL)
     {
           if(t->left!=NULL||t->right!=NULL)
              c1=1;
          c2 = return(count_nonleaf(t->left)+c1);
           return (count_nonleaf(t->right)+c2);
     }
     return 0;
}

Q. Write a program to count non leaf node in a tree?


/* Pass address of root in function argument
   count_non_leaft(root);
*/

count_nonleaf(struct bintree *t)
{
     int c1=0, c2=0;
     if(t!=NULL)
     {
           if(t->left!=NULL||t->right!=NULL)
              c1=1;
          c2 = return(count_nonleaf(t->left)+c1);
           return (count_nonleaf(t->right)+c2);
     }
     return 0;
}

Q. Write a program to count leaf node in a tree using recursive function.


/*Pass address of root node in function argument...
count_leaf(root);*/


count_leaf(struct bintree *t)
{
     int c1 = 0, c2 = 0;
     if(t!=NULL)
     {
           if(t->left == NULL && t->right == NULL)
                      c1 = 1;
           c2 = retun (count_leaf(t->left)+c1);
           return (count_leaf(t->right)+c2);
     }
     return 0;
}

Q. Write a program to count leaf node in a tree using recursive function.


/*Pass address of root node in function argument...
count_leaf(root);*/


count_leaf(struct bintree *t)
{
     int c1 = 0, c2 = 0;
     if(t!=NULL)
     {
           if(t->left == NULL && t->right == NULL)
                      c1 = 1;
           c2 = retun (count_leaf(t->left)+c1);
           return (count_leaf(t->right)+c2);
     }
     return 0;
}

Saturday, April 30, 2011

TREE in Data Structure with C

TREE

It is a non-linear data structure that may contain several contents. In a tree the nodes are connected to each other a parent and child relationship where each parent node may have multiple children nodes but has exactly a single parent node. The node which has no parent at all is called a root node. All the other nodes in the tree are descendent of root node.

Root node:- The only node which has no parent at all is called root node. But itself it is a parent of all other nodes in the tree.

Leaf node – The node which has no child node at all are called leaf node/terminal node or external node.

Non-leaf node – The node which has at least one child node are called non-leaf nod / non-terminal or internal node.

Height of tree – The maximum distance of a leaf node form the root node is called height of the tree.

Level of a node – The distance of a particular node from the root node is called its level. The root node is itself considered a level zero; its children are at level one and so on.

NB – Maximum number of children in a binary tree can be only three.

Order / Degree of a tree – The maximum number of children at a node in tree have, is called order of tree of degree of a tree. In general a tree n has maximum children per node and (n-1) keys per node.

Binary Tree –
A tree in which each node can have maximum of two children is called a binary tree. In other words a tree of order two is called binary tree. In which each node can have only two children and single info field. The two children of a node are referred to as left and right son respectively.

Strictly Binary Tree –
A binary tree in which each node has exactly two children or no children at all is called strictly binary tree.

Complete or full Binary tree –
A strictly binary tree is which all the leaf node at the same level is called complete or full binary tree.

Binary Tree traverse –
Traversing a binary tree means to visit each and every node in the tree only once in a predetermine sequence. There are following technique to traverse a binary tree –
1. Pre-order traverse
2. In-order traverse
3. Post-order traverse
4. Level by level traverse

1. Pre-order traverse – In this technique of traverse to a binary tree, the root node is visited before visiting its left and right children. The sequence of traverse may be define recursive in following three steps –
a. Visit the root
b. Traverse the left sub-tree in pre-order
c. Traverse the right sub-tree in pre-order

2. In-order traverse – In this sequence of traversing a binary tree, the root node is visited after visiting the left children but before right child. The step for recursive definition is given as below –
a. Traverse the left sub-tree in in-order
b. Visit the root
c. Traverse the right sub-tree in in-order.

3. Post-order traverse – In this sequence of traversing, the root node visited after traversing both its left and right sub-tree. The steps are given below –
a. Traverse the left sub-tree in post-order
b. Traverse the right sub-tree in post-order
c. Visit the root node.

4. Level by level traverse – In this scheme of traversing a binary tree, the root node is visited first then the nodes at level one and then the nodes at level two and so on.

TREE in Data Structure with C

TREE

It is a non-linear data structure that may contain several contents. In a tree the nodes are connected to each other a parent and child relationship where each parent node may have multiple children nodes but has exactly a single parent node. The node which has no parent at all is called a root node. All the other nodes in the tree are descendent of root node.

Root node:- The only node which has no parent at all is called root node. But itself it is a parent of all other nodes in the tree.

Leaf node – The node which has no child node at all are called leaf node/terminal node or external node.

Non-leaf node – The node which has at least one child node are called non-leaf nod / non-terminal or internal node.

Height of tree – The maximum distance of a leaf node form the root node is called height of the tree.

Level of a node – The distance of a particular node from the root node is called its level. The root node is itself considered a level zero; its children are at level one and so on.

NB – Maximum number of children in a binary tree can be only three.

Order / Degree of a tree – The maximum number of children at a node in tree have, is called order of tree of degree of a tree. In general a tree n has maximum children per node and (n-1) keys per node.

Binary Tree –
A tree in which each node can have maximum of two children is called a binary tree. In other words a tree of order two is called binary tree. In which each node can have only two children and single info field. The two children of a node are referred to as left and right son respectively.

Strictly Binary Tree –
A binary tree in which each node has exactly two children or no children at all is called strictly binary tree.

Complete or full Binary tree –
A strictly binary tree is which all the leaf node at the same level is called complete or full binary tree.

Binary Tree traverse –
Traversing a binary tree means to visit each and every node in the tree only once in a predetermine sequence. There are following technique to traverse a binary tree –
1. Pre-order traverse
2. In-order traverse
3. Post-order traverse
4. Level by level traverse

1. Pre-order traverse – In this technique of traverse to a binary tree, the root node is visited before visiting its left and right children. The sequence of traverse may be define recursive in following three steps –
a. Visit the root
b. Traverse the left sub-tree in pre-order
c. Traverse the right sub-tree in pre-order

2. In-order traverse – In this sequence of traversing a binary tree, the root node is visited after visiting the left children but before right child. The step for recursive definition is given as below –
a. Traverse the left sub-tree in in-order
b. Visit the root
c. Traverse the right sub-tree in in-order.

3. Post-order traverse – In this sequence of traversing, the root node visited after traversing both its left and right sub-tree. The steps are given below –
a. Traverse the left sub-tree in post-order
b. Traverse the right sub-tree in post-order
c. Visit the root node.

4. Level by level traverse – In this scheme of traversing a binary tree, the root node is visited first then the nodes at level one and then the nodes at level two and so on.

Saturday, April 23, 2011

Queue Circular

#include "conio.h"
#include "stdio.h"
#include "stdlib.h"

#define MAX 5

struct cqueue
{
int info[MAX];
int front, rear, count;
} cq;

int main(void)
{
int ch, n;
cq.front = 0;
cq.rear = -1;
cq.count = 0;
while(1)
{
clrscr();
f_disp(&cq);
printf("\n\n1. Enqueue \n2. Dequeue\n3. Display\n4. Exit.");
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter info : ");
scanf("%d",&n);
enqueue(&cq, n);
break;
case 2: n = dequeue(&cq);
if(n!=-1)
{
printf("Dequeued : %d",n);
getch();
}
else
{
printf("Queue underflow...");
getch();
break;
}
break;
case 3: disp(&cq);
break;
case 4: exit(0);
default: printf("Invalid choice. \nPlease enter 1-4");
}
}

getch();
return 0;
}

/* add node to queue list */
enqueue(struct cqueue *q, int n)
{
if(q->count == MAX)
{
printf("\nQueue overflow...");
getch();
return;
}
q->rear = (q->rear+1)%MAX;
q->info[q->rear] = n;
q->count++;
}

/* delete node from queue list */
dequeue(struct cqueue *q)
{
int n;
if(q->count==0)
{
printf("Queue underflow...");
getch();
return -1;
}

n = q->info[q->front];
q->front = (q->front+1)%MAX;
q->count--;
return n;
}


/* display items of queue */
disp(struct cqueue *q)
{
int i, n = q->count;
clrscr();
if(n == 0)
{
printf("No item...");
getch();
return;
}
else
{
printf("\n\n\tQueue are : \n");
for(i=q->front;n>0;n--)
{
printf("\t\t\t%d\n ", q->info[i]);
i = (i+1)%MAX;
}
}
printf("\n\nPress any key to return to main menu...");
getch();

}

/*display items on head of menu display. */
f_disp(struct cqueue *q)
{
int i, n = q->count;
if(n == 0)
{
printf("No item...");
return;
}
printf("Queue are : \n\t\t");
for(i=q->front;n>0;n--)
{
printf("%d, ", q->info[i]);
i = (i+1)%MAX;
}
}

Related Posts Plugin for WordPress, Blogger...

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites