1

I have to put nodes of binary search tree of every level in a linked list. That is if the height of the tree is 'h' then 'h+1' linked lists would be created and then each linked list would have all the nodes of each level. For this I have thought of creating an array of linked list. But the nodes are not being inserted in the list I guess. The code is as follows:-

struct node{ 
    int data;
    struct node *left;
    struct node *right;
    };

struct linked_list
{

    int data;
    struct linked_list *next;
};

    linkedlistofbst(struct node *new,struct linked_list *n1[], int level)
    {
    //printf("%d ",new->data);
    if(new==NULL)
    {
        return;
    }

    if(n1[level]==NULL)
    {
        struct linked_list *a =(struct linked_list *)malloc(sizeof(struct linked_list));
        a->data=new->data;
        a->next=NULL;
        n1[level]=a;
        printf("%d ",a->data);
    }
    else
    {
        struct linked_list *b =(struct linked_list *)malloc(sizeof(struct     linked_list));
        while(n1[level]->next!=NULL)
        {
            n1[level]=n1[level]->next;
        }
        b->data=new->data;
        b->next=NULL;
        n1[level]=b;
    }
    linkedlistofbst(new->left,n1,level+1);
    linkedlistofbst(new->right,n1,level+1);
    }

    main()

{
    struct linked_list *l=(struct linked_list *)malloc((a+1)*sizeof(struct    linked_list));//'a' is the height of the tree
    linkedlistofbst(new,&l, 0);//new is the pointer to the root node of the tree.
}
Ravi Kuldeep
  • 233
  • 1
  • 10

1 Answers1

1

You are right there is problem with the second argument, so do the following

Make the following changes in the main:

For defining an array of linked list of size a+1 and initializing them to NULL

struct linked_list **l=(struct linked_list **)malloc((a+1)*sizeof(struct    linked_list*));
for(i=0;i<(a+1);++i)
    l[i]=NULL;

Then call the method as

linkedlistofbst(new,l, 0);

Therefore your method must look like

linkedlistofbst(struct node *new,struct linked_list **l, int level)

also make the following modification in else as:

else
    {   
        struct linked_list *ptr=n1[level];
        while(ptr->next!=NULL)
        {
            ptr=ptr->next;
        }
        ptr->next=(struct linked_list *)malloc(sizeof(struct linked_list));
        ptr->next->data=new->data;
        ptr->next->next=NULL;        
    }
Sumeet
  • 8,086
  • 3
  • 25
  • 45
  • Tried but still no help. In the else condition I always create a linked list struct 'b' and assign all the data to 'b' and then make n[level] point it to b. So this part is correct I guess. The problem is in the initial condition (i.e if condition). Please correct me if I am wrong. @Dante – Ravi Kuldeep Jun 27 '15 at 14:15
  • @RaviKuldeep did u use my exact code? what problem came? please be specific. – Sumeet Jun 27 '15 at 14:24
  • @RaviKuldeep I found that your array of linked list must be initialized to NULL, I have updated my answer. – Sumeet Jun 27 '15 at 14:36
  • @RaviKuldeep I think that there are two problems with your code, first that the condition n1[level]==NULL is not being true even when array is empty, so I edited my answer. Secondly for a particular level your linked list of that level is not being extended, meaning in the end the linked list will contain the last element of that level but not all. Feel free for any queries. – Sumeet Jun 27 '15 at 14:46
  • It is always giving a segmentation fault(core dumped). Tried with the updated code but the same problem persists. I commented the two lines where it calls the tree->right and tree->left. By doing this it does not crash but when I try to print `l[0]->data` it gives an error and and when I replace '->' with a '.', it prints 0. Now according to me the array is not being correctly made or I have not passed the second argument of the function correctly. – Ravi Kuldeep Jun 27 '15 at 14:58
  • @RaviKuldeep Let me check. – Sumeet Jun 27 '15 at 15:22
  • tried the above code. Its working fine. Thanks for the help. Can you please tell me what was wrong in the second argument and how did you resolve it. – Ravi Kuldeep Jun 27 '15 at 16:49
  • @RaviKuldeep The second arg which you were passing was not actually an array of linked lists, It was only a pointer to linked list. So i visualized it as a 2 d array. The reason for this is: the data type of element it stored was pointer to linked list. If you are storing pointers to something in C then you better declare it as 2 d array using two stars(**). – Sumeet Jun 27 '15 at 17:00
  • @RaviKuldeep For example char *A, defines a pointer to array, but if you allocate it space of more than 1 element it becomes 1 d array, where A points to first element of array, So a variable that stores A must be of type char **A, because it is being implemented as 2 d array. – Sumeet Jun 27 '15 at 17:00
  • got it. Thanks for explaining it . One last question. how do I access them? Make the method as `struct linked_list **` and return n1?? @Dante – Ravi Kuldeep Jun 27 '15 at 17:31
  • @RaviKuldeep what exactly u want to access? – Sumeet Jun 27 '15 at 17:32
  • I want to access all the elements of every linked formed. – Ravi Kuldeep Jun 27 '15 at 17:36