Solved

Segmentation fault (core dumped) error - C program for Binary Search Tree : Level Order Traversal

Posted on 2016-11-09
2
32 Views
Last Modified: 2016-11-09
I have been trying to execute this code but I keep getting segmentation fault. I'm guessing there is some pointer mistake but I'm not able find it. Please help!

Link of my code: http://codepad.org/vw9km9cy
0
Comment
Question by:Abhishek Thanki
2 Comments
 
LVL 40

Accepted Solution

by:
evilrix earned 500 total points
ID: 41881505
Well, there were a few issues with your code that I've now fixed. I don't see it crash when I run it. Can you explain what steps are required to make it crash?

See my comments in the code

#include<stdio.h>
#include<stdlib.h>

//typedef struct
typedef struct Node
{
	int data;
	struct Node *right, *left;
}Node;


typedef struct
{
    int front, rear, size;
    unsigned capacity;
    Node* arr[100];
}Queue;

Queue* createQ()
{
    Queue* q = (Queue*) malloc(sizeof(Queue));
    q->capacity = 100;
    q->front = q->size = 0; 
    q->rear = q->capacity - 1;
    return q;
}

int isEmpty(Queue* q)
{  
	return (q->size == 0); 
}
int isFull(Queue* q)
{  
	return (q->size == q->capacity);  
}
void enqueue(Queue* q,Node* item)
{
    if (isFull(q))
        return;
    q->rear = (q->rear + 1)%q->capacity;
    q->arr[q->rear] = item;
    q->size = q->size + 1;
 }
 

Node* dequeue(Queue* q)
{
    if (isEmpty(q))
        return NULL;
    Node* item = q->arr[q->front];
    q->front = (q->front + 1)%q->capacity;
    q->size = q->size - 1;
    return item;
}

Node* create(Node* root, int data)
{
	if(root==NULL)
	{
		root = (Node*)malloc(sizeof(Node));
		root->data = data;
		root->left = root->right = NULL;
		return root;
	}
	else
	{
		if(data>root->data)
		{
			root->right = create(root->right,data);
		}
		else
		{
			root->left = create(root->left,data);
		}
		return root;
	}
}
void levelorder(Node* root)
{
	if(root==NULL) return;
	else
	{
		Queue* q = createQ();
		enqueue(q,root);
		while(!isEmpty(q))
		{
			Node* temp = dequeue(q);
			printf("%d ",temp->data);
			if(temp->left)
				enqueue(q,temp->left);
			else if(temp->right) //;  <<---- rogue semicolon?
				enqueue(q,temp->right);
		}
	}
}

//void main()
int main() // should always return int as it's required by the standard
{
	int data;
	Node *root = NULL;
	for(;;)
	{
		printf("\nEnter the elements (-1 to stop) : ");
		scanf("%d",&data);
		if(data==-1) break;
		root = create(root,data);
	}
	printf("\nPrinting the elemtents in level order : ");
	levelorder(root);

  return 0; // needed
}

Open in new window

1
 

Author Closing Comment

by:Abhishek Thanki
ID: 41881510
Thank you so much! It works fine now!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Prime numbers are natural numbers greater than 1 that have only two divisors (the number itself and 1). By “divisible” we mean dividend % divisor = 0 (% indicates MODULAR. It gives the reminder of a division operation). We’ll follow multiple approac…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now