/* * File: tree2.c * Creator: George Ferguson * * Second version of the binary tree program. * Allows integers to be added to the tree and the entire tree printed. */ #include #include struct Node { int value; struct Node* leftChild; struct Node* rightChild; }; struct Node* new_Node(int value) { struct Node* this = (struct Node*)malloc(sizeof(struct Node)); if (this == NULL) { return NULL; // Out of memory... } this->value = value; this->leftChild = NULL; this->rightChild = NULL; return this; } void Node_print(struct Node* this) { if (this != NULL) { Node_print(this->leftChild); printf("%d\n", this->value); Node_print(this->rightChild); } } struct Node* Node_add(struct Node* this, int value) { if (this == NULL) { return new_Node(value); } else if (value < this->value) { this->leftChild = Node_add(this->leftChild, value); } else if (value > this->value) { this->rightChild = Node_add(this->rightChild, value); } else { // Already in tree: ignore (or you could do something else) } return this; } int main(int argc, char* argv[]) { struct Node* root = NULL; root = Node_add(root, 20); root = Node_add(root, 10); root = Node_add(root, 5); root = Node_add(root, 15); root = Node_add(root, 30); root = Node_add(root, 40); root = Node_add(root, 20); Node_print(root); }