0% found this document useful (0 votes)
28 views15 pages

AVL_Trees_DSA_Java_Enhanced

Uploaded by

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

AVL_Trees_DSA_Java_Enhanced

Uploaded by

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

AVL Trees in DSA with Java Programming

Prepared by Shilpa Sharma


Introduction to AVL Trees
• AVL tree is a self-balancing binary search tree
that ensures logarithmic height for efficient
operations. The height difference between the
left and right subtrees (balance factor) is
always maintained at -1, 0, or 1.
• This prevents degradation of performance
seen in unbalanced trees.
Properties of AVL Trees
• 1. Balance factor: Calculated as Height(left) -
Height(right).
• 2. Rotations ensure balance: LL, RR, LR, RL.
• 3. Height is maintained logarithmically
proportional to the number of nodes.
Need for AVL Trees
• 1. Overcomes inefficiencies of unbalanced
Binary Search Trees (BSTs).
• 2. Suitable for applications requiring frequent
insertions and deletions, maintaining O(log n)
performance.
• 3. Ensures optimal search times.
AVL Tree Rotations
• Rotations are used to restore balance after
insertion or deletion:
• 1. LL (Left-Left) Rotation
• 2. RR (Right-Right) Rotation
• 3. LR (Left-Right) Rotation
• 4. RL (Right-Left) Rotation.
• Diagrams and examples are essential for
understanding.
LL Rotation
• Performed when nodes are inserted in the left
subtree of the left child of a node.
• This is fixed by a single right rotation. Example
and diagram illustrate this transformation.
RR Rotation
• Performed when nodes are inserted in the
right subtree of the right child of a node.
• This is fixed by a single left rotation. Example
and diagram illustrate this transformation.
LR Rotation
• Occurs when nodes are inserted in the left
subtree of the right child of a node.
• This is fixed by a left rotation on the child,
followed by a right rotation on the parent.
Example and diagram illustrate this
transformation.
RL Rotation
• Occurs when nodes are inserted in the right
subtree of the left child of a node.
• This is fixed by a right rotation on the child,
followed by a left rotation on the parent.
Example and diagram illustrate this
transformation.
AVL Tree Implementation in Java
• Steps:
• 1. Define a node class with attributes for data,
height, and child pointers.
• 2. Implement insertion logic to ensure
balancing after every operation.
• 3. Write rotation methods to handle
imbalances.
Java Code: Node Structure
• ```java
• class Node {
• int data, height;
• Node left, right;
• Node(int d) {
• data = d;
• height = 1;
• }
• }
Java Code: Insert Function
• The insert function ensures balancing:
• ```java
• int height(Node n) {
• return (n == null) ? 0 : n.height;
• }
• Node insert(Node node, int key) {
• // Logic to insert and balance tree
• }
• ```
Java Code: Rotations
• Rotations re-balance the tree:
• ```java
• Node rightRotate(Node y) {
• Node x = y.left;
• Node T2 = x.right;
• x.right = y;
• y.left = T2;
• return x;
• }
Advantages of AVL Trees
• 1. Ensures balanced height for all operations.
• 2. Guarantees O(log n) time complexity for
search, insertion, and deletion.
• 3. Widely used in performance-critical
applications.
Applications of AVL Trees
• 1. Database indexing to optimize query
performance.
• 2. File system management for fast access.
• 3. Efficient memory allocation in operating
systems.

You might also like