Skip to content

Commit

Permalink
added recursive depth first searches
Browse files Browse the repository at this point in the history
  • Loading branch information
GauravWalia19 committed Feb 8, 2019
1 parent 1e02652 commit ba2a933
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 1 deletion.
53 changes: 53 additions & 0 deletions C/Data-Structures/TREES/BINARY-TREE/DFS/RecursiveInorder.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <stdio.h>
#include <stdlib.h>
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");
}
53 changes: 53 additions & 0 deletions C/Data-Structures/TREES/BINARY-TREE/DFS/RecursivePostorder.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <stdio.h>
#include <stdlib.h>
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");
}
53 changes: 53 additions & 0 deletions C/Data-Structures/TREES/BINARY-TREE/DFS/RecursivePreorder.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <stdio.h>
#include <stdlib.h>
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");
}
14 changes: 13 additions & 1 deletion C/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions datastructures.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ba2a933

Please sign in to comment.