/* * File: BST.cpp * * Created on November 9, 2015, 9:44 PM */ #include #include using namespace std; struct TreeNode { int key; TreeNode *left, *right; }; // Allocates and returns a binary tree node with a given key value TreeNode* get_bin_tree_node(int val) { TreeNode* p = new TreeNode; if (p == NULL) return NULL; p->key = val; p->left = p->right = NULL; return p; } // inserts the key k in the binary search tree rooted at r // returns the root of the tree after the insertion TreeNode* bst_insert(int k, TreeNode* r) { TreeNode *p, *q, *prev = NULL; p = r; while (p != NULL) { if (p->key == k) { cout << "\nThe key " << k << " already exists in the tree. No insertion.\n"; return r; } prev = p; if (k < p->key) p = p->left; else p = p->right; } /* allocate a new node and set its field key equal to k */ if ((q = get_bin_tree_node(k)) == NULL) { cout << "...out of memory. Insertion aborted.\n"; return r; } /* link the new node in the tree */ if (prev == NULL) r = q; /* 1st node = tree root */ else if (k < prev->key) prev->left = q; /* link as left child */ else prev->right = q; /* link as right child */ cout << "\nThe key " << k <<" was successfully inserted.\n"; return r; } // returns the parent of a node whose key is k and r is the root of the tree TreeNode* parent(int k, TreeNode* r) { if ((r->left != NULL && r->left->key == k) || (r->right != NULL && r->right->key == k)) return r; if (k < r->key) return (parent(k, r->left)); else return (parent(k, r->right)); } // searchs a key in the binary search tree and returns a pointer to the node // with this key or NULL if the key was not found TreeNode* bst_search(int k, TreeNode* r) { //TODO: IMPLEMENT } // deletes the key k from the binary search tree rooted at r // returns a pointer to the root of the tree after the deletion TreeNode* bst_delete(int k, TreeNode* r) { //TODO: IMPLEMENT } // prints all the keys of the binary search tree (inorder) void print_binary_tree(int h, TreeNode *r) { //TODO: IMPLEMENT } // postorder freeing of the binary search tree void free_binary_tree(TreeNode *r) { //TODO: IMPLEMENT } // prints a user choice menu void print_menu() { cout << "\n"; cout << "Type 1 to insert an integer\n"; cout << " 2 to delete an integer\n"; cout << " 3 to search for an integer in the tree\n"; cout << " 4 to print the tree\n"; cout << " 5 to free the nodes of the tree\n"; cout << " 6 to print this menu of tree commands\n"; cout << " or any other number to exit.\n"; } // gets the user choice and returns as an integer int get_choice() { int i; cout << "Command> "; cin >> i; while (i == 6) { print_menu(); cout << "Command> "; cin >> i; } return i; } // main method int main(int argc, char** argv) { int i, choice; TreeNode *treeroot; treeroot = NULL; /* empty tree */ print_menu(); choice = get_choice(); /* handles choice = 6 */ while (choice > 0 && choice < 6) { switch (choice) { case 1: cout << "Enter a number: "; /* insertion */ cin >> i; treeroot = bst_insert(i, treeroot); break; case 2: if (treeroot == NULL) /* deletion */ cout << "\nThe tree is empty. There is nothing to delete.\n"; else { cout << "Enter a number: "; cin >> i; treeroot = bst_delete(i, treeroot); } break; case 3: cout << "Enter a number: "; /* search */ cin >> i; if (bst_search(i, treeroot) != NULL) cout << "The number was found in the tree.\n"; else cout << "The number was not found.\n"; break; case 4: /* print */ if (treeroot != NULL) print_binary_tree(0, treeroot); else cout << "\nThe tree is empty. There is nothing to print.\n"; break; case 5: /* free nodes */ if (treeroot != NULL) { free_binary_tree(treeroot); treeroot = NULL; } else cout << "\nThe tree is empty. There is nothing to free.\n"; break; default: /* not needed */ break; } cout << "\n"; cout << "Type 1 to 5 for a command on a binary search tree,\n"; cout << "6 for the menu of commands, or any other number to exit.\n"; choice = get_choice(); /* handles choice = 6 */ } cout << "\nProgram completed.\n"; return (EXIT_SUCCESS); }