From 71667df0d7b2bbbdb16b2db6cea72d50a9ac48c9 Mon Sep 17 00:00:00 2001 From: Rahul Rajeev Pillai <66192267+Ashborn-SM@users.noreply.github.com> Date: Sun, 4 Jul 2021 20:39:05 +0530 Subject: [PATCH] chore(C): add binary search tree (#376) Co-authored-by: Ujjwal <75884061+UG-SEP@users.noreply.github.com> Co-authored-by: Anirudh Kaushal Co-authored-by: Ujjwal <75884061+UG-SEP@users.noreply.github.com> --- algorithms/C/README.md | 1 + algorithms/C/tree/binary-search-tree.c | 123 +++++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 algorithms/C/tree/binary-search-tree.c diff --git a/algorithms/C/README.md b/algorithms/C/README.md index 862ac8b2..0b19e6e1 100644 --- a/algorithms/C/README.md +++ b/algorithms/C/README.md @@ -36,6 +36,7 @@ ## Tree - [Height Of Tree](tree/height-of-a-tree.c) - [Max and Min Element Of Tree](tree/min-and-max-of-tree.c) +- [Binary Search Tree](tree/binary-search-tree.c) ## Searching - [Binary Search](searching/Binary-search.c) diff --git a/algorithms/C/tree/binary-search-tree.c b/algorithms/C/tree/binary-search-tree.c new file mode 100644 index 00000000..2a8b1e79 --- /dev/null +++ b/algorithms/C/tree/binary-search-tree.c @@ -0,0 +1,123 @@ +#include +#include +#include + +struct node{ + struct node* left; + struct node* right; + int val; +}; + +struct node* createNode(int val){ + struct node* n = (struct node*)malloc(sizeof(struct node)); + n->val = val; + n->left = NULL; + n->right = NULL; + return n; +} + +struct node* insert(struct node* root, int val){ + if(root == NULL){ + root = createNode(val); + } + else if(root->val < val){ + root->right = insert(root->right, val); + } + else{ + root->left = insert(root->left, val); + } + + return root; +} + +bool is_full(struct node* root){ + if(root == NULL || (root->left == NULL && root->right == NULL)){ + return true; + } + else if(root->left != NULL && root->right != NULL){ + return is_full(root->left) && is_full(root->right); + } + return false; +} + +int depth(struct node* root){ + int d = 0; + while(root != NULL){ + root = root->left; + d++; + } + return d; +} + +bool is_perfect(struct node* root, int depth, int current_lvl){ + if(root == NULL){ + return true; + } + else if(root->left == NULL && root->right == NULL){ + return depth == current_lvl; + } + else if(root->left != NULL && root->right != NULL){ + return is_perfect(root->left, depth, 1+current_lvl) && + is_perfect(root->right, depth, 1+current_lvl); + } + + return false; +} + +void print(struct node* root){ + if(root != NULL){ + print(root->left); + printf("%i ", root->val); + print(root->right); + } +} + +void deallocate(struct node** root){ + if(*root != NULL){ + deallocate(&(*root)->left); + deallocate(&(*root)->right); + free(*root); + *root = NULL; + } +} + +int main(){ + struct node* root = NULL; + + root = insert(root, 37); + root = insert(root, 19); + root = insert(root, 4); + root = insert(root, 22); + root = insert(root, 51); + root = insert(root, 55); + root = insert(root, 42); + + if(is_full(root)){ + printf("BST is full\n"); + } + else{ + printf("BST is not full\n"); + } + if(is_perfect(root, depth(root)-1, 0)){ + printf("BST is Perfect\n"); + } + else{ + printf("BST is not Perfect\n"); + } + print(root); + /* + Tree structure + 37 + / \ + 19 51 + / \ / \ + 4 22 42 55 + + Output: + BST is full + BST is Perfect + 4 19 22 37 42 51 55 + */ + deallocate(&root); + return 0; +}