Jump to content

my program for creating a binary tree

- - - - -

  • Please log in to reply
No replies to this topic

#1
onus

onus

    Programmer

  • Members
  • PipPipPipPip
  • 115 posts
I made a program to have a binary tree and then
I took a pen and paper to understand the fault in my program
my program is

#include<stdio.h>

#include<stdlib.h>

struct node {

    struct node *left, *right;

    int data, color;

} *root;

int check = 0;

typedef struct node tree;


tree * create_node(int);

void add_tree(int value, tree *);

void travel_tree(tree *);

int main()

{

    int i, j, value;

    tree *nv;

    printf("enter value \n");

    scanf("%d", &value);

//    printf("\n check is %d",check);

    nv = create_node(value);

    add_tree(value, root);

    travel_tree(root);

}


tree * create_node(int num)

{

    tree *temp;

    printf("\n reached create_node\n");

    if (check == 0) {

        root = (tree *) malloc(sizeof(tree));

        root->data = num;

        root->left = NULL;

        root->right = NULL;

        temp = root;

        check++;

        printf("\nreached check==0 in create_node");

        return temp;

    }

    if (check != 0) {


        temp = (tree *) malloc(sizeof(tree));

        temp->data = num;

        temp->left = NULL;

        temp->right = NULL;

        return temp;


    }

    printf(" coming in \n");

//  check++;

    //return temp;

}


void add_tree(int v1, tree * node)

{

    tree *ki;

    ki = node;

    if (check == 0) {

        root = create_node(v1);

        return;        //check ==0 makes sure if the tree is add root node to it

    }

    if ((v1 < node->data) && (node->left)) {

        add_tree(v1, node->left);

    } else if ((v1 < ki->data) && (node->left == NULL)) {

        //node left is to be added at last

        node->left=create_node(v1);

        return;

    }

    if ((v1 > node->data) && (node->right)) {

        add_tree(v1, node->right);

        return;

    }

    else if ((v1 > node->data) && (node->right == NULL)) {

        //right node is to be added

        node->right = create_node(v1);

        return;

    }


}


void travel_tree(tree * temp)

{

    if (temp->left) {

        travel_tree(temp->left);

        printf(" %d", temp->data);

        return;

    }

    if (temp) {

        travel_tree(temp);

        printf(" %d", temp->data);

        return;

    }

    if (temp->right) {

        travel_tree(temp->right);

        printf(" %d", temp->data);

        return;

    }

}

The above program is giving me segmentation fault.

When I do a

./a.out following happens

enter value

46

 reached create_node


Segmentation fault
The printf statement inside the if(check==0) block in function create_node does not get executed.
So probably the first node itself is not getting created.
Can some one point me to error as why is that happening.

Edited by onus, 27 September 2010 - 04:09 AM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users