0% found this document useful (0 votes)
9 views4 pages

DS Lab Assignment 6

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)
9 views4 pages

DS Lab Assignment 6

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/ 4

Name : Shubham Maurya

Enrollment no : 0801IT231074
Btech IT 2nd Year(A3)

Lab Assignment 6

Q-1) Write a program to create binary tree with pre, post and in-order traversing.

Code :

#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* left;
struct Node* right;
};

struct Node* newNode(int data) {


struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}

void preOrder(struct Node* node) {


if (node == NULL) {
return;
}
printf("%d ", node->data);
preOrder(node->left);
preOrder(node->right);
}

void inOrder(struct Node* node) {


if (node == NULL) {
return;
}
inOrder(node->left);
printf("%d ", node->data);
inOrder(node->right);
}

void postOrder(struct Node* node) {


if (node == NULL) {
return;
}
postOrder(node->left);
postOrder(node->right);
printf("%d ", node->data);
}

int main() {
printf("Shubham Maurya\n0801IT231074\n\n");
struct Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);

printf("Pre-order traversal: ");


preOrder(root);

printf("\nIn-order traversal: ");


inOrder(root);

printf("\nPost-order traversal: ");


postOrder(root);

return 0;
}

Q-2) Write a program to create binary search tree with pre, post and in order traversing.
Code :
#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* left;
struct Node* right;
};

struct Node* createNode(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}

struct Node* insert(struct Node* root, int data) {


if (root == NULL) {
return createNode(data);
}
if (data < root->data) {
root->left = insert(root->left, data);
} else if (data > root->data) {
root->right = insert(root->right, data);
}
return root;
}

void preorderTraversal(struct Node* root) {


if (root != NULL) {
printf("%d ", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
}

void inorderTraversal(struct Node* root) {


if (root != NULL) {
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
}
void postorderTraversal(struct Node* root) {
if (root != NULL) {
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ", root->data);
}
}

int main() {
printf("Shubham Maurya\n0801IT231074\n\n");
struct Node* root = NULL;
int n, i, data;
printf("Enter number of elements to insert into BST: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &data);
root = insert(root, data);
}
printf("\nPre-order Traversal: ");
preorderTraversal(root);

printf("\nIn-order Traversal: ");


inorderTraversal(root);

printf("\nPost-order Traversal: ");


postorderTraversal(root);

return 0;
}

You might also like