Created
October 10, 2017 15:11
-
-
Save tlively/e674eb80d84f884c5e300cd69fcebfe2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdbool.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| // a binary search tree node | |
| typedef struct node | |
| { | |
| int val; | |
| struct node *left; | |
| struct node *right; | |
| } node; | |
| // inserts an item into the BST at `root_ptr`. | |
| // Returns true if successful, false otherwise. | |
| bool insert(node **root_ptr, int item); | |
| // traverses the BST `root` from left to right, | |
| // printing out its items | |
| void traverse(node *root); | |
| // inserts nodes into a binary search tree, | |
| // then traverses the tree and prints them | |
| int main(void) | |
| { | |
| // root of the BST | |
| node *root = NULL; | |
| // insert items into BST | |
| int items[] = {4, 7, 2, 9, 6, 8, 1, 3, 5, 0}; | |
| for (int i = 0; i < 10; i++) | |
| { | |
| if (!insert(&root, items[i])) | |
| { | |
| printf("Could not insert %d\n", items[i]); | |
| return 1; | |
| } | |
| } | |
| // print items in BST | |
| traverse(root); | |
| } | |
| bool insert(node **root_ptr, int item) | |
| { | |
| (void)root_ptr; | |
| (void)item; | |
| return false; | |
| } | |
| void traverse(node *root) | |
| { | |
| if (root == NULL) | |
| { | |
| return; | |
| } | |
| traverse(root->left); | |
| printf("%d\n", root->val); | |
| traverse(root->right); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment