0% found this document useful (0 votes)
3 views

AVL_Trees_DSA_Java

Uploaded by

shilpa
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

AVL_Trees_DSA_Java

Uploaded by

shilpa
Copyright
© © All Rights Reserved
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
• Shilpa Sharma
Introduction to AVL Trees
• AVL tree is a self-balancing binary search tree.
Named after inventors Adelson-Velsky and
Landis.
Properties of AVL Trees
• 1. Balance factor: -1, 0, 1
• 2. Rotations to maintain balance: LL, RR, LR, RL
Need for AVL Trees
• 1. Ensures O(log n) height.
• 2. Efficient search, insertion, and deletion.
AVL Tree Rotations
• 1. LL Rotation
• 2. RR Rotation
• 3. LR Rotation
• 4. RL Rotation
LL Rotation
• Occurs when nodes are inserted into the left
subtree of the left child.
RR Rotation
• Occurs when nodes are inserted into the right
subtree of the right child.
LR Rotation
• Occurs when nodes are inserted into the left
subtree of the right child.
RL Rotation
• Occurs when nodes are inserted into the right
subtree of the left child.
AVL Tree Implementation in Java
• Steps:
• 1. Node structure definition.
• 2. Insert function.
• 3. Rotation logic.
Java Code: Node Structure
• ```java
• class Node {
• int data, height;
• Node left, right;
• Node(int d) {
• data = d;
• height = 1;
• }
• }
Java Code: Insert Function
• ```java
• int height(Node n) {
• return (n == null) ? 0 : n.height;
• }
• Node insert(Node node, int key) {
• // Insertion logic here
• }
• ```
Java Code: Rotations
• ```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. Faster lookups compared to unbalanced
trees.
• 2. Ensures balanced height at all times.
Applications of AVL Trees
• 1. Databases
• 2. File systems
• 3. Memory management

You might also like