Thread: What's wrong with my Tree ):

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    8

    Thumbs up What's wrong with my Tree ):

    Hi there I'm trying to practice trees, and I'm not quite sure how I should implement my main function, although I'm pretty sure My other functions are correct... I could be wrong though :x

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    typedef struct node *tree;
    
    tree newNode (int newValue);
    tree insert(tree node, int newValue); 
    
    int main (int argc, char *argv[]) {
    
       int newValue;
       scanf ("%d", &newValue);
       newNode (newValue);
       insert(newNode, newValue); 
       return EXIT_SUCCESS;
    }
    
    //create a struct called node
    typedef struct node {
       int value;
       tree left;
       tree right;
    } node;
    
    //New node function,
    tree newNode (int newValue) {
       tree node = malloc (sizeof (struct node));
       node->value = newValue;
       node->left = NULL;
       node->right = NULL;
       return node;
    }
    
    //inserts a new node with the given number in the correct place in the tree.
    // Returns the new root pointer
    
    tree insert(tree node, int newValue) {
      //If the tree is empty, return a new, single node
       if (node == NULL) {
          return(newNode(newValue));
       } else if (newValue < node->value) {   //Otherwise, recur down the tree
          node->left = insert (node->left, newValue);
       } else if (newValue >= node->value) {
          node->right = insert (node->right, newValue);
       }
       return node;
    }
    Code:
       insert(newNode, newValue);
    In the above line from my main function, I get an error saying "trees.c:14: warning: passing argument 1 of ‘insert’ from incompatible pointer type" so obviously newNode doesn't belong there, but what Should go in there then?

    Thanks guys

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, the prototype of insert says it takes a tree. So you should pass the tree you want the value to be inserted in. Since you never bothered to create a tree, you don't have one to insert into. You should probably do that.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    8
    but I typedefed tree to be a pointer to a node, and since I created a node using newNode. so Shouldn't I be inserting a Node I want to insert the value in? which in this case is newNode?

    thanks for your reply

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    newNode is a function, it is not a node itself. newNode returns a node, it is true. You immediately throw it away and stomp on it (i.e., the result is not assigned to anything). And then, you wouldn't have to do insert again, unless you wanted to insert the node a second time.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    8
    oh okay, so does that mean newNode creates a new node called "node" when I call it?

    so it should be

    [code] insert(node, newValue); [\code]

    ?

    I recall doing that and I think I got an error saying node is undefined

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    No.

    (I suppose you want the full answer: newNode creates a new node, which it returns. It is your responsibility to put that node where you want it, via putting a name on the left side and an = in between.)

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    8
    ah, that does make a lot more sense... Thanks a lot :]

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, 01:17 AM
  2. Weight Balanced Tree - Implementation
    By paulovitorbal in forum C Programming
    Replies: 1
    Last Post: 10-31-2007, 02:28 PM
  3. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  4. 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
  5. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM