0% found this document useful (0 votes)
9 views7 pages

Data Structure and Algorithms

Dsa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

Data Structure and Algorithms

Dsa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

PMAS-Arid Agriculture University

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.

City AVL Tree Documentation:


1. Introduction
This program implements an AVL tree data structure to manage city records
efficiently. An AVL tree is a self-balancing binary search tree where the balance
factor of each node is maintained to ensure optimal performance during
operations.
2. City Node Structure
City Name: Name of the city.
year: Year associated with the city record.
Male Population: Population of males in the city.
Female Population: Population of females in the city.
Children Population: Population of children in the city.
Total Population: Sum of male, female, and children populations.
left, right: Pointers to the left and right child nodes.
height: Height of the node in the AVL tree.
3. City AVL Tree Class
3.1 Member Variables
root: Pointer to the root of the AVL tree.
3.2 Member Functions
3.2.1 add City Record
Purpose: Add a new city record to the AVL tree.
Parameters: City details - name, year, male population, female population,
children population.
3.2.2 display Records
Purpose: Display all city records using in-order traversal.
Output: City records with details: name, year, male population, female population,
children population, total population.
3.2.3 search City By Name
Purpose: Search for a city by its name.
Parameters: City name.
Output: Display details of the city if found, else indicate that the city was not
found.
3.2.4 delete City By Name
Purpose: Delete a city record by its name.
Parameters: City name.
Output: Indicate if the city was deleted successfully.
3.2.5 get Root
Purpose: Get the root of the AVL tree.
4. Tree Balancing Operations
4.1 calculate Height
Purpose: Calculate the height of a node in the AVL tree.
4.2 balance Factor
Purpose: Calculate the balance factor of a node.
4.3 rotate Right
Purpose: Perform a right rotation to balance the tree.
4.4 rotate Left
Purpose: Perform a left rotation to balance the tree.
4.5 balance
Purpose: Balance the AVL tree after an insertion or deletion.
4.6 insert
Purpose: Insert a new node into the AVL tree.
4.7 search By Name
Purpose: Search for a node by city name.
4.8 delete By Name
Purpose: Delete a node by city name.
4.9 inOrder Traversal
Purpose: Perform an in-order traversal of the AVL tree.
5. Main Functionality
The main function provides a simple user interface to interact with the AVL tree.
Options include adding city records, displaying records, searching by name,
deleting by name, and exiting.
6. Conclusion
This program efficiently manages city records using an AVL tree, ensuring
balanced performance during various operations. Users can add, display, search,
and delete city records through a user-friendly interface.

Xtra for AVL TREE Functionality:


AVL Tree Functionality:
1. Introduction
An AVL tree is a self-balancing binary search tree, named after its inventors
Adelson-Velsky and Landis. The primary goal of an AVL tree is to maintain balance
during insertion and deletion operations, ensuring that the tree remains
approximately balanced, and the height difference between the left and right
subtrees of any node is limited.

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.

You might also like