Solved

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

Posted on 2016-11-09
2
156 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This algorithm (in C#) will resize any image down to a given size while maintaining the original aspect ratio. The maximum width and max height are both optional but if neither are given, the original image is returned. This example is designed t…
Article by: Nadia
Suppose you use Uber application as a rider and you request a ride to go from one place to another. Your driver just arrived at the parking lot of your place. The only thing you know about the ride is the license plate number. How do you find your U…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

628 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