Skip to content

Commit

Permalink
Merge pull request Harshsngh07#41 from 007kartik-bot-1/master
Browse files Browse the repository at this point in the history
Added tree traversal using recursion
  • Loading branch information
Harshsngh07 authored Oct 17, 2019
2 parents 492dacd + cf6c834 commit 0883d4b
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 0 deletions.
48 changes: 48 additions & 0 deletions Language/C/treeTraversalRecur/stack.c
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");
}
18 changes: 18 additions & 0 deletions Language/C/treeTraversalRecur/stack.h
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
59 changes: 59 additions & 0 deletions Language/C/treeTraversalRecur/tree.c
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);
}
}
16 changes: 16 additions & 0 deletions Language/C/treeTraversalRecur/tree.h
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
60 changes: 60 additions & 0 deletions Language/C/treeTraversalRecur/treeTraversal.c
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;
}

0 comments on commit 0883d4b

Please sign in to comment.