Upload files to "DS/C/Lab/Week10"

This commit is contained in:
Aadit Agrawal 2024-10-15 10:18:41 +05:30
parent 972d35034c
commit 2a9ea110f0
5 changed files with 655 additions and 0 deletions

View File

@ -0,0 +1,66 @@
#include <stdio.h>
#include <stdlib.h>
typedef struct Node * tptr;
typedef struct Node {
int data;
struct Node *left;
struct Node *right;
} Node;
tptr createNode(int data) {
tptr newNode = (tptr)malloc(sizeof(Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
int areEqual(tptr tree1, tptr tree2) {
if (tree1 == NULL && tree2 == NULL) {
return 1;
}
if (tree1 == NULL || tree2 == NULL) {
return 0;
}
return (tree1->data == tree2->data) &&
areEqual(tree1->left, tree2->left) &&
areEqual(tree1->right, tree2->right);
}
void printTree(tptr root) {
if (root == NULL) {
return;
}
printTree(root->left);
printf("%d ", root->data);
printTree(root->right);
}
int main() {
tptr root1 = createNode(1);
root1->left = createNode(2);
root1->right = createNode(3);
root1->left->left = createNode(4);
tptr root2 = createNode(1);
root2->left = createNode(2);
root2->right = createNode(3);
root2->left->left = createNode(4);
printf("Tree 1: ");
printTree(root1);
printf("\n");
printf("Tree 2: ");
printTree(root2);
printf("\n");
if (areEqual(root1, root2)) {
printf("The trees are equal.\n");
} else {
printf("The trees are not equal.\n");
}
return 0;
}

View File

@ -0,0 +1,351 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 100
typedef struct TNode *Tptr;
typedef struct TNode {
int data;
Tptr leftchild;
Tptr rightchild;
} TNode;
Tptr root;
int top = -1;
Tptr stack[MAX];
// Create nodes in the tree
Tptr createNode(int val) {
Tptr temp = (Tptr)malloc(sizeof(TNode));
temp->data = val;
temp->leftchild = temp->rightchild = NULL;
return temp;
}
// Create the binary tree
void createTree(int N) {
int cData, dIndex;
char dir[50];
Tptr previous = NULL, current;
for (int i = 0; i < N; i++) {
printf("Enter data for node %d (integer value): ", i + 1);
scanf("%d", &cData);
Tptr temp = createNode(cData);
// Assign root node
if (!root) {
root = temp;
} else {
printf("Enter the direction to insert (L for left, R for right): ");
scanf("%s", dir);
current = root;
previous = NULL;
for (dIndex = 0; dir[dIndex] != '\0' && current != NULL; dIndex++) {
previous = current;
if (dir[dIndex] == 'L' || dir[dIndex] == 'l') {
current = current->leftchild;
} else if (dir[dIndex] == 'R' || dir[dIndex] == 'r') {
current = current->rightchild;
} else {
printf("Invalid direction. Use 'L' for left and 'R' for right.\n");
free(temp);
return;
}
}
if (current != NULL || dir[dIndex] != '\0') {
printf("Invalid position for insertion.\n");
free(temp);
return;
}
if (dir[dIndex - 1] == 'L' || dir[dIndex - 1] == 'l') {
previous->leftchild = temp;
} else {
previous->rightchild = temp;
}
}
}
}
bool isFull() {
return top == MAX;
}
bool isEmpty() {
return top == -1;
}
// Push nodes onto the stack
void Push(Tptr node) {
if (!isFull()) {
stack[++top] = node;
}
}
// Pop node from the stack
Tptr Pop() {
Tptr ret = NULL;
if (!isEmpty()) {
ret = stack[top--];
}
return ret;
}
Tptr peek() {
return stack[top];
}
// Print node elements in inorder
void IterativeInorder(Tptr node) {
while (true) {
while (node) { // Push all left children
Push(node);
node = node->leftchild;
}
node = Pop();
if (node == NULL) { // Stack is empty
break;
}
printf("%d ", node->data);
node = node->rightchild;
}
}
// Print node elements in preorder
void IterativePreorder(Tptr node) {
for (;;) {
for (; node; node = node->leftchild) {
Push(node);
printf("%d ", node->data);
}
node = Pop();
if (node == NULL) {
break;
}
node = node->rightchild;
}
}
// Print node elements in postorder
void IterativePostorder(Tptr node) {
while (true) {
if (node) { // Push all left children
Push(node);
node = node->leftchild;
} else {
if (!peek()) { // Stack is empty
break;
}
Tptr temp = peek()->rightchild;
if (temp == NULL) { // Right child doesn't exist
temp = Pop();
printf("%d ", temp->data);
while (!isEmpty() && temp == peek()->rightchild) { // Only when temp is the right child of the top
temp = Pop(); // Extra pop if it is the right child
printf("%d ", temp->data);
}
} else { // If right child of the top of stack exists, push it and its left children
node = temp;
}
}
}
}
// Return the parent of a given node
Tptr ParentNode(Tptr node, int target) {
if (!node) {
return NULL;
}
if ((node->leftchild && node->leftchild->data == target) || (node->rightchild && node->rightchild->data == target)) {
return node;
}
// Return left if not null, else return right
Tptr left = ParentNode(node->leftchild, target);
if (left != NULL) {
return left;
}
return ParentNode(node->rightchild, target);
}
// Count nodes
void countnodes(TNode* root, int *count) {
if (root) {
(*count)++;
countnodes(root->leftchild, count);
countnodes(root->rightchild, count);
}
}
int cntnodes(TNode* node) {
if (!node) {
return 0;
} else {
int right = cntnodes(node->rightchild);
int left = cntnodes(node->leftchild);
return left + right + 1;
}
}
// Find the depth of a given tree
int maxDepth(Tptr root) {
if (root == NULL) {
return 0;
} else {
int leftDepth = maxDepth(root->leftchild);
int rightDepth = maxDepth(root->rightchild);
return (leftDepth > rightDepth) ? (leftDepth + 1) : (rightDepth + 1);
}
}
// Print all the ancestors of a node
int printAncestors(Tptr root, int target) {
if (root == NULL) {
return 0;
}
if (root->data == target) {
return 1;
}
if (printAncestors(root->leftchild, target) || printAncestors(root->rightchild, target)) {
printf("%d ", root->data);
return 1;
}
return 0;
}
// Count the number of leaf nodes in a tree
int countLeafNodes(Tptr root) {
if (root == NULL) {
return 0;
}
if (root->leftchild == NULL && root->rightchild == NULL) {
return 1;
} else {
int leftLeafCount = countLeafNodes(root->leftchild);
int rightLeafCount = countLeafNodes(root->rightchild);
return leftLeafCount + rightLeafCount;
}
}
void cntleaf(Tptr root, int *count) {
if (root && (root->leftchild) == NULL && (root->rightchild) == NULL) {
(*count)++;
}
if (root) {
cntleaf(root->leftchild, count);
cntleaf(root->rightchild, count);
}
if (!root) {
return;
}
}
int main() {
int N;
root = NULL;
int choice, target;
int lcount = 0; // Move this declaration here
do {
printf("\nBinary Tree Operations\n");
printf("1. Create Tree\n");
printf("2. Inorder Traversal\n");
printf("3. Preorder Traversal\n");
printf("4. Postorder Traversal\n");
printf("5. Find Parent of a Node\n");
printf("6. Find Depth of the Tree\n");
printf("7. Print Ancestors of a Node\n");
printf("8. Count Leaf Nodes\n");
printf("9. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
// Create the binary tree
printf("Enter the number of nodes: ");
scanf("%d", &N);
createTree(N);
int count = cntnodes(root);
printf("Number of nodes: %d\n", count);
break;
case 2:
// Inorder Traversal
printf("Inorder Traversal: ");
IterativeInorder(root);
printf("\n"); // New line for clarity
break;
case 3:
// Preorder Traversal
printf("Preorder Traversal: ");
IterativePreorder(root);
printf("\n");
break;
case 4:
// Postorder Traversal
printf("Postorder Traversal: ");
IterativePostorder(root);
printf("\n");
break;
case 5:
// Find Parent of a Node
printf("Enter the value of the target node (integer): ");
scanf("%d", &target);
Tptr parent = ParentNode(root, target);
if (parent) {
printf("Parent of %d is %d\n", target, parent->data);
} else {
printf("Node not found\n");
}
break;
case 6:
// Find Depth of the Tree
printf("Depth of the tree is: %d\n", maxDepth(root));
break;
case 7:
// Print Ancestors of a Node
printf("Enter the value of the target node (integer): ");
scanf("%d", &target);
printf("Ancestors of %d: ", target);
if (!printAncestors(root, target)) {
printf("No ancestors found or node does not exist.\n");
}
printf("\n");
break;
case 8:
// Count Leaf Nodes
lcount = 0; // Reset count for each call
cntleaf(root, &lcount);
printf("Number of leaf nodes: %d\n", lcount);
break;
case 9:
// Exit
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 9);
return 0;
}

View File

@ -0,0 +1,79 @@
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *left;
struct Node *right;
} Node;
// Create Node for the Binary Tree
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Memory allocation error!\n");
exit(1);
}
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// Function to check if two trees are mirror images of each other
int areMirror(Node* tree1, Node* tree2) {
// both tree empty, mirror
if (tree1 == NULL && tree2 == NULL) {
return 1;
}
// one tree empty, not mirror
if (tree1 == NULL || tree2 == NULL) {
return 0;
}
// check recursively their subtrees
return (tree1->data == tree2->data) &&
areMirror(tree1->left, tree2->right) &&
areMirror(tree1->right, tree2->left);
}
// Function to print the tree in-order
void printTree(Node* root) {
if (root == NULL) {
return;
}
printTree(root->left);
printf("%d ", root->data);
printTree(root->right);
}
int main() {
Node *root1 = createNode(1);
root1->left = createNode(2);
root1->right = createNode(3);
root1->left->left = createNode(4);
root1->left->right = createNode(5); //6
Node *root2 = createNode(1);
root2->left = createNode(3);
root2->right = createNode(2);
root2->right->left = createNode(5);
root2->right->right = createNode(4);
printf("Tree 1 (In-Order): ");
printTree(root1);
printf("\n");
printf("Tree 2 (In-Order): ");
printTree(root2);
printf("\n");
if (areMirror(root1, root2)) {
printf("The trees are mirror images.\n");
} else {
printf("The trees are not mirror images.\n");
}
return 0;
}

View File

@ -0,0 +1,99 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 100
typedef struct TNode *Tptr;
typedef struct TNode
{
int data;
Tptr leftchild;
Tptr rightchild;
}TNode;
Tptr root;
int top = -1;
Tptr stack[MAX];
//Create nodes in the tree
Tptr createNode(int val)
{
Tptr temp =(Tptr)malloc(sizeof(TNode));
temp->data = val;
temp->leftchild = temp->rightchild = NULL;
return temp;
}
Tptr createRecursive(Tptr root,int value)
{
if (root == NULL) {
return createNode(value);
}
if (value < root->data) {
root->leftchild = createRecursive(root->leftchild, value);
} else if (value > root->data) {
root->rightchild = createRecursive(root->rightchild, value);
}
return root;
}
// Function for postorder Traversal of tree.
void printPostorder(Tptr root)
{
if (root) {
printPostorder(root->leftchild);
printPostorder(root->rightchild);
printf("%d ",root->data);
}
}
// Function for preorder Traversal of tree.
void printPreorder(Tptr root)
{
if (root) {
printf("%d ",root->data);
printPreorder(root->leftchild);
printPreorder(root->rightchild);
}
}
// Function for inorder Traversal of tree.
void printInorder(Tptr root)
{
if (root) {
printInorder(root->leftchild);
printf("%d ",root->data);
printInorder(root->rightchild);
}
}
int main() {
// Insert nodes into the binary tree
root = createRecursive(root,6);
root = createRecursive(root,4);
root = createRecursive(root,83);
root = createRecursive(root,25);
root = createRecursive(root,42);
// Print the binary tree using different tree traversal methods
printf("Binary Tree (In-Order): ");
printInorder(root);
printf("\n");
printf("Binary Tree (Pre-Order): ");
printPreorder(root);
printf("\n");
printf("Binary Tree (Post-Order): ");
printPostorder(root);
printf("\n");
return 0;
}

View File

@ -0,0 +1,60 @@
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *left;
struct Node *right;
} Node;
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Memory allocation error!\n");
exit(1);
}
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
Node* copyTree(Node* original) {
if (original == NULL) {
return NULL;
}
Node* newNode = createNode(original->data);
newNode->left = copyTree(original->left); // Recursively copy left subtree
newNode->right = copyTree(original->right); // Recursively copy right subtree
return newNode;
}
void printInorder(Node* root) {
if (root != NULL) {
printInorder(root->left);
printf("%d ", root->data);
printInorder(root->right);
}
}
int main() {
Node *originalRoot = createNode(1);
originalRoot->right = createNode(2);
originalRoot->left = createNode(3);
originalRoot->left->left= createNode(0);
originalRoot->right->left = createNode(4);
originalRoot->right->right = createNode(5);
printf("Original Tree (Inorder Traversal): ");
printInorder(originalRoot);
Node* copiedRoot = copyTree(originalRoot);
printf("\nCopied Tree (Inorder Traversal): ");
printInorder(copiedRoot);
printf("\n");
return 0;
}