Thread: binary tree

  1. #1
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926

    binary tree

    I am working on a binary tree. And was wondering what is the best way to traverse it(I want to print it) also does anyone see anything wrong with this. It looks fine to me(besides checking the return value of fgets and ssanf?
    Code:
    struct info{
            int x;
            struct info *less;
            struct info *more;
    };
    
    void add_number(void){
            static struct info *head;
            int x;
            char buffer[BUFSIZ];
            if(!head){
                    head=malloc(sizeof(struct info));
                    if(!head){
                            printf("Out of Memory\n");
                            exit(1);
                    }
                    printf("Enter a number\n");
                    fgets(buffer,sizeof buffer,stdin);
                    sscanf(buffer,"%d",&x);
                    head->x=x;
            }
            else{
                    printf("Enter a number\n");
                    fgets(buffer,sizeof buffer,stdin);
                    sscanf(buffer,"%d",&x);
                    if(x>head->x){
                            head->more=malloc(sizeof(struct info));
                            if(!head->more){
                                    printf("Out of Memory\n");
                                    exit(1);
                            }
                            head->more->x=x;
                            head=head->more; /*is this a good idea*/
                    }
                    else if(x<head->x){
                            head->less=malloc(sizeof(struct info));
                            if(!head->less){
                                    printf("Out of Memory\n");
                                    exit(1);
                            }
                            head->less->x=x;
                            head=head->less;  /*same here*/
                    }
                    else
                            printf("Equal number as before disregaurding\n");
            }
    }
    Last edited by linuxdude; 09-07-2004 at 09:00 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, you could always read this FAQ entry. Just a thought.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    Quote Originally Posted by quzah
    Well, you could always read this FAQ entry. Just a thought.

    Quzah.
    And be cool, stay in school.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Well, technically that is a binary search tree by definition. But realistically it isn't very useful. The problem is here:

    >head=head->more; /*is this a good idea*/

    and here:

    >head=head->less; /*same here*/

    Tell me, since you don't use a stack, or recursion, or save variables, or parent pointers, how do you get back to the original head? You don't, really. It's lost the instant you reseat the pointer without saving its current state. That's why most binary search tree insertion functions are recursive:
    Code:
    struct node *insert ( struct node *root, int value )
    {
      if ( root == NULL ) {
        root = malloc ( sizeof *root );
    
        if ( root == NULL )
          complain();
    
        root->data = value;
        root->left = root->right = NULL;
      }
      else if ( value < root->data )
        root->left = insert ( root->left, value );
      else
        root->right = insert ( root->right, value );
    
      return root;
    }
    It's an easy and concise way to save the state of your tree without losing it when you work on subtrees.
    My best code is written with the delete key.

  5. #5
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    that is what I was trying to figure out how to get to the begininng. Thanx Prelude, very simplistic. Thanks Quzah.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 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, 10:45 PM