简体   繁体   中英

Inorder traversal for binary search tree, shows some errors

This is the function which I am using for BST inorder Traversal

void inOrder(struct node* root)
{
    if(root==NULL)
    {
      return;
    }
    inOrder(root->left);
    printf("Key:%d,Pointer:%p\n",root->key,root);
    inOrder(root->right);
}

The structure for the same is

struct node{
    int key;
    struct node *left,*right;
};

When I run the function I get two 0 extra elements for the following inserts:

root=insert(root,7);
insert(root,4);
insert(root,10);
insert(root,3);
insert(root,5);
insert(root,1);
insert(root,2);
insert(root,0);
insert(root,8);
insert(root,14);
insert(root,6);
insert(root,9);
insert(root,16);
insert(root,12);
insert(root,15);
insert(root,17);
inOrder(root);printf("\n");

And the insert function is given below:

struct node *insert(struct node * root,int ele)
{
  struct node *temp=newNode(ele);
  struct node *tree=root;
  struct node *saver;

  if(root==NULL)
  {
    root=temp;
  }
  else
  {
    while(tree!=NULL)
    {
       saver=tree;      
        if(ele<tree->key)
          tree=tree->left;
        else
          tree=tree->right;
    }
    if(ele<saver->key)
    {
      saver->left=temp;
    }
    else
    {
      saver->right=temp;
    }
  }
  return saver;
}

The output shows two 0s apart from the main elements, which I added. I have recently learned the Binary-Search-Tree, and I tried to implement it, Can someone please help me rectify this error. Thank you in advance

in insert

if(root==NULL)
    {root=temp;
    }

must be

if(root==NULL)
{
  saver=temp;
}

because you return saver (not initialized in your version)

I suppose also in your main the code is not what you give but something like

struct node * root = NULL;

root=insert(root,7);
insert(root,4);
...

If I define newNode like that :

struct node * newNode(int ele)
{
  struct node * r = malloc(sizeof(struct node));

  r->key = ele;
  r->left = r->right = NULL;
  return r;
}

and I do the other changes :

pi@raspberrypi:/tmp $ gcc -g -pedantic -Wextra n.c
pi@raspberrypi:/tmp $ ./a.out
Key:0,Pointer:0xf04078
Key:1,Pointer:0xf04058
Key:2,Pointer:0xf04068
Key:3,Pointer:0xf04038
Key:4,Pointer:0xf04018
Key:5,Pointer:0xf04048
Key:6,Pointer:0xf040a8
Key:7,Pointer:0xf04008
Key:8,Pointer:0xf04088
Key:9,Pointer:0xf040b8
Key:10,Pointer:0xf04028
Key:12,Pointer:0xf040d8
Key:14,Pointer:0xf04098
Key:15,Pointer:0xf040e8
Key:16,Pointer:0xf040c8
Key:17,Pointer:0xf040f8

The full code is :

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

struct node{
    int key;
    struct node *left,*right;
};

void inOrder(struct node* root)
    {
    if(root==NULL)
        {return;}
    inOrder(root->left);
    printf("Key:%d,Pointer:%p\n",root->key,root);
    inOrder(root->right);
    }

struct node * newNode(int ele)
{
  struct node * r = malloc(sizeof(struct node));

  r->key = ele;
  r->left = r->right = NULL;
  return r;
}

struct node *insert(struct node * root,int ele)
{
  struct node *temp=newNode(ele);
  struct node *tree=root;
  struct node *saver;

  if(root==NULL)
  {
    saver=temp;
  }
  else
  {
    while(tree!=NULL)
    {
      saver=tree;        
      if(ele<tree->key)
        tree=tree->left;
      else
        tree=tree->right;
    }
    if(ele<saver->key)
    {
      saver->left=temp;
    }
    else
    {
      saver->right=temp;
    }
  }
  return saver;
}

int main()
{
  struct node * root = NULL;

  root=insert(root,7);
  insert(root,4);
  insert(root,10);
  insert(root,3);
  insert(root,5);
  insert(root,1);
  insert(root,2);
  insert(root,0);
  insert(root,8);
  insert(root,14);
  insert(root,6);
  insert(root,9);
  insert(root,16);
  insert(root,12);
  insert(root,15);
  insert(root,17);
  inOrder(root);printf("\n");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM