Data Structure and Algorithms
Data Structure and Algorithms
Rawalpindi
University Institute of Information
Technology
Data Structure Project
BSSE-3(B) Evening
Submitted by:
Muhammad Abubakar Aziz (22-ARID-1025)
Fahad Ali (22-ARID-1003)
Sajal Asghar (22-ARID-1053)
Submitted to:
Sir Muhammad Yousaf
DATA STRUCTURE AND ALGORITHMS
Summary:
Project is about the AVL Tree based City Population management
system. This program is designed to organize and manage city records
using an AVL tree, which is a specialized data structure ensuring
efficient organization and balance. The AVL tree allows users to
perform various operations, including adding new city records,
displaying existing ones, searching for cities by name, and deleting
cities. By implementing rotations and balancing mechanisms, the AVL
tree maintains optimal performance. The user interface is menu-
driven, providing a user-friendly experience for interacting with city
data. Overall, the program offers a solution for navigating city
information with ease.
2. Balancing Operations
2.1 Calculate Height (calculate Height)
Purpose: Determines the height of a node in the AVL tree.
Algorithm: Returns the height of the node if it exists; otherwise, returns 0.
2.2 Balance Factor (balance Factor)
Purpose: Computes the balance factor of a node, defined as the difference
between the heights of its left and right subtrees.
Algorithm: Calculates the balance factor using the heights of left and right
subtrees. A balance factor of 0, 1, or -1 indicates a balanced tree.
2.3 Right Rotation (rotate Right)
Purpose: Performs a right rotation to restore balance in the AVL tree.
Algorithm: Adjusts pointers to maintain the AVL property, updating heights of
affected nodes.
2.4 Left Rotation (rotate Left)
Purpose: Executes a left rotation to restore balance in the AVL tree.
Algorithm: Adjusts pointers to maintain the AVL property, updating heights of
affected nodes.
2.5 Balance (balance)
Purpose: Checks and balances the AVL tree after an insertion or deletion
operation.
Algorithm: Utilizes rotation operations (rotate Right and rotate Left) based on the
balance factors to ensure the AVL property.
3. Insertion Operation (insert)
Purpose: Inserts a new node into the AVL tree while maintaining balance.
Algorithm: Recursively inserts the new node, updates heights, and performs
balancing operations.
4. Search Operation (search By Name)
Purpose: Searches for a node with a specific key (city name) in the AVL tree.
Algorithm: Utilizes binary search to traverse the tree and find the node with the
given key.
5. Deletion Operation (delete By Name)
Purpose: Deletes a node with a specific key (city name) from the AVL tree while
maintaining balance.
Algorithm: Recursively deletes the node, replaces it with its in-order successor,
and performs balancing operations.
6. In-Order Traversal (inOrder Traversal)
Purpose: Performs an in-order traversal of the AVL tree, displaying the nodes in
ascending order.
Algorithm: Recursively visits the left subtree, processes the current node, and
then recursively visits the right subtree.
7. Tree Display (display Records)
Purpose: Displays all nodes in the AVL tree using in-order traversal.
Algorithm: Calls inOrder Traversal to print each node's details.
8. Tree Root Access (get Root)
Purpose: Returns the root of the AVL tree.
Use: Used externally to access the root for display or other operations.
9. Conclusion
AVL trees ensure that the tree remains balanced, providing efficient search, insert,
and delete operations with a time complexity of O(log n), where n is the number
of nodes in the tree. The balancing operations maintain a balance factor of 0, 1, or
-1 for each node, preventing the tree from becoming skewed and ensuring
optimal performance.