0% found this document useful (0 votes)
16 views5 pages

Prac Lab Dsa

The document contains a C program that implements a binary tree data structure and various operations such as checking if a node is a root or leaf, finding a node's left and right children, and retrieving a node's parent. It includes functions for initializing the tree and performing an inorder traversal. The main function allows user input for the number of nodes and operations to be performed on the binary tree.

Uploaded by

kvmail801
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)
16 views5 pages

Prac Lab Dsa

The document contains a C program that implements a binary tree data structure and various operations such as checking if a node is a root or leaf, finding a node's left and right children, and retrieving a node's parent. It includes functions for initializing the tree and performing an inorder traversal. The main function allows user input for the number of nodes and operations to be performed on the binary tree.

Uploaded by

kvmail801
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/ 5

DSA PRACTICE LAB

A.Krishna vamshi
2024BCY0033

#include <stdio.h>
#include <string.h>

#define MAX_SIZE 100

typedef struct {
int tree[MAX_SIZE];
int size;
} BinaryTree;

void initTree(BinaryTree *bt, int arr[], int n) {


bt->size = n;
for (int i = 0; i < n; i++) {
bt->tree[i] = arr[i];
}
}

int isRoot(BinaryTree *bt, int x) {


if (bt->size > 0 && bt->tree[0] == x) {
return 1;
}
return -1;
}
int left(BinaryTree *bt, int x) {
for (int i = 0; i < bt->size; i++) {
if (bt->tree[i] == x) {
int leftIndex = 2 * i + 1;
if (leftIndex < bt->size) {
return bt->tree[leftIndex];
}
}
}
return -1;
}

int right(BinaryTree *bt, int x) {


for (int i = 0; i < bt->size; i++) {
if (bt->tree[i] == x) {
int rightIndex = 2 * i + 2;
if (rightIndex < bt->size) {
return bt->tree[rightIndex];
}
}
}
return -1;
}

int parent(BinaryTree *bt, int x) {


for (int i = 0; i < bt->size; i++) {
if (bt->tree[i] == x) {
if (i == 0) {
return -1;
}
int parentIndex = (i - 1) / 2;
return bt->tree[parentIndex];
}
}
return -1;
}

int isLeaf(BinaryTree *bt, int x) {


for (int i = 0; i < bt->size; i++) {
if (bt->tree[i] == x) {
int leftIndex = 2 * i + 1;
int rightIndex = 2 * i + 2;
if (leftIndex >= bt->size && rightIndex
>= bt->size) {
return 1;
}
}
}
return -1;
}

void inorderTraversal(BinaryTree *bt, int index)


{
if (index >= bt->size) {
return;
}
inorderTraversal(bt, 2 * index + 1);
printf("%d ", bt->tree[index]);
inorderTraversal(bt, 2 * index + 2);
}

int main() {
int n;
scanf("%d", &n);
int arr[MAX_SIZE];

for (int i = 0; i < n; i++) {


scanf("%d", &arr[i]);
}

BinaryTree bt;
initTree(&bt, arr, n);

int num_operations;
scanf("%d", &num_operations);

for (int i = 0; i < num_operations; i++) {


char operation[10];
int x;
scanf("%s", operation);

if (strcmp(operation, "inorder") == 0) {
inorderTraversal(&bt, 0);
printf("\n");
} else {
scanf("%d", &x);
if (strcmp(operation, "left") == 0) {
printf("%d\n", left(&bt, x));
} else if (strcmp(operation, "right") ==
0) {
printf("%d\n", right(&bt, x));
} else if (strcmp(operation, "isRoot")
== 0) {
printf("%d\n", isRoot(&bt, x));
} else if (strcmp(operation, "isLeaf")
== 0) {
printf("%d\n", isLeaf(&bt, x));
} else if (strcmp(operation, "parent")
== 0) {
printf("%d\n", parent(&bt, x));
}
}
}

return 0;
}

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