Tree traversal

This is a discussion on Tree traversal within the C Programming forums, part of the General Programming Boards category; i'm trying to implement a pre order tree traversal on a name however i get an error "name undeclared in ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    67

    Tree traversal

    i'm trying to implement a pre order tree traversal on a name however i get an error "name undeclared in funtion pre order"

    what have i done wrong? how do i fix it? will my code perform what i want it to? any help is appreciated;

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    typedef struct tree
    {
        char name[20];
        struct tree *left;
        struct tree *right;
    }Tree;    
    
    Tree *makenode( char *in, Tree *l, Tree *r)
    {
        Tree *t=malloc(sizeof(Tree));
        t->left =l;
        t->right =r;
        strncpy(t->name,in,19);
        return t;
    }    
    
    Tree *insert( Tree *root, char *what)
    {
        if(root==NULL)
        {root=makenode(what,NULL,NULL);}
        else if(strcmp(what, root->name)<0)
        {root->left =insert(root->left,what);}
        else{root->right =insert(root->right,what);
    }
    return root;
    }     
    
    char *preorder(Tree *root, char *what)
    {
    	if(root==NULL)
    		{return "NOT FOUND";}
    	printf("%s ",root->name);
    	preorder(root->left,name);
    	preorder(root->right,name);
    }
    
    int main(void)
    {
        char s[20];
        Tree *tree =NULL;
        do{
            scanf("%s",s);
    	if(strcmp(s,".")==0)
    	{break;}
    	if(strcmp(s,".")!=0);
            {tree=insert(tree,s);}
        }
        while(strcmp(s,".")!=0);
        preorder(tree,s);
    return 0; 
    }
    thanks

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >what have i done wrong?
    Just what the error says you've done wrong. You're using name, but name doesn't exist:
    Code:
    char *preorder(Tree *root, char *what)
    {
    	if(root==NULL)
    		{return "NOT FOUND";}
    	printf("%s ",root->name);
    	preorder(root->left,name);
    	preorder(root->right,name);
    }
    It looks more like you want a binary search rather than a preorder traversal:
    Code:
    char *search(Tree *root, char *what)
    {
      if (root == NULL)
        return "NOT FOUND";
      else if (strcmp(what, root->name) < 0)
        return search(root->left, what);
      else if (strcmp(what, root->name) > 0)
        return search(root->right, what);
      else
        return root->name;
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    67
    ok looks like ive messed up badly , all i want to do is perform pre order tree traversal, e.g. go left, right , root , how do i do this?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >how do i do this?
    Code:
    void preorder(Tree *root)
    {
      if (root == NULL)
        return;
    
      printf("%s\n", root->name);
      preorder(root->left);
      preorder(root->right);
    }
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    67
    thanks prelude

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Tree Search
    By C++Newbie in forum C++ Programming
    Replies: 7
    Last Post: 04-05-2011, 02:17 AM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 06:59 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 11:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21