0% found this document useful (0 votes)
20 views2 pages

Trees: Department of Cse

This C program defines a binary tree structure with left and right child pointers. It includes functions to create a tree by recursively prompting the user for nodes, and to perform preorder, inorder, and postorder tree traversals by recursively printing the node data. The main function calls create to build the tree, then calls the traversal functions to output the order of visiting nodes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views2 pages

Trees: Department of Cse

This C program defines a binary tree structure with left and right child pointers. It includes functions to create a tree by recursively prompting the user for nodes, and to perform preorder, inorder, and postorder tree traversals by recursively printing the node data. The main function calls create to build the tree, then calls the traversal functions to output the order of visiting nodes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

DEPARTMENT OF CSE

TREES
#include<stdio.h>
#include<conio.h>
typedef struct node
{
int data;
struct node *left;
struct node *right;
} node;

int create()
{
node *p;
int x;
printf("Enter data(-1 for no node):");
scanf("%d",&x);

if(x==-1)
return NULL;

p=(node*)malloc(sizeof(node));
p->data=x;
printf("Enter left child of %d:\n",x);
p->left=create();
printf("Enter right child of %d:\n",x);
p->right=create();
return p;
}

void preorder(node *t)


{
if(t!=NULL)
{
printf(" %d",t->data);
preorder(t->left);
preorder(t->right);
}
}
void inorder(node *t)
{
if(t!=NULL)
{
inorder(t->left);
printf(" %d",t->data);
DEPARTMENT OF CSE
inorder(t->right);
}
}
void postorder(node *t)
{
if(t!=NULL)
{
postorder(t->left);
postorder(t->right);
printf(" %d",t->data);
}
}
void main()
{
node *root;
root=create();
printf("\nThe preorder traversal of tree is: ");
preorder(root);
printf("\nThe inorder traversal of tree is: ");
inorder(root);
printf("\nThe postorder traversal of tree is: ");
postorder(root);
getch();
}

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