Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
The greatest common divisor (gcd) of two positive integers is their largest common divisor. Let's consider two numbers 12 and 20. The divisors of 12 are 1, 2, 3, 4, 6, 12 The divisors of 20 are 1, 2, 4, 5, 10 20 The highest number among the c…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

911 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

18 Experts available now in Live!

Get 1:1 Help Now