ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am making a program to do a breadth first search.
The code which I am posting here just makes a que of the nodes visited in a binary tree in inorder fashion,so this implementation is not yet complete.
While developing I got a segmentation fault which I was not able to understand why I am getting so I am posting since the tree of same program (without BFS) is working.
Code:
#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 list {
struct list *next;
struct node *btree_node_ptr; //btree_node_ptr stores the address of members of structure node
} *que_start;
typedef struct list que;
tree *create_node(int);
void add_tree(int value, tree *);
void travel_tree(tree *);
void enque(tree *);
void travel_que(void);
void deque(tree *);
que *que_add_node(tree *);
que *tree_que;
int main()
{
int i, j,value;
tree *nv;
root = NULL;
que_start = NULL;
value = 0;
while (0) {
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);
printf("\n travelling in que \n");
travel_que();
}
tree *create_node(int num)
{
tree *temp;
temp = (tree *) malloc(sizeof(tree));
temp->data = num;
temp->left = NULL;
temp->right = NULL;
temp->color=2;
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);
enque(temp);
if (temp->right != NULL)
travel_tree(temp->right);
return;
}
que *que_add_node(tree * temp)
{
que *b;
b = (que *) malloc(sizeof(que));
b->next = NULL;
temp->color=1;
b->btree_node_ptr = temp;
return b;
}
void enque(tree *temp)
{
//to make a que of que
tree_que=que_start;
if (que_start == NULL) {
que_start = que_add_node(temp);
tree_que=que_start;
return;
}
else
{ while(tree_que->next!=NULL)
{
tree_que=tree_que->next;
}
tree_que->next=que_add_node(temp);
}
return;
}
void travel_que(void)
{
tree_que=que_start;
while (tree_que!=NULL)
{
printf("Data is %d color is %d\n",tree_que->btree_node_ptr->data,tree_que->btree_node_ptr->color);
tree_que=tree_que->next;
}
return;
}
void deque (tree *temp)
{
while (tree_que!=NULL)
{
tree_que=tree_que->next;
}
return;
}
The problem was that tree could not be travelled.
while(0)
any thing inside this statement was not executing.
I could not understand why.
I changed while(0) to while(1==1) and it worked,
int main()
{
int i, j,value;
tree *nv;
root = NULL;
que_start = NULL;
value = 0;
while (0) {
...
}
}
travel_tree(root);
printf("\n travelling in que \n");
travel_que();
}
Frankly, you've just given a very good reason why it would be a good idea to familiarize yourself with a debugger (if you haven't already).
ALSO:
1. As you know, this means "while (FALSE)":
Code:
while (0)
2. But, for the same reason, "while (1 == 1)" is duplicate redundant :
Code:
while (1 == 1) // Comparing any number with itself is always TRUE
// Equivalent:
// In C/C++, any non-zero number BY ITSELF also evaluates "true"
while (1)
// Equivalent:
// This is an idiom Kernighan and Ritchie use
for ( ;; )
3. This is also redundant (at least in this case):
Code:
// Poor
if (root == NULL)
root = create_node(value);
else if (root != NULL)
add_tree(value, root);
// Better
if (root == NULL)
root = create_node(value);
else
add_tree(value, root);
// Best
if (!root)
root = create_node(value);
else add_tree(value, root);
Small stuff: the compiler will often optimize away this kind of stuff. But the cleaner you write, the easier it is to read and maintain.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.