Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-11-09
2
Medium Priority
?
196 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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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 for-loops in the C programming language.

783 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