Solved

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

Posted on 2016-11-09
2
138 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

751 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