Docsity

DSA Lab Report 10 Comsats, Lab Reports of Data Structures and Algorithms

Partial preview of the text

Download DSA Lab Report 10 Comsats and more Lab Reports Data Structures and Algorithms in PDF only on Docsity!

CSC- 112

Algorithms and Data Structures

Lab Report # 10

Name: Muhammad Souban Javaid

Roll No: FA20-BEE- 146

Section: 6C

Submitted to: Sir Muhammad Imran

Dated: 16 - 05 - 2023

Lab 10 :

Binary Search Tree Implementation

Learning Outcomes:

After successfully completing this lab, the students will be able to:

1. Understand the properties of Binary Search Trees and their associated functions.

2. Develop C programs for implementing Binary Search Trees and their associated functions.

3. Use BSTs to load/store data to/from a file on the hard disk.

In-Lab Tasks:

You are provided with skeleton code that builds a Binary Search Tree by adding 10 nodes to it.

Functions for node insertion and printing the tree (in-order traversal only) are already

implemented. Your task is to complete the following functions.

1. Node deletion

Code:

bool delete_node(struct node* root, int data) { if (root == NULL) return false; // Searching for the node to be deleted struct node* current = root; struct node* parent = NULL; bool is_left_child = false; while (current != NULL && current->data != data) { parent = current; if (data < current->data) { current = current->left; is_left_child = true; } else { current = current->right; is_left_child = false; } } if (current == NULL) return false; // Node not found // Case 1: Node to be deleted is a leaf node if (current->left == NULL && current->right == NULL) { if (parent == NULL) { free(root); root = NULL; } else if (is_left_child)

2. Node search

Code:

bool bst_search(struct node* root, int key) { if (root == NULL) return false; if (root->data == key) return true; else if (key < root->data) return bst_search(root->left, key); else return bst_search(root->right, key); }

3. Pre-Order and Post-Order printing

Code:

void print_pre_order(struct node* root) { if (root == NULL) return; printf("%d ", root->data); print_pre_order(root->left); print_pre_order(root->right); } void print_post_order(struct node* root) { if (root == NULL) return; print_post_order(root->left); print_post_order(root->right); printf("%d ", root->data); }

Post-Lab Tasks:

Complete the following functions for the BST:

1. Save the Tree data to a file (In-Order, Pre-Order and Post-Order)

Code:

bool save_in_order(struct node* root, FILE* fptr) { if (root == NULL) return false; save_in_order(root->left, fptr); fprintf(fptr, "%d ", root->data); save_in_order(root->right, fptr); return true; } bool save_pre_order(struct node* root, FILE* fptr)

if (root == NULL) return false; fprintf(fptr, "%d ", root->data); save_pre_order(root->left, fptr); save_pre_order(root->right, fptr); return true; } bool save_post_order(struct node* root, FILE* fptr) { if (root == NULL) return false; save_post_order(root->left, fptr); save_post_order(root->right, fptr); fprintf(fptr, "%d ", root->data); return true; }

2. Load tree from a file containing numbers.

Code:

int load_tree(struct node* root, FILE* fptr) { if (root == NULL) return 0 ; int count = 0 ; while (fscanf(fptr, "%d", &(root->data)) == 1 ) { count++; root->left = NULL; root->right = NULL; root = root->right; } return count; }

Complete Code:

#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include "node.h" #include "BST.h" void print_in_order(struct node* ptr) { if (ptr == NULL) return; if (ptr->left != NULL) { print_in_order(ptr->left); } printf("%d\t", ptr->data);

printf("\n%d inserted to the right of %d!", insert_data, (*ptr)-

data); return true;

} } return true; } bool delete_node(struct node* root, int data) { if (root == NULL) return false; // Searching for the node to be deleted struct node* current = root; struct node* parent = NULL; bool is_left_child = false; while (current != NULL && current->data != data) { parent = current; if (data < current->data) { current = current->left; is_left_child = true; } else { current = current->right; is_left_child = false; } } if (current == NULL) return false; // Node not found // Case 1: Node to be deleted is a leaf node if (current->left == NULL && current->right == NULL) { if (parent == NULL) { free(root); root = NULL; } else if (is_left_child) { free(parent->left); parent->left = NULL; } else { free(parent->right); parent->right = NULL; } } // Case 2: Node to be deleted has only one child else if (current->left == NULL) { if (parent == NULL)

root = current->right; free(current); } else if (is_left_child) { parent->left = current->right; free(current); } else { parent->right = current->right; free(current); } } // Case 2: Node to be deleted has only one child (continued) else if (current->right == NULL) { if (parent == NULL) { root = current->left; free(current); } else if (is_left_child) { parent->left = current->left; free(current); } else { parent->right = current->left; free(current); } } // Case 3: Node to be deleted has two children else { struct node* successor = find_successor(current->right); int successor_data = successor->data; delete_node(root, successor_data); current->data = successor_data; } return true; } struct node* find_successor(struct node* root) { struct node* current = root; while (current->left != NULL) current = current->left; return current; } bool save_in_order(struct node* root, FILE* fptr) { if (root == NULL) return false;

if (root == NULL) return; printf("%d ", root->data); print_pre_order(root->left); print_pre_order(root->right); } void print_post_order(struct node* root) { if (root == NULL) return; print_post_order(root->left); print_post_order(root->right); printf("%d ", root->data); }

Output:

Conclusion:

In Lab 10, we implemented a Binary Search Tree (BST) in C. The BST is a data structure that

provides efficient searching, insertion, and deletion operations. It is composed of nodes where

each node has a key and two child nodes (left and right). The left child node contains keys smaller

than the parent node's key, while the right child node contains keys greater than the parent node's

key. This property enables efficient searching by recursively traversing the tree.

During the implementation, we created a structure to represent each node in the BST. We

implemented functions for inserting a new node, searching for a specific key, and deleting a node

from the tree. Additionally, we implemented functions for performing in-order, pre-order, and

post-order traversals of the tree, which allow us to visit all nodes in a specific order.

The BST proved to be a powerful data structure for organizing and retrieving data in an efficient

manner. The searching operation has a time complexity of O(log n) in the average case and O(n)

in the worst case, where n is the number of nodes in the tree. The insertion and deletion operations

also have time complexities of O(log n) in the average case and O(n) in the worst case.

The implementation of the Binary Search Tree provided us with a flexible and efficient data

structure for storing and manipulating data. The BST has a wide range of applications and can be

further enhanced with additional functionalities based on specific requirements.