0% found this document useful (0 votes)
5 views5 pages

outputCode

Uploaded by

Jay Ware
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

outputCode

Uploaded by

Jay Ware
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <stdio.

h>
#include <stdlib.h>
#include <ctype.h>

typedef struct node {


char data;
struct node *left;
struct node *right;
} node;

typedef struct stacknode {


node* data;
struct stacknode *next;
} stacknode;

typedef struct stack {


stacknode *top;
} stack;

void init_stack(stack *s) {


s->top = NULL;
}

node* topp(stack *s) {


return s->top->data;
}

int isempty(stack *s) {


return s->top == NULL;
}

void push(stack *s, node* a) {


stacknode *p = malloc(sizeof(stacknode));
p->data = a;
p->next = s->top;
s->top = p;
}

node* pop(stack *s) {


stacknode *p;
node* x;
x = s->top->data;
p = s->top;
s->top = s->top->next;
free(p);
return x;
}

node* create_post(char postfix[10]);


node* create_pre(char prefix[10]);
void inorder_non_recursive(node *t);
void inorder(node *p);
void preorder(node *p);
void postorder(node *p);
void preorder_non_recursive(node *t);
void postorder_non_recursion(node *t);

node* create_post(char postfix[10]) {


node *p;
stack s;
init_stack(&s);
for (int i = 0; postfix[i] != '\0'; i++) {
char token = postfix[i];
if (isalnum(token)) {
p = malloc(sizeof(node));
p->data = token;
p->left = NULL;
p->right = NULL;
push(&s, p);
} else {
p = malloc(sizeof(node));
p->data = token;
p->right = pop(&s);
p->left = pop(&s);
push(&s, p);
}
}
return pop(&s);
}

node* create_pre(char prefix[10]) {


node *p;
stack s;
init_stack(&s);
int i;

for (i = 0; prefix[i] != '\0'; i++) {}

for (; i >= 0; i--) {


char token = prefix[i];
if (isalnum(token)) {
p = malloc(sizeof(node));
p->data = token;
p->left = NULL;
p->right = NULL;
push(&s, p);
} else {
p = malloc(sizeof(node));
p->data = token;
p->left = pop(&s);
p->right = pop(&s);
push(&s, p);
}
}
return pop(&s);
}

int main() {
node *r = NULL;
char postfix[10], prefix[10];
int ch, choice;

do {
printf("\n\t****TREE OPERATIONS****\n1.Construct tree from postfix
expression/ prefix expression\n2.Inorder traversal\n3.Preorder traversal\
n4.Postorder traversal\n5.Exit\nEnter your choice=");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("ENTER CHOICE:\n1.Postfix expression\n2.Prefix expression\
nchoice=");
scanf("%d", &choice);
if (choice == 1) {
printf("\nEnter postfix expression=");
scanf("%s", postfix);
r = create_post(postfix);
} else {
printf("\nEnter prefix expression=");
scanf("%s", prefix);
r = create_pre(prefix);
}
printf("\n\nTree created successfully");
break;
case 2:
printf("\nInorder Traversal of tree:\n");
inorder(r);
printf("\n Without recursion:\t");
inorder_non_recursive(r);
break;
case 3:
printf("\nPreorder Traversal of tree:\n");
preorder(r);
printf("\npreorder traversal without recursion:\t");
preorder_non_recursive(r);
break;
case 4:
printf("\nPostorder Traversal of tree:\n");
postorder(r);
printf("\npostorder traversal without recursion\n");
postorder_non_recursion(r);
break;
}
} while (ch != 5);
return 0;
}

void inorder(node *p) {


if (p != NULL) {
inorder(p->left);
printf("%c", p->data);
inorder(p->right);
}
}

void preorder(node *p) {


if (p != NULL) {
printf("%c", p->data);
preorder(p->left);
preorder(p->right);
}
}

void postorder(node *p) {


if (p != NULL) {
postorder(p->left);
postorder(p->right);
printf("%c", p->data);
}
}

void inorder_non_recursive(node *t) {


stack s;
init_stack(&s);
while (t != NULL) {
push(&s, t);
t = t->left;
}
while (!isempty(&s)) {
t = pop(&s);
printf("%c", t->data);
t = t->right;
while (t != NULL) {
push(&s, t);
t = t->left;
}
}
}

void preorder_non_recursive(node *t) {


stack s;
init_stack(&s);
while (t != NULL) {
printf("%c", t->data);
push(&s, t);
t = t->left;
}
while (!isempty(&s)) {
t = pop(&s);
t = t->right;
while (t != NULL) {
printf("%c", t->data);
push(&s, t);
t = t->left;
}
}
}

void postorder_non_recursion(node *t) {


stack s, s1;
init_stack(&s);
init_stack(&s1);
node *t1;
while (t != NULL) {
push(&s, t);
push(&s1, NULL);
t = t->left;
}
while (!isempty(&s)) {
t = pop(&s);
t1 = pop(&s1);
if (t1 == NULL) {
push(&s, t);
push(&s1, (node *)1);
t = t->right;
while (t != NULL) {
push(&s, t);
push(&s1, NULL);
t = t->left;
}
} else {
printf("%c", t->data);
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy