Final Dsa Report
Final Dsa Report
Final Dsa Report
1
BINARY TREE
Each node has at most two child nodes (left and right).
The left subtree of a node contains only nodes with values less than
the node's value.
The right subtree of a node contains only nodes with values greater
than the node's value.
The left and right subtrees must also be binary trees.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
struct node* right;
2
};
// Function to create a new node
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;
}
// Function to insert a node in the binary tree
struct node* insert(struct node* root, int data) {
if (root == NULL) {
return newNode(data);
}
if (data < root->data) {
root->left = insert(root->left, data);
} else if (data > root->data) {
root->right = insert(root->right, data);
}
return root;
}
// Function to perform in-order traversal
void inorder(struct node* root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}
int main() {
struct node* root = NULL;
root = insert(root, 10);
root = insert(root, 5);
root = insert(root, 15);
root = insert(root, 3);
root = insert(root, 7);
root = insert(root, 12);
root = insert(root, 20);
printf("In-order traversal: ");
inorder(root);
printf("\n");
return 0;
}
3
Output
In-order traversal: 3 5 7 10 12 15 20
The space complexity for binary trees is O(n), as the maximum number of
nodes that can be stored in the tree is n.
4
BINARY SEARCH TREE
The left subtree of a node contains only nodes with values less than
the node's value.
The right subtree of a node contains only nodes with values greater
than the node's value.
Both the left and right subtrees must also be binary search trees.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
struct node* right;
};
5
// Function to create a new node
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;
}
// Function to insert a node in the binary search tree
struct node* insert(struct node* root, int data) {
if (root == NULL) {
return newNode(data);
}
if (data < root->data) {
root->left = insert(root->left, data);
} else if (data > root->data) {
root->right = insert(root->right, data);
}
return root;
}
// Function to perform in-order traversal
void inorder(struct node* root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}
int main() {
struct node* root = NULL;
root = insert(root, 10);
root = insert(root, 5);
root = insert(root, 15);
root = insert(root, 3);
root = insert(root, 7);
root = insert(root, 12);
root = insert(root, 20);
printf("In-order traversal: ");
inorder(root);
printf("\n");
return 0;
}
6
Output
In-order traversal: 3 5 7 10 12 15 20
The space complexity for binary search trees is O(n), as the maximum
number of nodes that can be stored in the tree is n.
7
AVL TREE
Insertion: Adding a new node to the tree while maintaining the AVL
tree properties.
Deletion: Removing a node from the tree while maintaining the AVL
tree properties.
Traversal: Visiting all the nodes in the tree in a specific order (in-
order, pre-order, post-order).
Search: Finding a specific node in the tree.
To maintain the AVL property, the tree may need to perform rotations (left
rotation, right rotation, left-right rotation, and right-left rotation) during
insertion and deletion operations.
8
Here's a sample C program that implements the basic operations
on an AVL tree:
#include <stdio.h>
#include <stdlib.h>
y->left = x;
x->right = T2;
9
return y;
}
x->right = y;
y->left = T2;
return x;
}
int main() {
struct node* root = NULL;
root = insert(root, 10);
root = insert(root, 20);
root = insert(root, 30);
root = insert(root, 40);
root = insert(root, 50);
root = insert(root, 25);
return 0;
}
Output
In-order traversal: 3 5 7 10 12 15 20
10
Time and Space Complexity
The time and space complexity of the basic operations on AVL trees are as
follows:
The space complexity for AVL trees is O(n), as the maximum number of
nodes that can be stored in the tree is n.
11
CONCLUSION
Binary trees, binary search trees, and AVL trees are fundamental data
structures in computer science and engineering. They provide efficient
solutions for various problems, such as searching, sorting, and managing
hierarchical data. Understanding the basic concepts, algorithms, and
applications of these tree data structures is crucial for students and
professionals in the field of computer science and engineering.
12
REFERENCE
https://www.w3schools.com/dsa/dsa_data_binarytrees.php
https://www.geeksforgeeks.org/binary-tree-data-structure/
https://www.programiz.com/dsa/avl-tree
13