#include<stdio.h>
#include<stdlib.h>
struct node {
struct node *left, *right;
int data, color;
} *root;
//root is used to point to the root node of tree
int check = 0;
typedef struct node tree;
struct que {
struct que *next;
struct node *ald; //ald stores the address of members of structure node
} *que_begin;
typedef struct que linklist;
tree *create_node(int);
void add_tree(int value, tree *);
void travel_tree(tree *);
void enque(tree *);
linklist *que_node(tree *);
int main()
{
int i, j,value;
tree *nv;
root = NULL;
que_begin = NULL;
value = 0;
while (1 == 1) {
printf("enter value \n");
scanf("%d", &value);
if (value == 1)
break;
if (root == NULL)
root = create_node(value);
else if (root != NULL) {
// nv = create_node(value);
add_tree(value, root);
}
}
travel_tree(root);
}
tree *create_node(int num)
{
tree *temp;
temp = (tree *) malloc(sizeof(tree));
temp->data = num;
temp->left = NULL;
temp->right = NULL;
return temp;
}
void add_tree(int v1, tree * node)
{
if (v1 <= node->data) {
if (node->left != NULL) {
add_tree(v1, node->left);
} else {
node->left = create_node(v1);
}
return;
}
if (v1 > node->data) {
if (node->right != NULL) {
add_tree(v1, node->right);
} else {
node->right = create_node(v1);
}
return;
}
}
void travel_tree(tree * temp)
{
// printf("\n inside travel_tree");
if (temp->left != NULL)
travel_tree(temp->left);
printf(" \n %d ", temp->data);
if (temp->right != NULL)
travel_tree(temp->right);
return;
}
linklist *que_node(tree * temp)
{
linklist *b;
b = (linklist *) malloc(sizeof(linklist));
b->next = NULL;
b->ald = temp;
return b;
}
void enque(tree *temp)
{
//to make a que of linklist
linklist *list;
list=que_begin;
if (que_begin == NULL) {
que_begin = que_node(temp);
list=que_begin;
}
else
{ while(list->next!=NULL)
{
list=list->next;
}
list->ald=temp;
}
}
The above code will create the tree and a function enque will que the nodes but I could not write the BFS function can any one help.
2 replies to this topic
#1
Posted 08 October 2010 - 01:33 AM
I am writing a program for BFS travel.
|
|
|
#2
Posted 09 October 2010 - 09:13 PM
#3
Posted 11 October 2010 - 08:43 AM
Sorry for the delayed reply BFS is supposed to call enque and deque and will be doing Breadth First Search on the tree that I made.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









