Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aded inorder tree traveral without recursion #38

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions postorderTraversalIter/postorder.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <stdio.h>
#include <stdlib.h>

#include "tree.h"
#include "stack.h"

void printstack(node *stack)
{
printf("stack: ");
while (!isempty(stack))
{
printf("%d ", stack->top->data);
}
printf("\n");
}

void postorderIter(tree *root)
{
}

int main()
{
// FILE *output = freopen("out.txt", "w", stdout);
// FILE *input = freopen("in.txt", "r", stdin);

tree *root = NULL;

int n, val;
scanf("%d", &n);

for (int i = 0; i < n; i++)
{
scanf("%d", &val);
root = insert(root, val);
}

printf("inorder: ");
inorder(root);
printf("\n");

printf("preorder: ");
preorder(root);
printf("\n");

printf("postorder: ");
postorder(root);
printf("\n");

// fclose(output);
// fclose(input);
return 0;
}
48 changes: 48 additions & 0 deletions postorderTraversalIter/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;
}

node *pop(node **st)
{
if (isempty(*st))
{
printf("Stack empty can't pop");
}
node *temp = (*st);
(*st) = (*st)->next;
return temp;
}

void display(node *st)
{
while (!isempty(st))
{
printf("%c ", st->top);
st = st->next;
}
printf("\n");
}
18 changes: 18 additions & 0 deletions postorderTraversalIter/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);
node *pop(node **st);

#endif
59 changes: 59 additions & 0 deletions postorderTraversalIter/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 postorderTraversalIter/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