TrustTyler
TrustTyler

Reputation: 55

Binary Tree implementation on C

I am trying to implement tree in C but the thing is whenever i try to traverse it, it only shows the first three nodes of the tree and the rest are lost. like, if i enter 100, 200, 300, 400, 500, 600, 700 then only 100 ,200, 300 will be in the output. I think the problem is with insert function but i just can't figure it out.

#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *prev;
    struct node *next;
};
typedef struct node list;
list *head, *tail, *current, *newn;
void inorder(struct node *t)
{
    if(t != NULL)
    {

        inorder(t->prev);
        printf("%d->",t->data);
        inorder(t->next);
    }
}

struct node * insert(int key, struct node *t)
{
    if(t == NULL)
    {
        t = (list*)malloc(sizeof(list));
        t->data = key;
        t->prev = NULL;
        t->next = NULL;
    }
    else if(t->prev == NULL)
    {
        t->prev = insert(key,t->prev);
    }
    else if(t->next == NULL)
    {
        t->next = insert(key,t->next);
    }
    return(t);
}
int main()
{
    int x=1, y, z=1;
    current = (list*)malloc(sizeof(list));
    printf("Enter data:");
    scanf("%d",&current->data);
    current->next = NULL;
    current->prev = NULL;
    head = current;
    while(z == 1)
    {
        printf("Enter data:");
        scanf("%d",&y);
        current = insert(y,current);
        printf("want to insert more:");
        scanf("%d",&z);  
    }
    printf("\nInorder Traversal:");
    newn = head;
    inorder(newn);

}

Upvotes: 2

Views: 2444

Answers (1)

BLUEPIXY
BLUEPIXY

Reputation: 40155

only 100 ,200, 300 will be in the output.

at Insert function

if(t == NULL)
{
    ...
}
else if(t->prev == NULL)
{
    ...
}
else if(t->next == NULL)
{
    ...
}
return(t);

Because it is When t, t->prev and t->next are not all NULL Nothing (that is, inserting) is done.

When adding conditions and recursive calls like

else if(t->prev->prev == NULL)
{
    t->prev->prev = insert(key, t->prev->prev);
}

Insertion of the node is done, but since growth becomes like depth-first search, the growth of the tree becomes biased.
So, as an approach you need to search for the next insertion point like breadth first search.
I think there are some methods, As a method I propose, it is a way to keep it as a pool when creating a NULL node rather than searching.
A concrete implementation using a queue as a node pool is as follows(Please note that many checks are omitted And using global variables).

#include <stdio.h>
#include <stdlib.h>

struct node{
    int data;
    struct node *prev;
    struct node *next;
};
typedef struct node list;

void inorder(struct node *t){
    if(t != NULL){
        inorder(t->prev);
        printf("%d->",t->data);
        inorder(t->next);
    }
}
//node of queue 
typedef struct null_node {
    list **nodepp;
    struct null_node *next;
} node_pool;
//queue 
typedef struct queue {
    node_pool *head;
    node_pool *tail;
} queue;
//enqueue 
void push(queue *q, list **nodepp){
    node_pool *np = malloc(sizeof(*np));
    np->nodepp = nodepp;
    np->next = NULL;
    if(q->head == NULL){
        q->tail = q->head = np;
    } else {
        q->tail = q->tail->next = np;
    }
}
//dequeue 
list **pop(queue *q){
    node_pool *head = q->head;
    if(head == NULL)
        return NULL;

    q->head = q->head->next;
    if(q->head == NULL)
        q->tail = NULL;
    list **nodepp = head->nodepp;
    free(head);
    return nodepp;
}

void clear_queue(queue *q){
    while(pop(q));
}

list *Head;
queue Q;

struct node *insert(int key, struct node *root){
    list *t = malloc(sizeof(*t));
    t->data = key;
    t->next = t->prev = NULL;
    push(&Q, &t->prev);//enqueue a NULL node
    push(&Q, &t->next);

    if(root == NULL){
        return t;
    }
    list **null_nodep = pop(&Q);//dequeue the node
    *null_nodep = t;//Replace with new node
    return root;
}
int main(void){
    int /*x=1, unused x*/ y, z=1;

    Head = NULL;
    while(z == 1){
        printf("Enter data:");
        scanf("%d",&y);
        Head = insert(y, Head);
        printf("want to insert more:");
        scanf("%d",&z);  
    }
    printf("\nInorder Traversal:");
    inorder(Head);
    clear_queue(&Q);//Discard queued nodes
}

Upvotes: 2

Related Questions