From ba2a933cc60c71c96613eb62a92aa9a364d139de Mon Sep 17 00:00:00 2001 From: GauravWalia19 Date: Sat, 9 Feb 2019 00:05:06 +0530 Subject: [PATCH] added recursive depth first searches --- .../TREES/BINARY-TREE/DFS/RecursiveInorder.c | 53 +++++++++++++++++++ .../BINARY-TREE/DFS/RecursivePostorder.c | 53 +++++++++++++++++++ .../TREES/BINARY-TREE/DFS/RecursivePreorder.c | 53 +++++++++++++++++++ C/README.md | 14 ++++- datastructures.md | 3 ++ 5 files changed, 175 insertions(+), 1 deletion(-) create mode 100644 C/Data-Structures/TREES/BINARY-TREE/DFS/RecursiveInorder.c create mode 100644 C/Data-Structures/TREES/BINARY-TREE/DFS/RecursivePostorder.c create mode 100644 C/Data-Structures/TREES/BINARY-TREE/DFS/RecursivePreorder.c diff --git a/C/Data-Structures/TREES/BINARY-TREE/DFS/RecursiveInorder.c b/C/Data-Structures/TREES/BINARY-TREE/DFS/RecursiveInorder.c new file mode 100644 index 0000000..7127b4f --- /dev/null +++ b/C/Data-Structures/TREES/BINARY-TREE/DFS/RecursiveInorder.c @@ -0,0 +1,53 @@ +#include +#include +struct Node +{ + int data; + struct Node* left; + struct Node* right; +}; +struct Node* newNode(int data) +{ + struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); + newNode->data=data; + newNode->left=NULL; + newNode->right=NULL; + return newNode; +} +struct Node* ROOT = NULL; + +void inorder(struct Node* current) +{ + if(current==NULL) + { + return; + } + inorder(current->left); + printf("%d ",current->data); + inorder(current->right); +} + +int main() +{ + //building a tree + struct Node* a = newNode(1); + struct Node* b = newNode(2); + struct Node* c = newNode(3); + struct Node* d = newNode(4); + struct Node* e = newNode(5); + struct Node* f = newNode(6); + struct Node* g = newNode(7); + + ROOT = a; + ROOT->left = b; + ROOT->right = c; + + ROOT->left->left = d; + ROOT->left->right=e; + + ROOT->right->left=f; + ROOT->right->right=g; + + inorder(ROOT); + printf("\n"); +} \ No newline at end of file diff --git a/C/Data-Structures/TREES/BINARY-TREE/DFS/RecursivePostorder.c b/C/Data-Structures/TREES/BINARY-TREE/DFS/RecursivePostorder.c new file mode 100644 index 0000000..c5d8b5e --- /dev/null +++ b/C/Data-Structures/TREES/BINARY-TREE/DFS/RecursivePostorder.c @@ -0,0 +1,53 @@ +#include +#include +struct Node +{ + int data; + struct Node* left; + struct Node* right; +}; +struct Node* newNode(int data) +{ + struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); + newNode->data=data; + newNode->left=NULL; + newNode->right=NULL; + return newNode; +} +struct Node* ROOT = NULL; + +void postorder(struct Node* current) +{ + if(current==NULL) + { + return; + } + postorder(current->left); + postorder(current->right); + printf("%d ",current->data); +} + +int main() +{ + //building a tree + struct Node* a = newNode(1); + struct Node* b = newNode(2); + struct Node* c = newNode(3); + struct Node* d = newNode(4); + struct Node* e = newNode(5); + struct Node* f = newNode(6); + struct Node* g = newNode(7); + + ROOT = a; + ROOT->left = b; + ROOT->right = c; + + ROOT->left->left = d; + ROOT->left->right=e; + + ROOT->right->left=f; + ROOT->right->right=g; + + postorder(ROOT); + printf("\n"); +} \ No newline at end of file diff --git a/C/Data-Structures/TREES/BINARY-TREE/DFS/RecursivePreorder.c b/C/Data-Structures/TREES/BINARY-TREE/DFS/RecursivePreorder.c new file mode 100644 index 0000000..63fd9ef --- /dev/null +++ b/C/Data-Structures/TREES/BINARY-TREE/DFS/RecursivePreorder.c @@ -0,0 +1,53 @@ +#include +#include +struct Node +{ + int data; + struct Node* left; + struct Node* right; +}; +struct Node* newNode(int data) +{ + struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); + newNode->data=data; + newNode->left=NULL; + newNode->right=NULL; + return newNode; +} +struct Node* ROOT = NULL; + +void preorder(struct Node* current) +{ + if(current==NULL) + { + return; + } + printf("%d ",current->data); + preorder(current->left); + preorder(current->right); +} + +int main() +{ + //building a tree + struct Node* a = newNode(1); + struct Node* b = newNode(2); + struct Node* c = newNode(3); + struct Node* d = newNode(4); + struct Node* e = newNode(5); + struct Node* f = newNode(6); + struct Node* g = newNode(7); + + ROOT = a; + ROOT->left = b; + ROOT->right = c; + + ROOT->left->left = d; + ROOT->left->right=e; + + ROOT->right->left=f; + ROOT->right->right=g; + + preorder(ROOT); + printf("\n"); +} \ No newline at end of file diff --git a/C/README.md b/C/README.md index f2e5f22..efa109e 100644 --- a/C/README.md +++ b/C/README.md @@ -64,7 +64,19 @@ #### TREES -* BINARY TREE +##### BINARY TREE + +* Depth First Search + * IN ORDER + * ITERATIVE + * [RECURSIVE](Data-Structures/TREES/BINARY-TREE/DFS/RecursiveInorder.c) + * PRE ORDER + * ITERATIVE + * [RECURSIVE](Data-Structures/TREES/BINARY-TREE/DFS/RecursivePreorder.c) + * POST ORDER + * ITERATIVE + * [RECURSIVE](Data-Structures/TREES/BINARY-TREE/DFS/RecursivePostorder.c) + * GENERIC TREES * THREADED BINARY TREE * XOR TREES diff --git a/datastructures.md b/datastructures.md index 485f37b..5d8c69e 100644 --- a/datastructures.md +++ b/datastructures.md @@ -345,18 +345,21 @@ Indexer for Data Structures Lover * ITERATIVE * [JAVA](Java/Data-Structures/TREES/BINARY-TREE/IterativeInorder.java) * RECURSIVE + * [C](C/Data-Structures/TREES/BINARY-TREE/DFS/RecursiveInorder.c) * [JAVA](Java/Data-Structures/TREES/BINARY-TREE/Inorder.java) * PRE ORDER * ITERATIVE * [JAVA](Java/Data-Structures/TREES/BINARY-TREE/IterativePreorder.java) * RECURSIVE + * [C](C/Data-Structures/TREES/BINARY-TREE/DFS/RecursivePreorder.c) * [JAVA](Java/Data-Structures/TREES/BINARY-TREE/Preorder.java) * POST ORDER * ITERATIVE * [JAVA](Java/Data-Structures/TREES/BINARY-TREE/IterativePostorder.java) * RECURSIVE + * [C](C/Data-Structures/TREES/BINARY-TREE/DFS/RecursivePostorder.c) * [JAVA](Java/Data-Structures/TREES/BINARY-TREE/Postorder.java) ##### BREADTH FIRST SEARCH