Jump to content

BFS travel program

- - - - -

  • Please log in to reply
2 replies to this topic

#1
onus

onus

    Programmer

  • Members
  • PipPipPipPip
  • 115 posts
I am writing a program for BFS travel.

#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
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
What is this BFS function supposed to do?
sudo rm -rf /

#3
onus

onus

    Programmer

  • Members
  • PipPipPipPip
  • 115 posts
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