0% found this document useful (0 votes)
32 views3 pages

Typedef Struct Char Struct: #Include #Include

This C program builds a binary tree from a postfix expression and then traverses the tree using preorder, inorder, and postorder traversal methods. It defines a node structure to represent nodes in the tree and uses a stack to simulate recursion while building the tree from the postfix notation. It then prints out the results of each traversal type.

Uploaded by

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

Typedef Struct Char Struct: #Include #Include

This C program builds a binary tree from a postfix expression and then traverses the tree using preorder, inorder, and postorder traversal methods. It defines a node structure to represent nodes in the tree and uses a stack to simulate recursion while building the tree from the postfix notation. It then prints out the results of each traversal type.

Uploaded by

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

#include <stdio.

h>
#include <stdlib.h>

typedef struct nodeStructure


{
char data;
struct nodeStructure *left, *right;
} node;

node *root;
node *stack[10];
int top;

void operatorNode( char );


void operandNode( char );

node *pop();
void push( node * );

void postOrder( node * );


void preOrder( node * );
void inOrder( node * );

int main(void)
{
root = NULL;
top = -1;

char postfix[10] = "ABC*+D+";

int i = 0;
char ch;
while( (ch = postfix[i]) != '\0' )
{
if ( ch == '/' || ch == '*' || ch == '-'|| ch == '+' )
{
operatorNode( ch );
}
else
{
operandNode( ch );
}
i++;
}

root = pop();
printf( "PREORDER : " );
preOrder( root );
printf( "\n" );

printf( "INORDER : " );


inOrder( root );
printf( "\n" );

printf( "POSTORDER: " );


postOrder( root );
getch();
return 0;
}

void push( node *ptr )


{
top++;
stack[top] = ptr;
return;
}
node *pop()
{
node *del;
del = stack[top];
top--;
return del;
}

void operandNode( char ch )


{
node *newNode;
newNode = (node *)malloc( 1 * sizeof(node) );
newNode->data = ch;
newNode->left = NULL;
newNode->right = NULL;
push( newNode );
}

void operatorNode( char ch )


{
node *newNode, *pl, *pr;
newNode = (node *)malloc( 1 * sizeof(node) );

newNode->data = ch;
pr = pop();
pl = pop();
newNode->left = pl;
newNode->right = pr;

push( newNode );
}

void preOrder( node *ptr )


{
printf( "%c ", ptr->data );
if ( ptr->left != NULL )
{
preOrder( ptr->left );
}
if ( ptr->right != NULL )
{
preOrder( ptr->right );
}
return;
}

void inOrder( node *ptr )


{
if ( ptr->left != NULL )
{
inOrder( ptr->left );
}

printf( "%c ", ptr->data );

if ( ptr->right != NULL )
{
inOrder( ptr->right );
}
return;
}

void postOrder( node *ptr )


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

return;
}

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