-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added recursive depth first searches
- Loading branch information
1 parent
1e02652
commit ba2a933
Showing
5 changed files
with
175 additions
and
1 deletion.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
C/Data-Structures/TREES/BINARY-TREE/DFS/RecursiveInorder.c
This file contains 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
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
53
C/Data-Structures/TREES/BINARY-TREE/DFS/RecursivePostorder.c
This file contains 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
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
53
C/Data-Structures/TREES/BINARY-TREE/DFS/RecursivePreorder.c
This file contains 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
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"); | ||
} |
This file contains 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
This file contains 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