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