Hey
This is my first post on this site.
I want to insert an element into binary tree using pointer passing through functions.
In my program i have used three structure which are follows :-
Code:
struct tree{
int data1;
struct tree *leftptr;
struct tree *rightptr;
};
struct list{
struct tree **data;
struct list *node;
};
struct queue{
struct list *front;
struct list *rear;
};
void Insertintree(struct tree **root1,int data){
struct tree *new=malloc(sizeof(struct tree));
struct tree *cur=NULL;
struct tree *n2;
struct queue *q=malloc(sizeof(struct queue));
q->front=NULL;
q->rear=NULL;
if(!new&&!q){
printf("Memory Error");
return ;
}
new->data1=data;
new->leftptr=NULL;
new->rightptr=NULL;
if(*root1==NULL){
*root1=new;
return;
}
enqueue(q,root1);
while(q->front)
{
cur=dequeue(q);
if(!cur->leftptr||!cur->rightptr){
if(!cur->leftptr)
cur->leftptr=new;
else
cur->rightptr=new;
// printf("%d",new->data1);
// deletequeue(q);
break;
}
else
{
printf("In else while");
if(cur->leftptr){
n2=malloc(sizeof(struct tree));
n2=NULL;
n2=cur->leftptr;
enqueue(q,&n2);
// printf("%p",&(cur->leftptr));
}
if(cur->rightptr){
n2=malloc(sizeof(struct tree));
n2=NULL;
n2=cur->rightptr;
enqueue(q,&n2);
// printf("%p",&(cur->rightptr));
}
}
}
deletequeue(q);
return;
}
valgrind output is :-
==2814== Memcheck, a memory error detector
==2814== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==2814== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==2814== Command: ./a.out
==2814==
==2814== Invalid free() / delete / delete[] / realloc()
==2814== at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2814== by 0x4006FB: deletequeue (in /home/ajay/a.out)
==2814== by 0x4008D2: Insertintree (in /home/ajay/a.out)
==2814== by 0x400937: main (in /home/ajay/a.out)
==2814== Address 0x51fd560 is 0 bytes inside a block of size 16 free'd
==2814== at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2814== by 0x4006DE: deletequeue (in /home/ajay/a.out)
==2814== by 0x4008D2: Insertintree (in /home/ajay/a.out)
==2814== by 0x400937: main (in /home/ajay/a.out)
==2814==
In else while==2814==
==2814== HEAP SUMMARY:
==2814== in use at exit: 248 bytes in 12 blocks
==2814== total heap usage: 16 allocs, 5 frees, 312 bytes allocated
==2814==
==2814== 16 bytes in 1 blocks are definitely lost in loss record 1 of 12
==2814== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2814== by 0x400738: Insertintree (in /home/ajay/a.out)
==2814== by 0x400904: main (in /home/ajay/a.out)
==2814==
==2814== 16 bytes in 1 blocks are definitely lost in loss record 2 of 12
==2814== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2814== by 0x4005D6: enqueue (in /home/ajay/a.out)
==2814== by 0x4007C5: Insertintree (in /home/ajay/a.out)
==2814== by 0x400915: main (in /home/ajay/a.out)
==2814==
==2814== 16 bytes in 1 blocks are definitely lost in loss record 3 of 12
==2814== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2814== by 0x4005D6: enqueue (in /home/ajay/a.out)
==2814== by 0x4007C5: Insertintree (in /home/ajay/a.out)
==2814== by 0x400926: main (in /home/ajay/a.out)
==2814==
==2814== 16 bytes in 1 blocks are definitely lost in loss record 4 of 12
==2814== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2814== by 0x4005D6: enqueue (in /home/ajay/a.out)
==2814== by 0x4007C5: Insertintree (in /home/ajay/a.out)
==2814== by 0x400937: main (in /home/ajay/a.out)
==2814==
==2814== 16 bytes in 1 blocks are definitely lost in loss record 5 of 12
==2814== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2814== by 0x4005D6: enqueue (in /home/ajay/a.out)
==2814== by 0x400874: Insertintree (in /home/ajay/a.out)
==2814== by 0x400937: main (in /home/ajay/a.out)
==2814==
==2814== 24 bytes in 1 blocks are definitely lost in loss record 9 of 12
==2814== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2814== by 0x4008E7: main (in /home/ajay/a.out)
==2814==
==2814== 24 bytes in 1 blocks are definitely lost in loss record 10 of 12
==2814== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2814== by 0x400849: Insertintree (in /home/ajay/a.out)
==2814== by 0x400937: main (in /home/ajay/a.out)
==2814==
==2814== 24 bytes in 1 blocks are definitely lost in loss record 11 of 12
==2814== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2814== by 0x40088B: Insertintree (in /home/ajay/a.out)
==2814== by 0x400937: main (in /home/ajay/a.out)
==2814==
==2814== 96 (24 direct, 72 indirect) bytes in 1 blocks are definitely lost in loss record 12 of 12
==2814== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2814== by 0x400722: Insertintree (in /home/ajay/a.out)
==2814== by 0x400904: main (in /home/ajay/a.out)
==2814==
==2814== LEAK SUMMARY:
==2814== definitely lost: 176 bytes in 9 blocks
==2814== indirectly lost: 72 bytes in 3 blocks
==2814== possibly lost: 0 bytes in 0 blocks
==2814== still reachable: 0 bytes in 0 blocks
==2814== suppressed: 0 bytes in 0 blocks
==2814==
==2814== For counts of detected and suppressed errors, rerun with: -v
==2814== ERROR SUMMARY: 10 errors from 10 contexts (suppressed: 0 from 0)
Here i think problem is in allocating memory to n2 . why i do this because i want to store the address of address of left and right pointers of tree and extract that address to get the address of left and right pointers. Is my method correct ? Spend whole yesterday on this problem ? If anyone knows answer please reply.