From eff01210bf6a105a611a92f8030913d362d311a2 Mon Sep 17 00:00:00 2001 From: Sambit Kumar Tripathy <56729866+sambit221@users.noreply.github.com> Date: Sun, 25 Apr 2021 20:41:36 +0530 Subject: [PATCH] chore(C): min & max value of the tree (#239) --- algorithms/C/README.md | 63 ++++++++------- algorithms/C/tree/height-of-a-tree.c | 66 +++++++++++++++ algorithms/C/tree/min-and-max-of-tree.c | 102 ++++++++++++++++++++++++ 3 files changed, 202 insertions(+), 29 deletions(-) create mode 100644 algorithms/C/tree/height-of-a-tree.c create mode 100644 algorithms/C/tree/min-and-max-of-tree.c diff --git a/algorithms/C/README.md b/algorithms/C/README.md index 9d51c7c7..efa287a9 100644 --- a/algorithms/C/README.md +++ b/algorithms/C/README.md @@ -1,29 +1,34 @@ -# C - -## Arrays -- [Even and Odd](arrays/even-and-odd.c) -- [Unique Elements in an array](arrays/unique-elements-in-an-array.c) - -## Graphs -- [Prim's Algorithm](graphs/Prim's-algorithm.c) - -## Linked Lists -- [Insert and Delete at Beginning](linked-lists/Insert-and-delete-beginning.c) -- [Josephus Problem](linked-lists/josephus-problem.c) - -## Queues -- [Double Ended Queue using array](queues/double-ended-queue-using-array.c) - -## Sorting -- [Merge Sort](sorting/merge-sort.c) -- [Insertion Sort](sorting/insertion-sort.c) - -## Strings -- [Count Words](strings/count-words.c) -- [Palindrome](strings/palindrome.c) -- [Permutation of String](string/Permutation-of-String.c) - -## Searching -- [Binary Search](searching/Binary-search.c) - - +# C + +## Arrays +- [Even and Odd](arrays/even-and-odd.c) +- [Unique Elements in an array](arrays/unique-elements-in-an-array.c) + +## Graphs +- [Prim's Algorithm](graphs/Prim's-algorithm.c) + +## Linked Lists +- [Insert and Delete at Beginning](linked-lists/Insert-and-delete-beginning.c) +- [Josephus Problem](linked-lists/josephus-problem.c) + +## Queues +- [Double Ended Queue using array](queues/double-ended-queue-using-array.c) + +## Sorting +- [Merge Sort](sorting/merge-sort.c) +- [Insertion Sort](sorting/insertion-sort.c) + +## Strings +- [Count Words](strings/count-words.c) +- [Palindrome](strings/palindrome.c) +- [Permutation of String](string/Permutation-of-String.c) + + ## Tree +- [Height Of Tree](tree/height-of-a-tree.c) +- [Max and Min Element Of Tree](tree/min-and-max-of-tree.c) + +## Searching +- [Binary Search](searching/Binary-search.c) + + + diff --git a/algorithms/C/tree/height-of-a-tree.c b/algorithms/C/tree/height-of-a-tree.c new file mode 100644 index 00000000..12275a2d --- /dev/null +++ b/algorithms/C/tree/height-of-a-tree.c @@ -0,0 +1,66 @@ +// Program to find the height in a tree. + +#include +#include + +struct Node{ + int data; + struct Node *left; + struct Node *right; +}; + +// creating a Node +struct Node * createNode(int data){ + struct Node *n; + n = (struct Node *)malloc(sizeof(struct Node)); // allocating the memory in the heap + n -> data = data; // setting the data + n -> left = NULL; // setting left node to be NULL + n -> right = NULL; // setting right node to be NULL + return n; // returning the created node +} + +// Function to find the max between 2 number +int max(int a, int b){ + if(a>b) + return a; + return b; +} + +// Function to find height of the tree +int findHeight(struct Node*root){ + if (root == NULL) // if node is NULL then it will return -1 + return -1; + return max(findHeight(root->left),findHeight(root->right))+1; // it will return max of height of its 2 subtrees +1 +} + +// Driver code +int main(int argc, char const *argv[]){ + // constructing the root node by calling createNode function + struct Node *p0 = createNode(5); + struct Node *p1 = createNode(3); + struct Node *p2 = createNode(6); + struct Node *p3 = createNode(1); + struct Node *p4 = createNode(4); + + // linking the root node with left and right child nodes + p0 -> left = p1; + p0 -> right = p2; + p1 -> left = p3; + p1 -> right = p4; + // The tree looks like this + // p0 + // / \ + // p1 p2 + // / \ + // p3 p4 + + printf("\nHeight of the tree is %d",findHeight(p0)); + + return 0; + +} +/* +Output: Height of the tree is 2 + +Time complexity: O(n) + */ diff --git a/algorithms/C/tree/min-and-max-of-tree.c b/algorithms/C/tree/min-and-max-of-tree.c new file mode 100644 index 00000000..19399177 --- /dev/null +++ b/algorithms/C/tree/min-and-max-of-tree.c @@ -0,0 +1,102 @@ +// program to find the minimum and maximum element in a binary search tree (using recursion) + +#include +#include + +struct Node{ + int data; + struct Node *left; + struct Node *right; +}; + +// creating a Node +struct Node * createNode(int data){ + struct Node *n; + n = (struct Node *)malloc(sizeof(struct Node)); // allocating the memory in the heap + n -> data = data; // setting the data + n -> left = NULL; // setting left node to be NULL + n -> right = NULL; // setting right node to be NULL + return n; // returning the created node +} + +//function to insert a node in tree +struct Node *insert(struct Node *root, int data){ + if(root==NULL){ // empty tree condition + root = createNode(data); + return root; + } + + else if(data <= root->data) // data to be inserted at left subtree + root->left = insert(root->left,data); + + else if(data > root->data) // data to be inserted at right subtree + root->right = insert(root->right,data); + return root; +} + +//function to find the min element from the tree using recursion +int findMin(struct Node *root){ + if(root==NULL){ + printf("Tree is empty\n"); + return -1; + } + + struct Node *current = root; // current is the traversing node + while(current->left !=NULL){ + current=current->left; // if left node is not empty then traversing node = left node + } + return current->data; +} + +//function to find the max element using recursion +int findMax(struct Node *root){ + if(root==NULL){ + printf("Tree is empty\n"); + return -1; + } + + struct Node *current = root; // current is the traversing node + while(current->right !=NULL){ + current=current->right; // if right node is not empty then traversing node = right node + } + return current->data; +} + +int main(int argc, char const *argv[]){ + + // constructing the root node + struct Node *p0 = createNode(5); + struct Node *p1 = createNode(3); + struct Node *p2 = createNode(6); + struct Node *p3 = createNode(1); + struct Node *p4 = createNode(4); + + // linking the root node with left and right children + p0 -> left = p1; + p0 -> right = p2; + p1 -> left = p3; + p1 -> right = p4; + // the tree looks like this + // p0 + // / \ + // p1 p2 + // / \ + // p3 p4 + + // inserting few new nodes + insert(p0,15); + insert(p0,18); + insert(p0,0); + insert(p0,-1); + printf("\nMin element of the tree is %d",findMin(p0)); + printf("\nMax element of the tree is %d",findMax(p0)); + return 0; +} +/* +Output: +Min element of the tree is -1 +Max element of the tree is 18 + +Time complexity: O(n) + + */