DS Lab Assignment 6
DS Lab Assignment 6
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;
};
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);
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;
};
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);
return 0;
}