Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views13 pages

Final Dsa Report

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 13

INTRODUCTION

Data Structures and Algorithms (DSA) form the cornerstone of computer


science and software engineering education. Among the vast array of data
structures studied in DSA, tree structures stand out as particularly
powerful and versatile. Trees are hierarchical data structures that provide
efficient solutions to a wide range of computational problems, from
organizing and searching data to modelling real-world hierarchical
relationships.

This report focuses on three fundamental tree structures: binary trees,


binary search trees (BSTs), and AVL trees. These structures build upon
each other, each adding specific properties and capabilities that enhance
their utility in various applications.

Understanding these tree structures is crucial for several reasons:

 Efficiency: Tree structures, particularly balanced trees like AVL


trees, offer logarithmic time complexity for many operations,
making them highly efficient for large datasets.

 Versatility: Trees can model a wide variety of real-world structures


and relationships, from file systems to game decision trees.

 Foundation for Advanced Concepts: Many advanced data


structures and algorithms build upon the concepts introduced in
these basic tree structures.

 Problem-Solving Skills: Implementing and working with tree


structures enhances problem-solving and algorithmic thinking skills.

This report aims to provide a comprehensive overview of binary trees,


binary search trees, and AVL trees. We will delve into their basic concepts,
explore the algorithms for key operations, analyse time and space
complexities, discuss applications in engineering and computer science,
and provide numerical examples to illustrate their functionality.

1
BINARY TREE

Basic Key Concepts


A binary tree is a hierarchical data structure where each node has at most
two child nodes, typically referred to as the left child and the right child.
The topmost node in the tree is called the root, and the nodes without any
children are called leaf nodes.

The key properties of binary trees are:

 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.

Algorithms and Programs


The fundamental operations on binary trees include:

 Insertion: Adding a new node to the tree while maintaining the


binary tree properties.
 Deletion: Removing a node from the tree while maintaining the
binary 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.

Here's a sample C program that implements the basic operations


on a binary tree:

#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

Time and Space Complexity


The time and space complexity of the basic operations on binary trees are
as follows:

 Insertion: The average time complexity is O(log n), where n is the


number of nodes in the tree. The worst-case time complexity is O(n)
when the tree is skewed (either left-skewed or right-skewed).
 Deletion: The average time complexity is O(log n), and the worst-
case time complexity is O(n).
 Traversal: The time complexity for all three traversal methods (in-
order, pre-order, post-order) is O(n), as each node is visited exactly
once.
 Search: The average time complexity is O(log n), and the worst-
case time complexity is O(n).

The space complexity for binary trees is O(n), as the maximum number of
nodes that can be stored in the tree is n.

Applications in Engineering and Uses


Binary trees are widely used in various engineering applications,
including:

 Database Indexing: Binary trees are used to implement indexing


in databases, allowing for efficient data retrieval.
 Expression Parsing: Binary trees are used to parse expressions in
compilers and interpreters.
 Network Routing: Binary trees are used in network routing
algorithms to find the shortest path between nodes.
 File Systems: Binary trees are used in file systems to manage
hierarchical data structures.

4
BINARY SEARCH TREE

Basic Key Concepts


A binary search tree (BST) is a specific type of binary tree where the value
of each node is greater than or equal to the values in its left subtree and
less than the values in its right subtree. This property allows for efficient
searching, insertion, and deletion operations.

The key properties of binary search trees are:

 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.

Algorithms and Programs


The fundamental operations on binary search trees include:

 Insertion: Adding a new node to the tree while maintaining the


binary search tree properties.
 Deletion: Removing a node from the tree while maintaining the
binary search 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.

Here's a sample C program that implements the basic operations


on a binary search tree:

#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

Time and Space Complexity


The time and space complexity of the basic operations on binary search
trees are as follows:

 Insertion: The average time complexity is O(log n), where n is the


number of nodes in the tree. The worst-case time complexity is O(n)
when the tree is skewed (either left-skewed or right-skewed).
 Deletion: The average time complexity is O(log n), and the worst-
case time complexity is O(n).
 Traversal: The time complexity for all three traversal methods (in-
order, pre-order, post-order) is O(n), as each node is visited exactly
once.
 Search: The average time complexity is O(log n), and the worst-
case time complexity is O(n).

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.

Applications in Engineering and Uses


Binary search trees have numerous applications in engineering and
computer science, including:

 Sorting and searching: Binary search trees can be used to


implement efficient sorting and searching algorithms, such as binary
search.
 Database indexing: Binary search trees are used to create and
maintain indexes in database management systems, allowing for
efficient data retrieval.
 Event scheduling: Binary search trees can be used to manage and
schedule events, such as in operating systems or real-time systems.

7
AVL TREE

Basic Key Concepts


An AVL tree (named after its inventors, Adelson-Velskii and Landis) is a
self-balancing binary search tree. In an AVL tree, the heights of the two
child subtrees of any node differ by at most one. This property ensures
that the tree remains balanced, which in turn guarantees efficient search,
insertion, and deletion operations.

The key properties of AVL trees are:

 Balance factor of a node in an AVL tree is the difference between the


height of the left subtree and that of the right subtree of that node.

 Balance Factor = (Height of Left Subtree - Height of Right Subtree) or


(Height of Right Subtree - Height of Left Subtree)

 The self balancing property of an avl tree is maintained by the balance


factor. The value of balance factor should always be -1, 0 or +1.

Algorithms and Programs


The fundamental operations on AVL trees include:

 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>

// AVL tree node structure


struct node {
int data;
struct node* left;
struct node* right;
int height;
};

// Function to get the height of a node


int getHeight(struct node* node) {
if (node == NULL) {
return 0;
}
return node->height;
}

// 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;
node->height = 1;
return node;
}

// Function to perform left rotation


struct node* leftRotate(struct node* x) {
struct node* y = x->right;
struct node* T2 = y->left;

y->left = x;
x->right = T2;

x->height = max(getHeight(x->left), getHeight(x->right)) + 1;


y->height = max(getHeight(y->left), getHeight(y->right)) + 1;

9
return y;
}

// Function to perform right rotation


struct node* rightRotate(struct node* y) {
struct node* x = y->left;
struct node* T2 = x->right;

x->right = y;
y->left = T2;

y->height = max(getHeight(y->left), getHeight(y->right)) + 1;


x->height = max(getHeight(x->left), getHeight(x->right)) + 1;

return x;
}

// Function to insert a node in the AVL tree


struct node* insert(struct node* root, int data) {
// ... (implementation omitted for brevity)
}

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);

// ... (add more operations as needed)

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:

 Insertion: The average time complexity is O(log n), where n is the


number of nodes in the tree. The worst-case time complexity is
O(log n) due to the self-balancing property of AVL trees.
 Deletion: The average time complexity is O(log n), and the worst-
case time complexity is O(log n).
 Traversal: The time complexity for all three traversal methods (in-
order, pre-order, post-order) is O(n), as each node is visited exactly
once.
 Search: The average time complexity is O(log n), and the worst-
case time complexity is O(log n).

The space complexity for AVL trees is O(n), as the maximum number of
nodes that can be stored in the tree is n.

Applications in Engineering and Uses


AVL trees have numerous applications in engineering and computer
science, including:

 Databases and file systems: AVL trees are used to implement


efficient indexing and searching mechanisms in databases and file
systems.
 Compiler design: AVL trees are used in compilers to represent and
manipulate symbol tables, which store information about variables,
functions, and other programming language constructs.
 Computational geometry: AVL trees are used in various
computational geometry algorithms, such as range queries and
nearest neighbor searches.
 Real-time systems: AVL trees can be used in real-time systems to
manage and schedule events, such as in operating systems or
embedded systems.
 Game development: AVL trees are used in game development to
maintain and query spatial data structures, such as collision
detection and scene management.

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.

This report has provided a comprehensive overview of these tree data


structures, covering their key concepts, algorithms, time and space
complexities, and various applications in engineering and computer
science. By understanding these topics, students can develop a strong
foundation in data structures and algorithms, which is essential for solving
complex problems and building efficient software systems.

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

You might also like