#include #include #include #include "bst.h" #define TRUE 1 #define FALSE 0 // Insert element into tree, return 0 if node is already in tree int insert(uint32_t num, Node newNode, Node* root) { 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(uint32_t 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 uint32_t findMax(Node n) { while (n->rightChild != NULL) { n = n->rightChild; } return n->value; } int removeNode(uint32_t 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 uint32_t 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 && 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); } uint32_t treeSize(Node n) { if (n == NULL) return 0; return treeSize(n->leftChild) + treeSize(n->rightChild) + 1; } int height(Node root) { if (root == NULL) return -1; return heightHelper(root); } int heightHelper(Node n) { int lheight = 0; int rheight = 0; if (n->leftChild != NULL) lheight = heightHelper(n->leftChild); if (n->rightChild != NULL) rheight = heightHelper(n->rightChild); if (lheight > rheight) return lheight + 1; return rheight + 1; } /* * Create a new node */ Node makeNode(uint32_t 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; }