Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
27 views81 pages

Sujal Sahu Lab Externalfile

The document outlines a practical software lab curriculum for the Bachelor of Computer Applications program at St. Andrews Institute of Technology & Management for the 2024-2025 session. It includes various assignments focused on HTML and C++ programming, detailing tasks such as designing web pages, creating forms, and implementing data structures. Each task specifies the required elements and skills to be demonstrated, along with due dates for submission.

Uploaded by

khushiy239145
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)
27 views81 pages

Sujal Sahu Lab Externalfile

The document outlines a practical software lab curriculum for the Bachelor of Computer Applications program at St. Andrews Institute of Technology & Management for the 2024-2025 session. It includes various assignments focused on HTML and C++ programming, detailing tasks such as designing web pages, creating forms, and implementing data structures. Each task specifies the required elements and skills to be demonstrated, along with due dates for submission.

Uploaded by

khushiy239145
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/ 81

ST.

ANDREWS INSTITUTE
OF TECHNOLOGY & MANAGEMENT
Gurugram Delhi (NCR)
Approved by AICTE, Govt. of India, New Delhi
Affiliated to Maharshi Dayanand University
+
‘A ’ Grade State University, accredited by NAAC

Session: 2024 – 2025

Practical software Lab–Based on paper BCA 206 & 208, i.e.HTML and C++
Programming

Course Code: BCA-210

Semester: 4th

Bachelor of Computer Applications


Submitted By
Name: Khushi
Roll No.: 239145

DEPARTMENT OF
COMPUTER APPLICATIONS
St. Andrews Institute of Technology & Management,
Gurugram
Department of Computer Applications
Practical Lab Evaluation Sheet
P Practical Date CO Pract Viv Atten Pract Ove
r ical aVoce dan ce ica l ra
o Perfo (04) (04) File ll
g rm (04)
r (2
ed
a 0)
(08)
m
N
o.

Design an HTML webpage that


showcases the use of all formatting tags
in HTML, along with CSS styling.
The webpage should
include:

- Headings (h1-h6)
- Paragraphs (p)
- Bold and italic text (b, i, strong, em)
- Lists (ul, ol, li)
- Links (a)
Apply CSS styles to enhance the
layout, typography, and visual
appearance of the webpage. Use
1 CSS selectors etc. 17/02/2025 CO3

2 Design a web page to demonstrate your

college name aligned with the logo of


your college. 24/02/2025 CO3

3 Design a web page which asks for 11/03/2025 CO3


mode of payment which includes the
options: Credit card/Debit card/Online
transfer

(use radio buttons).

4 Design an HTML webpage that


displays your personal bio-data using a
table tag. The table should include the
following information:

- Name
- Contact Information
(Phone Number and Email)
- Education (Degree, Institution,
and Year of Graduation)
Use the concepts of rowspan and

colspan to merge cells and create a


visually appealing table structure.
18/03/2025 CO4
Design an HTML webpage that
demonstrates the use of frames. Create
a frameset that includes:

- A top frame for a header or


navigation menu
- A left frame for a sidebar or menu
- A main frame for displaying
content - A bottom frame for a
footer or copyright information

Use HTML frame tags to create the


frameset and specify the source files
for each frame.

5 01/04/2025 CO4
a) Demonstrate use of cout and
cin to print a first n term of
Fibonacci series.
b) Write a C ++ program to create
your own namespace and swap
values two variables.
c) Demonstrate string operations
in
6 C++ 17/02/2025 CO1

7 a) Demonstrate all types of


constructors in a C++ program.
24/02/2025 CO2
b) Create Static data members in a C++
program and demonstrate how they are
different form normal data members
c) Write a C++ program to demonstrate
use of new and delete operator
8 a) Write C++ Program that 11/03/2025 CO3
illustrate how the Single, Multiple,
Multilevel and Hierarchical
Inheritance are supported.
b) Write a C++ program to
overload member function area() and
demonstrate function overloading c)
Demonstrate the effect of virtual
functions in C++

9 Write a C++ program to Demonstrate 18/03/2025 CO4


Class template and Function template

10 Write a Program containing a possible 01/04/2025 CO4


exception. use a try block to through it
under catch block to handle it properly.

11 Write a program to create header node 17/02/2025 CO1


of any tree.

12 Write a program to create binary search 24/02/2025 CO1


tree.

13 Write a program to create AVL tree. 11/03/2025 CO1

14 Write a program to create M-Way tree. 18/03/2025 CO1

15 Write a program to perform Warshall 01/04/2025 CO2


Algorithm.

Average Marks
WEB DESIGNING
Q1. Design an HTML webpage that showcases the use of all formatting tags in HTML, along with CSS styling.
The webpage should include:

- Headings (h1-h6)
- Paragraphs (p)
- Bold and italic text (b, i, strong, em)
- Lists (ul, ol, li)
- Links (a)
Apply CSS styles to enhance the layout, typography, and visual appearance of the

webpage. Use CSS selectors etc.

Ans:- <!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>HTML Formatting Tags Showcase</title>

<style> body { font-

family: Arial, sans-serif; line-

height: 1.6; margin: 0;

padding: 20px; background-

color: #f4f4f4;

} h1

{ color: #333;

text-align: center; }

h2, h3, h4, h5, h6 {

color: #555;

p{

margin: 15px 0;

font-size: 1.1em;

b, strong

{ color:

#d9534f;
} i, em {

color: #5bc0de;

} ul, ol

{ margin: 20px 0;

padding-left: 20px;

a{

color: #5cb85c; text-

decoration: none;

a:hover { text-

decoration: underline;

.container

{ max-width: 800px;

margin: auto;

background: white;

padding: 20px;

border-radius: 8px;

box-shadow: 0 2px 10px

rgba(0, 0, 0, 0.1);

</style>

</head>

<body>

<div class="container">

<h1>HTML Formatting Tags Showcase</h1>


<h2>Headings</h2>

<h3>This is a Level 3 Heading</h3>

<h4>This is a Level 4 Heading</h4>

<h5>This is a Level 5 Heading</h5>

<h6>This is a Level 6 Heading</h6>

<h2>Paragraphs</h2>

<p>This is a <strong>bold</strong> paragraph that demonstrates the use of <b>bold</b> and <i>italic</i>
text. You can also use <em>emphasis</em> to highlight important points.</p>

<p>Another paragraph can be added here to show how multiple paragraphs look on the page.</p>

<h2>Lists</h2>

<h3>Unordered List</h3>

<ul>

<li>Item 1</li>

<li>Item 2</li>

<li>Item 3</li>

</ul>

<h3>Ordered List</h3>

<ol>

<li>First Item</li>

<li>Second Item</li>

<li>Third Item</li>

</ol>

<h2>Links</h2>
<p>For more information, visit <a href="https://www.w3schools.com" target="_blank">W3Schools</a> for
tutorials and references on web development.</p>

</div>

</body>

</html>
OUTPUT
Q2. Design a web page to demonstrate your college name aligned with the logo of your college.

Ans:- <!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>My College</title>

<style> body { font-

family: Arial, sans-serif;

background-color: #f4f4f4;

margin: 0; padding: 20px;

display: flex; justify-content:

center; align-items: center;

height: 100vh;

.container { text-align: center;

background: white; padding: 20px;

border-radius: 8px; box-shadow: 0 2px

10px rgba(0, 0, 0, 0.1);

h1 { color:

#333; margin:

20px 0;

} img { max-width: 150px; /* Adjust the

size of the logo */ height: auto; margin-

bottom: 20px;

}
</style>

</head>

<body>

<div class="container">

<img src="image/q2.jpeg" alt="College Logo" />

<h1>St. Andrews Institute of Technology and Management</h1>

</div>

</body>

</html>

OUTPUT
Q3. Design a web page which asks for mode of payment which includes the options: Credit card/Debit
card/Online transfer

(use radio buttons).


Ans:- <!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Payment Mode Selection</title>

<style> body { font-

family: Arial, sans-serif;

background-color: #f4f4f4;

margin: 0; padding: 20px;

display: flex; justify-content:

center; align-items: center;

height: 100vh;

.container { background: white;

padding: 20px; border-radius: 8px;

box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);

text-align: center;

width: 300px;

} h1 { color:

#333; margin-bottom:

20px;

} label

{ display: block;
margin: 10px 0;

font-size: 1.1em;

input[type="radio"]

{ margin-right: 10px;

button { margin-top: 20px; padding: 10px

15px; font-size: 1em; background-color:

#5cb85c; /* Bootstrap success color */

color: white;

border: none;

border-radius: 5px;

cursor: pointer;

button:hover { background-color: #4cae4c; /*

Darker shade on hover */

</style>

</head>

<body>

<div class="container">

<h1>Select Payment Mode</h1>

<form>

<label>

<input type="radio" name="payment" value="credit_card" required>

Credit Card

</label>
<label>

<input type="radio" name="payment" value="debit_card">

Debit Card

</label>

<label>

<input type="radio" name="payment" value="online_transfer">

Online Transfer

</label>

<button type="submit">Submit</button>

</form>

</div>

</body>

</html>
OUTPUT
Q4. Design an HTML webpage that displays your personal bio-data using a table tag. The table should include
the following information:

- Name
- Contact Information (Phone Number and Email)
- Education (Degree, Institution, and Year of Graduation)
Use the concepts of rowspan and colspan to merge cells and create a visually appealing table structure.

Ans:- <!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8" />

<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<title>Personal Bio-Data</title>

<style> body { font-

family: Arial, sans-serif;

background-color: #f4f4f4;

margin: 0; padding: 20px;

display: flex; justify-content:

center; align-items: center;

height: 100vh;

.container { background: white;

border-radius: 10px; box-shadow: 0 4px

20px rgba(0, 0, 0, 0.1); padding: 20px;

width: 400px;

} h1 { text-

align: center;

color: #333;

}
table { width: 100%;

border-collapse: collapse;

margin-top: 20px;

th,

td {

border: 1px solid #ddd;

padding: 10px; text-

align: left;

th {

background-color: #007bff;

color: white;

tr:nth-child(even) { background-

color: #f2f2f2;

tr:hover { background-

color: #ddd; }

</style>

</head>

<body>

<div class="container">

<h1>Personal Bio-Data</h1>

<table>

<tr>
<th colspan="2">Personal Information</th>

</tr>

<tr>

<td>Name</td>

<td>Khushi</td>

</tr>

<tr>

<td rowspan="2">Contact Information</td>

<td>Phone Number: 9876512304</td>

</tr>

<tr>

<td>Email: [email protected]</td>

</tr>

<tr>

<th colspan="2">Education</th>

</tr>

<tr>

<td>Degree</td>

<td>Bachelor of Computer Application</td> </tr>

<tr>

<td>Institution</td>

<td>Maharishi Dayanand University</td>

</tr>

<tr>

<td>Year of Graduation</td>

<td>2026</td>

</tr>
</table>

</div>

</body>

</html>
Output:

Q5. Design an HTML webpage that demonstrates the use of frames. Create a frameset that includes:
- A top frame for a header or navigation menu
- A left frame for a sidebar or menu
- A main frame for displaying content
- A bottom frame for a footer or copyright information

Use HTML frame tags to create the frameset and specify the source files for each frame.

Ans- Index.html

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Frameset Example</title>

</head>

<frameset rows="15%, 75%, 10%">

<frame src="header.html" name="topFrame">

<frameset cols="20%, 80%">

<frame src="sidebar.html" name="leftFrame">

<frame src="content.html" name="mainFrame">

</frameset>

<frame src="footer.html" name="bottomFrame">

</frameset>

<noframes>

<body>Your browser does not support frames.</body>

</noframes>

</html>

header.html
<!DOCTYPE html>

<html lang="en">

<head><title>Header</title></head>

<body style="background-color: #4CAF50; color: white; text-align: center;">

<h1>My Website Header</h1>

</body>

</html>

Sidebar.html

<!DOCTYPE html>

<html lang="en">

<head><title>Sidebar</title></head>

<style>

li a{

text-decoration: none;

color: black;

font-size: 23px;

</style>

<body style="background-color: #f0f0f0;">

<h3 style="font-size: 30px;">Menu</h3>

<ul>

<li><a href="content.html" target="mainFrame">Home</a></li>

<li><a href="about.html" target="mainFrame">About</a></li>

<li><a href="services.html" target="mainFrame">Services</a></li>

</ul>

</body>

</html>
content.html

<!DOCTYPE html>

<html lang="en">

<head><title>Content</title></head>

<body>

<h2>Welcome to the Main Content Area!</h2>

<p>This is where the page content will be displayed when you click links from the sidebar.</p>

</body>

</html>

footer.html

<!DOCTYPE html>

<html lang="en">

<head><title>Footer</title></head>

<body style="background-color: #4CAF50; color: white; text-align: center;">

<p>&copy; 2025 My Website. All rights reserved.</p>

</body>

</html>
OUTPUT
OOPS
Q6. A) Demonstrate use of cout and cin to print a first n term of Fibonacci series.

Ans:- #include <iostream>

using namespace std;

int main() {

int n;

cout << "Enter the number of terms in the Fibonacci series: ";

cin >> n;

int first = 0, second = 1;

cout << "Fibonacci Series: ";

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

if (i == 0) { cout <<

first << " "; } else if (i ==

1) { cout << second <<

" ";

} else { int next =

first + second; cout <<

next << " "; first =

second; second = next;

}
}

cout << endl;

return 0; }
OUTPUT
Q 6. B) Write a C ++ program to create your own namespace and swap values two variables.

Ans:- #include <iostream>

using namespace std;

namespace MyNamespace {

void swap(int &a, int &b) {

int temp = a; a = b;

b = temp;

int main() {

int x, y;

cout << "Enter two integers to swap: "; cin >> x >> y; cout

<< "Before swapping: x = " << x << ", y = " << y << endl;

MyNamespace::swap(x, y);

cout << "After swapping: x = " << x << ", y = " << y << endl;

return 0;

}
OUTPUT
Q 6. C) Demonstrate string operations in C++.
Ans:-

1. Creating and initializing strings.


2. Concatenating strings.
3. Finding substrings.
4. Modifying strings (inserting, erasing).
5. Comparing strings.
6. Getting the length of a string.

CODE
#include <iostream>

#include <string>

using namespace std;

int main() {

// 1. Creating and initializing strings

string str1 = "Hello"; string str2 =

"World";

// 2. Concatenating strings string str3 = str1 +

" " + str2; cout << "Concatenated String: " <<

str3 << endl;

// 3. Finding substrings size_t pos =

str3.find("World"); if (pos != string::npos) { cout

<< "'World' found at position: " << pos << endl;

} else { cout << "'World' not

found." << endl; }


// 4. Modifying strings str3.insert(5,

","); cout << "After Insertion: " << str3 <<

endl;

str3.erase(5, 1); cout << "After Erasing:

" << str3 << endl; // 5. Comparing strings

string str4 = "Hello World"; if (str3 ==

str4) { cout << "str3 is equal to str4."

<< endl;

} else { cout << "str3 is not equal to

str4." << endl;

// 6. Getting the length of a string cout <<

"Length of str3: " << str3.length() << endl;

// 7. Converting to uppercase (using a loop)

string upperStr = str3; for (char &c :

upperStr) { c = toupper(c);

cout << "Uppercase String: " << upperStr << endl;

return 0;

}
OUTPUT
Q 7. A) Demonstrate all types of constructors in a C++ program.

Ans:-
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
4. Move Constructor

CODE
#include <iostream>

#include <cstring>

using namespace std;

class MyClass {

private:

char* name;

int age;

public:

// 1. Default Constructor

MyClass() { name = new

char[20]; strcpy(name,

"Unknown");

age = 0; cout << "Default Constructor called: " << name << ", Age: " <<

age << endl;

// 2. Parameterized Constructor

MyClass(const char* n, int a) {


name = new char[strlen(n) + 1];

strcpy(name, n); age = a;

cout << "Parameterized Constructor called: " << name << ", Age: " << age << endl;

// 3. Copy Constructor MyClass(const MyClass& obj) { name = new

char[strlen(obj.name) + 1]; strcpy(name, obj.name); age = obj.age;

cout << "Copy Constructor called: " << name << ", Age: " << age << endl;

// 4. Move Constructor (C++11 and later) MyClass(MyClass&& obj)

noexcept { name = obj.name; age = obj.age; obj.name = nullptr;

obj.age = 0; cout << "Move Constructor called: " << name << ", Age: " <<

age << endl;

~MyClass() { cout << "Destructor called for: " << (name ? name :

"Unknown") << endl; delete[] name;

void display() const { cout << "Name: " << name <<

", Age: " << age << endl;

};
int main() {

// 1. Using Default Constructor

MyClass obj1;

// 2. Using Parameterized Constructor

MyClass obj2("Alice", 30);

// 3. Using Copy Constructor

MyClass obj3 = obj2;

// 4. Using Move Constructor

MyClass obj4 = std::move(obj2);

// Displaying information

obj1.display();

obj3.display();

obj4.display(); return 0;

}
OUTPUT
Q 7. B) Create Static data members in a C++ program and demonstrate how they are different form
normal data members.

Ans:-

Key Differences:

1. Memory Allocation: Static data members are allocated memory only once, while normal data members are
allocated memory for each object.
2. Access: Static data members can be accessed without creating an instance of the class, while normal data
members require an instance.
3. Lifetime: Static data members exist for the lifetime of the program, while normal data members exist for
the lifetime of the object.

CODE
#include <iostream>

using namespace std;

class MyClass

{ private:

int normalData;

static int staticData;

public:

MyClass(int value) : normalData(value)

{ staticData++;

void display() const { cout << "Normal Data: " << normalData << ", Static Data:

" << staticData << endl;

}
static int getStaticData()

{ return staticData;

};

int MyClass::staticData = 0;

int main() {

MyClass obj1(10);

MyClass obj2(20);

obj1.display();

obj2.display();

cout << "Static Data accessed through class: " << MyClass::getStaticData() << endl;

return 0;

}
OUTPUT
Q 7. C) Write a C++ program to demonstrate use of new and delete operator

Ans:- #include <iostream> using namespace std;

class MyClass {

private: int

data;

public:

MyClass(int value) : data(value) { cout <<

"Constructor called for data: " << data << endl;

~MyClass() { cout << "Destructor called for data: "

<< data << endl;

void display() const { cout <<

"Data: " << data << endl;

};

int main() {

MyClass* obj1 = new MyClass(10); obj1-

>display();
MyClass* objArray = new MyClass[3] { MyClass(20), MyClass(30), MyClass(40) };

for (int i = 0; i < 3; ++i)

{ objArray[i].display();

delete obj1;

delete[] objArray;

return 0;

}
OUTPUT
Q8. A) Write C++ Program that illustrate how the Single, Multiple, Multilevel and Hierarchical Inheritance are
supported.

Ans:- #include <iostream>

using namespace std;

// Base class for Single Inheritance

class Animal { public:

void eat() { cout << "Animal is

eating." << endl;

};

// Derived class for Single Inheritance

class Dog : public Animal { public:

void bark() { cout << "Dog is

barking." << endl;

};

// Base class for Multiple Inheritance

class Bird { public:

void fly() { cout << "Bird is

flying." << endl;

};
// Derived class for Multiple Inheritance

class Bat : public Animal, public Bird

{ public:

void screech() { cout << "Bat is

screeching." << endl;

};

// Base class for Multilevel Inheritance

class Vehicle { public:

void start() { cout << "Vehicle is

starting." << endl;

};

// Derived class for Multilevel Inheritance

class Car : public Vehicle { public:

void drive() { cout << "Car is

driving." << endl;

};

// Further derived class for Multilevel Inheritance class SportsCar : public Car { public:

void race() { cout << "SportsCar is

racing." << endl;

};
// Base class for Hierarchical Inheritance

class Shape { public:

void draw() { cout << "Drawing a

shape." << endl;

};

// Derived class for Hierarchical Inheritance

class Circle : public Shape { public:

void draw() { cout << "Drawing a

circle." << endl;

};

// Another derived class for Hierarchical Inheritance

class Square : public Shape { public:

void draw() {

cout << "Drawing a square." << endl;

};

int main() {

// Single Inheritance

Dog dog; dog.eat();

dog.bark();

// Multiple Inheritance

Bat bat; bat.eat();


bat.fly();

bat.screech();

// Multilevel Inheritance

SportsCar sportsCar;

sportsCar.start();

sportsCar.drive();

sportsCar.race();

// Hierarchical Inheritance

Circle circle;

circle.draw(); Square

square; square.draw();

return 0;

}
OUTPUT
Q8. B) Write a C++ program to overload member function area() and demonstrate function overloading.

Ans:- #include <iostream>

#include <cmath> using

namespace std;

class Shape

{ public:

double area(double length, double width)

{ return length * width;

double area(double radius)

{ return M_PI * radius * radius;

};

int main() { Shape shape; double rectangleLength = 5.0; double

rectangleWidth = 3.0; double rectangleArea =

shape.area(rectangleLength, rectangleWidth); cout << "Area of

Rectangle: " << rectangleArea << endl;

double circleRadius = 4.0; double circleArea

= shape.area(circleRadius); cout << "Area of

Circle: " << circleArea << endl;


return 0;

}
OUTPUT
Q8. C) Demonstrate the effect of virtual functions in C++

Ans:-

Key Concepts:

1. Virtual Function: A member function in a base class that you expect to override in derived classes.
2. Dynamic Binding: The mechanism by which a call to an overridden function is resolved at runtime rather
than compile time.
3. Base Class Pointer: A pointer of the base class type can point to objects of derived classes.

CODE
#include <iostream>

using namespace std;

class Base

{ public:

virtual void show() { cout << "Base class show

function called." << endl;

void display() { cout << "Base class display

function called." << endl;

};

class Derived : public Base {


public:

void show() override { cout << "Derived class show

function called." << endl;

void display() { cout << "Derived class display

function called." << endl;

};

int main() {

Base* basePtr;

Derived derivedObj;

basePtr = &derivedObj;

basePtr->show();

basePtr->display();

return 0;

}
OUTPUT
Q9. Write a C++ program to Demonstrate Class template and Function template

Ans:- #include <iostream> using namespace std;

// Class Template

template <typename T>

class Box

{ private:

T length;

public:

Box(T l) : length(l) {}

T volume() { return length *

length * length;

};

// Function Template

template <typename T>

T add(T a, T b)

{ return a + b;

int main() {

// Using Class Template Box<int> intBox(5); cout <<

"Volume of intBox: " << intBox.volume() << endl;


Box<double> doubleBox(5.5); cout << "Volume of doubleBox: "

<< doubleBox.volume() << endl;

// Using Function Template int intResult =

add<int>(10, 20); cout << "Sum of integers: " <<

intResult << endl;

double doubleResult = add<double>(10.5, 20.5);

cout << "Sum of doubles: " << doubleResult << endl;

return 0;

}
OUTPUT
Q10. Write a Program containing a possible exception. use a try block to through it under catch block to handle it
properly.

Ans:- #include <iostream>

#include <stdexcept> using

namespace std;

// Function to perform division double divide(double

numerator, double denominator) { if (denominator == 0) {

throw runtime_error("Division by zero is not allowed.");

return numerator / denominator;

int main() { double

num1, num2;

cout << "Enter numerator: ";

cin >> num1; cout << "Enter

denominator: "; cin >> num2;

try {

// Attempt to divide double

result = divide(num1, num2); cout

<< "Result: " << result << endl;

} catch (const runtime_error& e) {

cout << "Error: " << e.what() << endl;


}

return 0;

}
OUTPUT
DATA STRUCTURE
Q 11. Write a program to create header node of any tree.

Ans:- #include <stdio.h>

#include <stdlib.h> struct

TreeNode {

int data; struct

TreeNode* left; struct

TreeNode* right;

};

struct HeaderNode

{ struct TreeNode* root;

};

struct TreeNode* createTreeNode(int data) { struct TreeNode* newNode = (struct

TreeNode*)malloc(sizeof(struct TreeNode)); newNode->data = data; newNode-

>left = NULL; newNode->right = NULL; return newNode;

struct HeaderNode* createHeaderNode() { struct HeaderNode* header = (struct

HeaderNode*)malloc(sizeof(struct HeaderNode)); header->root = NULL; return

header;

}
struct TreeNode* insert(struct TreeNode* node, int data) {

if (node == NULL) { return createTreeNode(data);

if (data < node->data) { node->left

= insert(node->left, data);

} else { node->right = insert(node-

>right, data);

return node;

void inOrderTraversal(struct TreeNode* node) {

if (node != NULL) { inOrderTraversal(node-

>left); printf("%d ", node->data);

inOrderTraversal(node->right);

int main() { // Create a header node struct

HeaderNode* header = createHeaderNode();

header->root = insert(header->root, 50);

insert(header->root, 30); insert(header->root, 70);

insert(header->root, 20); insert(header->root, 40);

insert(header->root, 60); insert(header->root, 80);


printf("In-order traversal of the binary tree: ");

inOrderTraversal(header->root); printf("\n");

free(header);

return 0;

}
OUTPUT
Q 12. Write a program to create binary search tree.

Ans:- #include <stdio.h>

#include <stdlib.h> struct

TreeNode {

int data; struct

TreeNode* left; struct

TreeNode* right;

};

struct TreeNode* createTreeNode(int data) { struct TreeNode* newNode = (struct

TreeNode*)malloc(sizeof(struct TreeNode)); newNode->data = data; newNode-

>left = NULL; newNode->right = NULL; return newNode;

struct TreeNode* insert(struct TreeNode* node, int data) {

if (node == NULL) { return

createTreeNode(data);

if (data < node->data) { node->left =

insert(node->left, data); } else if (data >


node->data) { node->right =

insert(node->right, data);

return node;

void inOrderTraversal(struct TreeNode* node) {

if (node != NULL) { inOrderTraversal(node-

>left); printf("%d ", node->data);

inOrderTraversal(node->right);

struct TreeNode* search(struct TreeNode* root, int key) {

if (root == NULL || root->data == key)

{ return root;

if (key > root->data) { return

search(root->right, key);

}
return search(root->left, key);

int main() { struct TreeNode*

root = NULL;

root = insert(root, 50); insert(root, 30);

insert(root, 70); insert(root, 20); insert(root, 40);

insert(root, 60); insert(root, 80); printf("In-order

traversal of the binary search tree: ");

inOrderTraversal(root); printf("\n");

int key = 40; struct TreeNode* result = search(root, key);

if (result != NULL) { printf("Value %d found in the binary

search tree.\n", key);

} else {

printf("Value %d not found in the binary search tree.\n", key);

return 0;

}
OUTPUT
Q 13. Write a program to create AVL tree.

Ans:- #include <stdio.h>

#include <stdlib.h>

struct TreeNode {

int data; struct

TreeNode* left; struct

TreeNode* right; int

height;

};

int getHeight(struct TreeNode* node) {

if (node == NULL) { return 0;

return node->height;

int getBalance(struct TreeNode* node) {

if (node == NULL) { return 0;

return getHeight(node->left) - getHeight(node->right);

struct TreeNode* createTreeNode(int data) { struct TreeNode* newNode = (struct

TreeNode*)malloc(sizeof(struct TreeNode)); newNode->data = data; newNode-

>left = NULL; newNode->right = NULL; newNode->height = 1; return

newNode;
}

struct TreeNode* rightRotate(struct TreeNode* y) { struct TreeNode* x = y->left; struct

TreeNode* T2 = x->right; x->right = y; y->left = T2; y->height = 1 + (getHeight(y->left) >

getHeight(y->right) ? getHeight(y->left) : getHeight(y->right)); x->height = 1 + (getHeight(x->left) >

getHeight(x->right) ? getHeight(x->left) : getHeight(x->right)); return x;

struct TreeNode* leftRotate(struct TreeNode* x) { struct TreeNode* y = x->right; struct

TreeNode* T2 = y->left; y->left = x; x->right = T2; x->height = 1 + (getHeight(x->left) >

getHeight(x->right) ? getHeight(x->left) : getHeight(x->right)); y->height = 1 + (getHeight(y->left) >

getHeight(y->right) ? getHeight(y->left) : getHeight(y->right)); return y;

struct TreeNode* insert(struct TreeNode* node, int data) {

if (node == NULL) { return createTreeNode(data);

if (data < node->data) { node->left

= insert(node->left, data);

} else if (data > node->data) { node-

>right = insert(node->right, data);

} else

{ return

node;

node->height = 1 + (getHeight(node->left) > getHeight(node->right) ? getHeight(node->left) : getHeight(node-


>right)); int balance = getBalance(node); if

(balance > 1 && data < node->left->data) {

return rightRotate(node);

if (balance < -1 && data > node->right->data)

{ return leftRotate(node);

if (balance > 1 && data > node->left->data) {

node->left = leftRotate(node->left); return

rightRotate(node);

if (balance < -1 && data < node->right->data) {

node->right = rightRotate(node->right);

return leftRotate(node);

return node;

void inOrderTraversal(struct TreeNode* node) {

if (node != NULL) { inOrderTraversal(node-

>left); printf("%d ", node->data);

inOrderTraversal(node->right);

}
int main() { struct TreeNode* root = NULL;

root = insert(root, 30); root = insert(root,

20); root = insert(root, 40); root =

insert(root, 10); root = insert(root, 25);

root = insert(root, 35); root = insert(root,

50); printf("In-order traversal of the AVL

tree: "); inOrderTraversal(root); printf("\

n"); return 0;

}
OUTPUT
Q14. Write a program to create M-Way tree.

Ans:- #include <stdio.h>

#include <stdlib.h>

#define M 3

struct MWayNode {

int keys[M - 1]; struct

MWayNode* children[M]; int

numKeys;

};

struct MWayNode* createNode(int key) { struct MWayNode* newNode = (struct

MWayNode*)malloc(sizeof(struct MWayNode)); newNode->keys[0] = key; newNode-

>numKeys = 1;

for (int i = 0; i < M; i++) { newNode-

>children[i] = NULL;

return newNode;

void insert(struct MWayNode** root, int key) {

if (*root == NULL) { *root =

createNode(key);

return;

}
struct MWayNode* current = *root;

while (1) {

int i;

for (i = 0; i < current->numKeys; i++) {

if (key < current->keys[i]) { break;

if (current->children[i] == NULL) { if

(current->numKeys < M - 1) { for (int j =

current->numKeys; j > i; j--) { current-

>keys[j] = current->keys[j - 1];

current->keys[i] = key; current-

>numKeys++;

return;

} else {

printf("Node is full, splitting not implemented.\n");

return;

current = current->children[i];

}
void levelOrderTraversal(struct MWayNode* root) {

if (root == NULL) return;

struct MWayNode* queue[100];

int front = 0, rear = 0;

queue[rear++] = root;

while (front < rear) { struct MWayNode*

current = queue[front++]; for (int i = 0; i <

current->numKeys; i++) { printf("%d ",

current->keys[i]);

for (int i = 0; i <= current->numKeys; i++) {

if (current->children[i] != NULL)

{ queue[rear++] = current->children[i];

int main() { struct MWayNode*

root = NULL; insert(&root, 10);

insert(&root, 20); insert(&root,

5); insert(&root, 15);

insert(&root, 25); insert(&root,

30);
printf("Level-order traversal of the M-way tree: ");

levelOrderTraversal(root); printf("\n");

return 0;

}
OUTPUT
Q 15. Write a program to perform Warshall Algorithm.

Ans:- #include <stdio.h>

#define MAX 10

void warshall(int graph[MAX][MAX], int n)

{ int reach[MAX][MAX];

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

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

reach[i][j] = graph[i][j];

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

for (int j = 0; j < n; j++) { reach[i][j] = reach[i][j] ||

(reach[i][k] && reach[k][j]);

printf("Transitive Closure Matrix:\n");

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

0; j < n; j++) { printf("%d ",

reach[i][j]);

printf("\n");

}
}

int main() {

int n;

int graph[MAX][MAX];

printf("Enter the number of vertices: ");

scanf("%d", &n); printf("Enter the adjacency

matrix (0 or 1):\n");

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

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

scanf("%d", &graph[i][j]);

warshall(graph, n);

return 0;

}
OUTPUT

You might also like