1

i was trying to do tree traversal.(per-order,in-order,and post-order) here is my code.

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


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


void Insert(struct node* root,int item)
{
    struct node* parent;
    struct node* NewNode = (struct node*)malloc(sizeof(struct node));
    NewNode->left = NULL;
    NewNode->data = item;
    NewNode->right = NULL;

    if (root == NULL)
        root = NewNode;
    else
    {
        parent = root;
        while (1)
        {
            if (parent->data>item)
            {
                if(parent->left == NULL)
                {
                    parent->left = NewNode;
                    return;
                }
                parent = parent->left;

            }
            if (parent->data<item)
            {
                if(parent->right == NULL)
                {
                    parent->right = NewNode;
                    return;
                }
                parent = parent->right;
            }
        }

    }
}




void pre(struct node *newNode)
{
    if(newNode!=NULL)
    {
        printf("%d ",newNode->data);
        pre(newNode->left);
        pre(newNode->right);
    }
}

void in(struct node *newNode)
{
    if(newNode!=NULL)
    {
        in(newNode->left);
        printf("%d ",newNode->data);
        in(newNode->right);
    }
}


void post(struct node *newNode)
{
    if(newNode!=NULL)
    {
        post(newNode->left);
        post(newNode->right);
        printf("%d ",newNode->data);
    }
}


int main(void)
{
    int num,i;

    printf("\nHow many Numbers you wanna Enter in tree:\t");
    scanf("%d",&num);
    int numArr[num];
    printf("\nEnter the numbers: \n");
    struct node* root = NULL;
    for (i=0;i<num;i++)
    {
        scanf("%d",&numArr[i]);
        Insert(root,numArr[i]);
    }

    printf("\nPre order traversal is:\n");
    pre(root);
    printf("\nIn order traversal is:\n");
    in(root);
    printf("\nPost order traversal is:\n");
    post(root);
}

i think i have problem with inserting the value because when i run the code the output is just empty. could anyone explain me where m i going wrong?

at insert function m taking the root node and the item to be inserted as argument.

then i'm creating a new node using malloc.

inserting data to the new node and left, right is null as left and right is currently not pointing to any nodes.

then checking if root is null or not. if it is null m assigning the new node to root node.

if root is not null. (i should not loose the root so i am copying root to parent and using that.)

m checking if the data is less than root or not. if it is less m going to the left of root if left of root is null m inserting the address of new node there if it is not null m just going left and left until it becomes null.

m doing the same thing(just going right) if data is greater than the value in root.

my explanation tells me m going on the right path. but my code tell me a different story.

Devjeet Mandal
  • 345
  • 1
  • 4
  • 23
  • 1
    Since the function argument itself can not be changed in the function, you need to pass a pointer if you want to change it (like `void Insert(struct node* root` --> `void Insert(struct node** root`. then call `Insert(&root,numArr[i]);` and `if (root == NULL) root = NewNode; else { parent = root;` --> `if (*root == NULL) *root = NewNode; else { parent = *root;`) (Or substitute return value (like `root = Insert(root, numArr[i]);`) – BLUEPIXY Aug 31 '17 at 22:05
  • pass root as pointer to pointer to node, `struct node ** p_root` , then in function dereference p_root; `*p_root = newNode;` – milevyo Aug 31 '17 at 22:06
  • 1
    You never update the root pointer in main so the tree is always empty. Redesign the interface to the insert code. Either take a pointer to pointer and update that way, or return the new root pointer (or the old one if it didn't change). – Jonathan Leffler Aug 31 '17 at 22:07
  • @BLUEPIXY and milevyo things are getting above my head can you elaborate – Devjeet Mandal Aug 31 '17 at 22:11
  • ok so if i create the root globally will that help? – Devjeet Mandal Aug 31 '17 at 22:14
  • @BLUEPIXY yeah i got it,i mean the correct output but not surely why it happned? can you explain that? – Devjeet Mandal Aug 31 '17 at 22:19
  • In C, its (copy of) value is passed as a function argument. It is because that variable itself is not passed on. – BLUEPIXY Aug 31 '17 at 22:22
  • few diagrams helped me to understand this. than you everyone – Devjeet Mandal Aug 31 '17 at 22:26
  • like [this](https://stackoverflow.com/a/39269792/971127) ? and Read [this](http://c-faq.com/~scs/cclass/int/sx8.html) – BLUEPIXY Aug 31 '17 at 22:40
  • @BLUEPIXY like what? – Devjeet Mandal Aug 31 '17 at 22:43
  • click link it.. – BLUEPIXY Aug 31 '17 at 22:44

1 Answers1

0

this is the correct answer thanks to everyone for help.

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


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


void Insert(struct node** root,int item)
{
    struct node* parent;
    struct node* NewNode = (struct node*)malloc(sizeof(struct node));
    NewNode->left = NULL;
    NewNode->data = item;
    NewNode->right = NULL;

    if (*root == NULL)
        *root = NewNode;
    else
    {
        parent = *root;
        while (1)
        {
            if (parent->data>item)
            {
                if(parent->left == NULL)
                {
                    parent->left = NewNode;
                    return;
                }
                parent = parent->left;

            }
            if (parent->data<item)
            {
                if(parent->right == NULL)
                {
                    parent->right = NewNode;
                    return;
                }
                parent = parent->right;
            }
        }

    }
}




void pre(struct node *newNode)
{
    if(newNode!=NULL)
    {
        printf("%d ",newNode->data);
        pre(newNode->left);
        pre(newNode->right);
    }
}

void in(struct node *newNode)
{
    if(newNode!=NULL)
    {
        in(newNode->left);
        printf("%d ",newNode->data);
        in(newNode->right);
    }
}


void post(struct node *newNode)
{
    if(newNode!=NULL)
    {
        post(newNode->left);
        post(newNode->right);
        printf("%d ",newNode->data);
    }
}


int main(void)
{
    int num,i;

    printf("\nHow many Numbers you wanna Enter in tree:\t");
    scanf("%d",&num);
    int numArr[num];
    printf("\nEnter the numbers: \n");
    struct node* root = NULL;
    for (i=0;i<num;i++)
    {
        scanf("%d",&numArr[i]);
        Insert(&root,numArr[i]);
    }

    printf("\nPre order traversal is:\n");
    pre(root);
    printf("\nIn order traversal is:\n");
    in(root);
    printf("\nPost order traversal is:\n");
    post(root);
}
Devjeet Mandal
  • 345
  • 1
  • 4
  • 23