#include #include #include "bst.h" #define TRUE 1 #define FALSE 0 int main(void) { Node root = NULL; int result = insert(10, &root); // result = insert(15, &root); result = insert(5, &root); // result = insert(1, &root); // result = insert(3, &root); printTree(root); printf("\n\n"); // result = get(3, root); // result = get(7, root); // result = removeNode(5, &root); result = removeNode(10, &root); result = insert(15, &root); result = removeNode(15, &root); printTree(root); printf("\n\n"); // use long // insert 1,000,000 random numbers (from a range of 0..10,000,000) // start timer // insert another (1,000,000) elements // read timer // find (1,000,000) elements // read timer // remove (1,000,000) elements // read timer return EXIT_SUCCESS; } // Insert element into tree, return 0 if node is already in tree int insert(int num, Node* root) { Node newNode = makeNode(num); if (*root == NULL) { *root = newNode; return TRUE; } Node n = *root; while (TRUE) { if (n->value > num) { if (n->leftChild == NULL) { n->leftChild = newNode; return TRUE; } else { n = n->leftChild; } } else if (n->value < num) { if (n->rightChild == NULL) { n->rightChild = newNode; return TRUE; } else { n = n->rightChild; } } else { return FALSE; } } } // Finds a node, returns node or 0 if not found. int get(int num, Node root) { if (root == NULL) return FALSE; Node n = root; while (TRUE) { if (n->value > num) { if (n->leftChild == NULL) { return FALSE; } else { n = n->leftChild; } } else if (n->value < num) { if (n->rightChild == NULL) { return FALSE; } else { n = n->rightChild; } } else { return num; } } } // Finds max node int findMax(Node n) { while (n->rightChild != NULL) { n = n->rightChild; } return n->value; } int removeNode(int num, Node* root) { Node n = *root; Node p = NULL; while (TRUE) { if (n->value > num) { if (n->leftChild == NULL) { return FALSE; } else { p = n; n = n->leftChild; } } else if (n->value < num) { if (n->rightChild == NULL) { return FALSE; } else { p = n; n = n->rightChild; } } else { // found it if (n->leftChild != NULL) { if (n->rightChild != NULL) { // two children int temp = findMax(n->leftChild); n->value = temp; num = temp; p = n; n = n->leftChild; } else { // left child if (p == NULL) { *root = n->leftChild; } else { if (p->leftChild->value == n->value){ p->leftChild = n->leftChild; } else { p->rightChild = n->leftChild; } } return TRUE; } } else { if (n->rightChild != NULL) { // right child if (p == NULL) { *root = n->rightChild; } else { if (p->leftChild && (p->leftChild->value == n->value)){ p->leftChild = n->rightChild; } else { p->rightChild = n->rightChild; } } return TRUE; } else { // leaf if (p == NULL) { *root = NULL; } else { if (p->leftChild && (p->leftChild->value == n->value)){ p->leftChild = NULL; } else { p->rightChild = NULL; } } return TRUE; } } } } } // Fix to deal with larger ints once necessary (fix what?) void printTree(Node n) { if (n == NULL) return; printf("%d ",n->value); printTree(n->leftChild); printTree(n->rightChild); } /* * Create a new node */ Node makeNode(int num) { Node n = (Node)malloc(sizeof(struct _Node)); if (n == NULL) { printf("Error allocating memory for node with num %d\n", num); exit(1); } n->value = num; n->leftChild = NULL; n->rightChild = NULL; return n; }