0% found this document useful (0 votes)
15 views3 pages

AVL Tree

This document defines an AVLTree class that implements an AVL self-balancing binary search tree. It includes methods for inserting nodes, rotating the tree for balance, calculating heights and balance factors, and traversing the tree in-order. The main method creates an AVLTree, inserts sample keys, and prints an in-order traversal to demonstrate the balanced tree structure.

Uploaded by

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

AVL Tree

This document defines an AVLTree class that implements an AVL self-balancing binary search tree. It includes methods for inserting nodes, rotating the tree for balance, calculating heights and balance factors, and traversing the tree in-order. The main method creates an AVLTree, inserts sample keys, and prints an in-order traversal to demonstrate the balanced tree structure.

Uploaded by

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

import java.util.

*;

public class Main {


public static void main(String[] args) {
AVLTree avlTree = new AVLTree();

int[] keys = {10, 20, 30, 40, 50, 25};

for (int key : keys) {


avlTree.root = avlTree.insert(avlTree.root, key);
}

System.out.println("In-order traversal of the AVL tree:");


avlTree.inOrderTraversal(avlTree.root);
}
}
class Node {
int key, height;
Node left, right;

Node(int key) {
this.key = key;
this.height = 1;
}
}

class AVLTree {
Node root;

int height(Node node) {


return (node == null) ? 0 : node.height;
}

int balanceFactor(Node node) {


return (node == null) ? 0 : height(node.left) - height(node.right);
}

Node rightRotate(Node y) {
Node x = y.left;
Node T2 = x.right;

// Perform rotation
x.right = y;
y.left = T2;

// Update heights
y.height = Math.max(height(y.left), height(y.right)) + 1;
x.height = Math.max(height(x.left), height(x.right)) + 1;

return x;
}

Node leftRotate(Node x) {
Node y = x.right;
Node T2 = y.left;

// Perform rotation
y.left = x;
x.right = T2;

// Update heights
x.height = Math.max(height(x.left), height(x.right)) + 1;
y.height = Math.max(height(y.left), height(y.right)) + 1;

return y;
}

Node insert(Node node, int key) {


// Perform standard BST insertion
if (node == null) {
return new Node(key);
}

if (key < node.key) {


node.left = insert(node.left, key);
} else if (key > node.key) {
node.right = insert(node.right, key);
} else {
return node; // Duplicate keys are not allowed
}

// Update height of the current node


node.height = Math.max(height(node.left), height(node.right)) + 1;

// Check balance factor


int balance = balanceFactor(node);

// Left Heavy
if (balance > 1) {
if (key < node.left.key) {
return rightRotate(node);
} else {
node.left = leftRotate(node.left);
return rightRotate(node);
}
}

// Right Heavy
if (balance < -1) {
if (key > node.right.key) {
return leftRotate(node);
} else {
node.right = rightRotate(node.right);
return leftRotate(node);
}
}

return node;
}

void inOrderTraversal(Node node) {


if (node != null) {
inOrderTraversal(node.left);
System.out.print(node.key + " ");
inOrderTraversal(node.right);
}
}
}

You might also like