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

Document 29

The document contains a C program that defines a binary tree structure and implements functions for creating nodes and performing tree traversals (pre-order, post-order, and in-order). It initializes a binary tree with specific nodes and prints the results of each traversal. The main function demonstrates the creation and traversal of the tree.

Uploaded by

Ñákúl Joshi
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)
7 views2 pages

Document 29

The document contains a C program that defines a binary tree structure and implements functions for creating nodes and performing tree traversals (pre-order, post-order, and in-order). It initializes a binary tree with specific nodes and prints the results of each traversal. The main function demonstrates the creation and traversal of the tree.

Uploaded by

Ñákúl Joshi
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/ 2

Lab Assignment 9

#include <stdio.h>

#include <stdlib.h>

typedef struct node {

int data;

struct node* left;

struct node* right;

}Node;

Node* createNode(int data) {

Node* q;

q = (Node*)malloc(sizeof(Node));

q->data = data;

q->left = NULL;

q->right = NULL;

return q;}

void preOrder(Node *root) {

if (root != NULL){

printf("%d",root->data);

preOrder(root -> left);

preOrder(root ->right);

}}

void postOrder(Node *root) {

if (root != NULL){

postOrder(root -> left);

postOrder(root ->right);

printf("%d",root->data);

}}

void InOrder(Node *root) {


if (root != NULL){
InOrder(root -> left);

printf("%d",root->data);

InOrder(root ->right);

}}

int main() {

Node *p = createNode(4);

Node *p1 = createNode(1);

Node *p2 = createNode(6);

Node *p3 = createNode(5);

Node *p4 = createNode(2);

p -> left = p1;

p -> right = p2;

p1 -> left = p3;

p1 -> right = p4;

preOrder(p);

printf("\n");

postOrder(p);

printf("\n");

InOrder(p);}

Output :

You might also like