forked from Harshsngh07/Hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Harshsngh07#41 from 007kartik-bot-1/master
Added tree traversal using recursion
- Loading branch information
Showing
5 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
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,48 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#include "stack.h" | ||
#include "tree.h" | ||
|
||
node *getNode(tree *c) | ||
{ | ||
node *nn = (node *)malloc(sizeof(node)); | ||
nn->next = NULL; | ||
nn->top = c; | ||
return nn; | ||
} | ||
|
||
int isempty(node *st) | ||
{ | ||
if (st == NULL) | ||
return 1; | ||
return 0; | ||
} | ||
|
||
void push(tree *adr, node **st) | ||
{ | ||
node *nn = getNode(adr); | ||
nn->next = (*st); | ||
(*st) = nn; | ||
} | ||
|
||
void pop(node **st) | ||
{ | ||
if (isempty(*st)) | ||
{ | ||
printf("Stack empty can't pop"); | ||
} | ||
node *temp = (*st); | ||
(*st) = (*st)->next; | ||
free(temp); | ||
} | ||
|
||
void display(node *st) | ||
{ | ||
while (!isempty(st)) | ||
{ | ||
printf("%c ", st->top); | ||
st = st->next; | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef STACK_HEADER | ||
#define STACK_HEADER | ||
|
||
#include "tree.h" | ||
|
||
typedef struct Node | ||
{ | ||
tree *top; | ||
struct Node *next; | ||
} node; | ||
|
||
node *getNode(tree *c); | ||
void display(node *st); | ||
int isempty(node *st); | ||
void push(tree *adr, node **st); | ||
void pop(node **st); | ||
|
||
#endif |
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,59 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#include "tree.h" | ||
|
||
tree *getTreeNode(int data) | ||
{ | ||
tree *nn = (tree *)malloc(sizeof(tree)); | ||
nn->left = nn->right = NULL; | ||
nn->data = data; | ||
return nn; | ||
} | ||
|
||
tree *insert(tree *root, int data) | ||
{ | ||
if (!root) | ||
{ | ||
return getTreeNode(data); | ||
} | ||
else if (root->data < data) | ||
{ | ||
root->right = insert(root->right, data); | ||
} | ||
else | ||
{ | ||
root->left = insert(root->left, data); | ||
} | ||
return root; | ||
} | ||
|
||
void inorder(tree *root) | ||
{ | ||
if (root) | ||
{ | ||
inorder(root->left); | ||
printf("%d ", root->data); | ||
inorder(root->right); | ||
} | ||
} | ||
|
||
void preorder(tree *root) | ||
{ | ||
if (root) | ||
{ | ||
printf("%d ", root->data); | ||
preorder(root->left); | ||
preorder(root->right); | ||
} | ||
} | ||
|
||
void postorder(tree *root) | ||
{ | ||
if (root) | ||
{ | ||
postorder(root->left); | ||
postorder(root->right); | ||
printf("%d ", root->data); | ||
} | ||
} |
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,16 @@ | ||
#ifndef TREE_HEADER | ||
#define TREE_HEADER | ||
|
||
typedef struct Tree | ||
{ | ||
int data; | ||
struct Tree *left, *right; | ||
} tree; | ||
|
||
tree *insert(tree *root, int data); | ||
void inorder(tree *root); | ||
void postorder(tree *root); | ||
void preorder(tree *root); | ||
void inorder(tree *root); | ||
|
||
#endif |
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,60 @@ | ||
// to run type "gcc treeTraversal.c tree.c stack.c" | ||
// then run the executable file and enter your input | ||
|
||
#include <stdio.h> | ||
|
||
#include "tree.h" | ||
#include "stack.h" | ||
|
||
void printstack(node *stack) | ||
{ | ||
printf("stack: "); | ||
while (!isempty(stack)) | ||
{ | ||
printf("%p ", stack->top->data); | ||
} | ||
printf("\n"); | ||
} | ||
|
||
int main() | ||
{ | ||
tree *root = NULL; | ||
|
||
int n, val; | ||
printf("Enter number of nodes: "); | ||
scanf("%d", &n); | ||
|
||
for (int i = 0; i < n; i++) | ||
{ | ||
printf("Enter node %d: ", i+1); | ||
scanf("%d", &val); | ||
root = insert(root, val); | ||
} | ||
|
||
//using binary search tree. | ||
//for an input of 2 1 4 3 5 | ||
// 2 | ||
// / \ | ||
// 1 3 | ||
// / \ | ||
// 4 5 | ||
|
||
// inorder: 1 2 3 4 5 | ||
// preorder: 2 1 4 3 5 | ||
// postorder: 1 3 5 4 2 | ||
|
||
|
||
printf("inorder: "); | ||
inorder(root); | ||
printf("\n"); | ||
|
||
printf("preorder: "); | ||
preorder(root); | ||
printf("\n"); | ||
|
||
printf("postorder: "); | ||
postorder(root); | ||
printf("\n"); | ||
|
||
return 0; | ||
} |