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

AVL Tree Java Code

The document provides a Java implementation of an AVL Tree, a type of self-balancing binary search tree. It includes the necessary classes and methods for inserting nodes, performing rotations, and maintaining balance. The main function demonstrates the insertion of a sequence of integers and outputs the preorder traversal of the constructed AVL tree.
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)
2 views3 pages

AVL Tree Java Code

The document provides a Java implementation of an AVL Tree, a type of self-balancing binary search tree. It includes the necessary classes and methods for inserting nodes, performing rotations, and maintaining balance. The main function demonstrates the insertion of a sequence of integers and outputs the preorder traversal of the constructed AVL tree.
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

AVL Tree in Java

Q. Create AVL Tree (Balanced BST) for the following sequence: 3,


2, 1, 4, 5, 6, 7, 8, 9

Java Code:

// Java program to insert a node in AVL tree


class Node {
int key, height;
Node left, right;

Node(int d) {
key = d;
height = 1;
}
}

class AVLTree {

Node root;

int height(Node N) {
if (N == null)
return 0;
return [Link];
}

int max(int a, int b) {


return (a > b) ? a : b;
}

Node rightRotate(Node y) {
Node x = [Link];
Node T2 = [Link];

// Perform rotation
[Link] = y;
[Link] = T2;

// Update heights
[Link] = max(height([Link]), height([Link])) + 1;
[Link] = max(height([Link]), height([Link])) + 1;

// Return new root


return x;
}
Node leftRotate(Node x) {
Node y = [Link];
Node T2 = [Link];

// Perform rotation
[Link] = x;
[Link] = T2;

// Update heights
[Link] = max(height([Link]), height([Link])) + 1;
[Link] = max(height([Link]), height([Link])) + 1;

// Return new root


return y;
}

int getBalance(Node N) {
if (N == null)
return 0;
return height([Link]) - height([Link]);
}

Node insert(Node node, int key) {


if (node == null)
return (new Node(key));

if (key < [Link])


[Link] = insert([Link], key);
else if (key > [Link])
[Link] = insert([Link], key);
else
return node;

[Link] = 1 + max(height([Link]), height([Link]));

int balance = getBalance(node);

// Left Left Case


if (balance > 1 && key < [Link])
return rightRotate(node);

// Right Right Case


if (balance < -1 && key > [Link])
return leftRotate(node);

// Left Right Case


if (balance > 1 && key > [Link]) {
[Link] = leftRotate([Link]);
return rightRotate(node);
}

// Right Left Case


if (balance < -1 && key < [Link]) {
[Link] = rightRotate([Link]);
return leftRotate(node);
}

return node;
}

void preOrder(Node node) {


if (node != null) {
[Link]([Link] + " ");
preOrder([Link]);
preOrder([Link]);
}
}

public static void main(String[] args) {


AVLTree tree = new AVLTree();
int[] keys = {3, 2, 1, 4, 5, 6, 7, 8, 9};
for (int key : keys) {
[Link] = [Link]([Link], key);
}
[Link]("Preorder traversal of constructed AVL tree
is:");
[Link]([Link]);
}
}

You might also like