Professional Documents
Culture Documents
FA20 BEE 146 Lab11
FA20 BEE 146 Lab11
Lab Report # 11
Learning Outcomes:
After successfully completing this lab, the students will be able to:
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 modify the insert function to incorporate AVL insertion. You
will find Programming Example on Page 324 of the above-mentioned book useful.
new_root->right = ptr;
ptr->left = temp;
return new_root;
}
// Function to rotate a node to the left
struct node *rotateLeft(struct node *ptr) {
struct node *new_root = ptr->right;
struct node *temp = new_root->left;
new_root->left = ptr;
ptr->right = temp;
return new_root;
}
Right(ptr);
}
(*ptr)->right = rightNode->left;
rightNode->left = *ptr;
*ptr = rightNode;
}
struct node* make_new_node(int insert_data)
{
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->data = insert_data;
temp->left = NULL;
temp->right = NULL;
temp->height = 0;
return temp;
}
Post-Lab Tasks:
Complete the following functions for the BST:
1. Add the delete node functionality.
Code:
bool delete_node(struct node* root, int data)
{
if (root == NULL)
return false;
if (root->data == data)
{
struct node* temp = root;
bool result = delete_child(root, &temp);
free(temp);
return result;
}
else if (data < root->data)
{
if (delete_node(root->left, data))
return true;
}
else
{
if (delete_node(root->right, data))
return true;
}
return false;
}
return true;
}
else if (child->left == NULL && child->right != NULL)
{
if (parent->left == child)
parent->left = child->right;
else
parent->right = child->right;
return true;
}
else if (child->left != NULL && child->right == NULL)
{
if (parent->left == child)
parent->left = child->left;
else
parent->right = child->left;
return true;
}
else
{
struct node* predecessor = child->left;
struct node* predecessorParent = child;
Conclusion: In conclusion, the AVL Tree implementation lab allowed us to explore the
concept of balanced binary search trees and their significance in maintaining efficient
operations. Through hands-on implementation and problem-solving, we deepened our
understanding of AVL Trees and the challenges involved in ensuring balance. This lab
provided valuable insights into data structure design and sharpened our problem-solving
skills.
------------------------------------------------------------------------------------