Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

c++ programming (tree structure) problem?

Hi,

I have a problem with searching in a tree structure data in C++. My code is below. Every node in the tree has 3 children. My 'search' function is recursive and I want to specify that is there any node in the tree that has my 'item' or not. But 'search' does not work.

class tree{

char* key;

tree* left;

tree* mid;

tree* right;

public:

bool search(tree *temp, char *item) //temp is root of tree

{

if (temp == NULL)

return false;

if (item == temp->key)

return true;

if(search( temp->left,item )) return true;

else if(search( temp->mid,item )) return true;

else if(search( temp->right,item )) return true;

else return false;

}

}

2 Answers

Relevance
  • Paul
    Lv 7
    1 decade ago
    Favorite Answer

    You can't use == to test char * strings in C or C++. You either need to use proper C++ strings (which *do* support the == operator) or if you have to use C strings for some reason then use the C string library function strcmp(), i.e. change:

    ....if (item == temp->key)

    to:

    ....if (strcmp(item, temp->key) == 0)

    (and don't forget to #include <cstring>).

  • 5 years ago

    a million) c ought to truly be declated as a char, at a minimum of casted into it:           char c; 2) input_head grow to be initialized exterior of your important function (evaluate putting it interior the important function or on the comparable line):           struct record *enter, *input_head = NULL; that ought to restore your issues =) $./q enter in an expression finding out a million, 2, 3! !3 ,2 ,a million gnitset struct record *enter, *input_head = NULL; int important(void){      char c;      int i = 0;      printf("enter in an expressionn"); ... reset of your code... complete code (no longer plenty replaced like all different solutions reported, merely the two very minor errors) #contain <stdio.h> #contain <stdlib.h> struct record {      char archives;      struct record *next; }; struct OR_list {      struct record *record ;      struct OR_list *OR_list; }; struct n_tree {      char archives;      struct OR_list *record; }; struct record *enter, *input_head = NULL; int important(void) {      char c;      int i = 0;      printf("enter in an expressionn");      together as((c =getchar()) != ('n')){           enter = (struct record*)malloc(sizeof(struct record));           enter->archives = c;           enter->next = input_head;           input_head = enter;      }      enter = input_head;      together as(enter){           printf("%c", enter->archives);           enter = enter->next;      }      return 0; }

Still have questions? Get your answers by asking now.