Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Inserts the item at the appropriate place in the tree. Returns `0` on success and `1` on failure. Updates the depth on the tree after each insert.
- int insertItem(BST* tree, void* item){
- //if root isn't made, make root with passed item
- if (tree->tree_root == NULL){
- Node* root = createNode(item);
- tree->tree_root = root;
- tree->depth++;
- return 0;
- }
- //if not, pass recursively into the insert function, make my job easier
- else{
- tree->depth++;
- return insert(tree->tree_root, item);
- }
- }
- int insert(Node* thru, void* item){
- if(item > thru->item) // if item is greater. Should be inserted to right
- thru->nodes[1] = insert(thru->nodes[1], item);
- else if (item < thru->item)// item is smaller, should be inserted to left
- thru->nodes[0] = insert(thru->nodes[0],item);
- else
- return 1;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment