urgent!!! help plz 
Author Message
 urgent!!! help plz

Hi.
 I do not  why the leftchild and rightchild of the root are null.
why?

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

struct node_b
{
 int key;
 struct node_b *leftchild;
 struct node_b *rightchild;

Quote:
};

typedef struct node_b node;

node *create(int data)
{
 node *root;
 root=malloc(sizeof(node));
 root->key=data;
 root->leftchild=NULL;
 root->rightchild=NULL;
 return root;

Quote:
}

void insert(int data, node *b_node)
{
 if(b_node==NULL)
 {
  b_node=malloc(sizeof(node));
  b_node->key=data;
  b_node->leftchild=NULL;
  b_node->rightchild=NULL;
 }
 else
 {
  if(data<b_node->key)
  {
   insert(data, b_node->leftchild);
  }
  else
  {
   insert(data, b_node->rightchild);
  }
 }

Quote:
}

void print_node(int low, int high, node *b_node)
{
 if(b_node!=NULL)
 {
  print_node(low, high, b_node->leftchild);
  print_node(low, high, b_node->rightchild);
  print_node(low, high, b_node);
  if(b_node->key<=high&&b_node->key>=low)
  {
   printf(" %d", b_node->key);
  }
 }

Quote:
}

void freetree(node *b_node)
{
 if(b_node!=NULL)
 {
  freetree(b_node->leftchild);
  freetree(b_node->rightchild);
  free(b_node);
 }

Quote:
}

int main(int argc, char *argv[])
{
 int i=0;
 int indata;
 int numOperand=0;
 node *root_node;
 int low_key=0;
 int up_key=0;

 FILE *infile;
 char DataIn[6];
 strcpy(DataIn, argv[1]);
 infile=fopen(DataIn, "r");

 if(infile==NULL)
 {
  printf("Cannot open file!\n");
  exit(1);
 }

 fscanf(infile, "%d\n", &numOperand);
 for(i=1;i<=numOperand;i++)
 {
  fscanf(infile, "%d", &indata);
  if(i==1)
  {
   root_node=create(indata);
  }
  else
  {
   insert(indata, root_node);
  }
 }
 fscanf(infile, "\n%d", &low_key);
 fscanf(infile, "%d", &up_key);

 printf("The nodes within %d ", low_key);
 printf("to %d ", up_key);
 printf("are \n");
 print_node(low_key, up_key, root_node);

 fclose(infile);

 freetree(root_node);

 return 0;

Quote:
}



Tue, 17 Sep 2002 03:00:00 GMT  
 urgent!!! help plz

Quote:

> void insert(int data, node *b_node)
> {
>  if(b_node==NULL)
>  {
>   b_node=malloc(sizeof(node));

since you actually allocate b_node (and hence change its value), it must be
passed by reference:

void insert(int data, node **b_node)
{
 if(*b_node==NULL)
 {
  *m_node=malloc(sizeof(node));
 /* ... */

Quote:
}

btw please post shorter pieces of code from now on.

--
              /"\                              m i k e    b u r r e l l

               X        AGAINST HTML MAIL      http://mikpos.dyndns.org
              / \



Tue, 17 Sep 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. URGENT ! HELP NEWBIE plz - easy dialog question

2. (Urgent, plz help)Problem getting app pointer in VC1.52 (16bit)

3. Urgent Kindly answer ASAP and plz do post a reply to mtyagi0@lycos.com

4. Urgent Kindly answer ASAP and plz do post a reply to mtyagi0@lycos.com

5. URGENT URGENT dlopen() help needed...

6. URGENT URGENT PLEASE HELP ME ActiveX multiples classes

7. Help Me PLZ

8. Problem Printing plz help

9. help me plz with richeditctrl component

10. C++ .net projects PLZ HELP

11. read input from user?, plz help

12. Help Plz :) How to fire an .exe file?

 

 
Powered by phpBB® Forum Software