0% found this document useful (0 votes)
19 views6 pages

AVLTREE

The document contains a Java implementation of an AVL Tree, which is a self-balancing binary search tree. It includes methods for insertion, deletion, searching, updating nodes, and traversing the tree in pre-order, in-order, and breadth-first search (BFS). The AVL Tree maintains its balance through rotations and height adjustments after modifications.

Uploaded by

Hữu Minh
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)
19 views6 pages

AVLTREE

The document contains a Java implementation of an AVL Tree, which is a self-balancing binary search tree. It includes methods for insertion, deletion, searching, updating nodes, and traversing the tree in pre-order, in-order, and breadth-first search (BFS). The AVL Tree maintains its balance through rotations and height adjustments after modifications.

Uploaded by

Hữu Minh
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/ 6

import java.util.

LinkedList;
import java.util.Queue;

public class AVLTree {


private int size;
private AVLNode root;

public AVLTree() {
this.size = 0;
this.root = null;
}

public int size() {


return this.size;
}
public int height(AVLNode node) {
return (node == null) ? 0 : node.height;
}

public void insert(int key) {


this.root = insert(this.root, key);
}
private AVLNode insert(AVLNode node, int key) {
if (node == null) {
this.size++;
return new AVLNode(key);
}

if (node.equal(key)) return node;

if (node.lessThan(key)) {
node.right = insert(node.right, key);
} else {
node.left = insert(node.left, key);
}

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

int balance = getBalance(node);

if (balance > 1) {
// => right
if (node.left.lessThan(key)) {
// left rotation
node.left = leftRotation(node.left);
}
// right rotation
return rightRotation(node);
} else if (balance < -1) {
// => left
if (node.right.greaterThan(key)) {
// right rotation
node.right = rightRotation(node.right);
}
// left rotation
return leftRotation(node);
}
return node;
}
public boolean search(int key) {
return search(root, key);
}

private boolean search(AVLNode node, int key) {


if (node == null) {
return false;
}

if (node.equal(key)) {
return true;
} else if (node.lessThan(key)) {
return search(node.right, key);
} else {
return search(node.left, key);
}
}

public void delete(int key) {


root = delete(root, key);
}

private AVLNode delete(AVLNode node, int key) {


if (node == null) {
return null;
}

if (node.lessThan(key)) {
node.right = delete(node.right, key);
} else if (node.greaterThan(key)) {
node.left = delete(node.left, key);
} else {
// Node with only one child or no child
if (node.left == null || node.right == null) {
AVLNode temp = (node.left != null) ? node.left : node.right;

// No child case
if (temp == null) {
temp = node;
node = null;
} else {
// One child case
node = temp;
}
} else {
// Node with two children, get the inorder successor
AVLNode temp = minValueNode(node.right);

// Copy the inorder successor's data to this node


node.setData(temp.getData());

// Delete the inorder successor


node.right = delete(node.right, temp.getData());
}
}
// If the tree had only one node then return
if (node == null) {
return null;
}

// Update height of the current node


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

// Get the balance factor of this ancestor node to check whether this node
became unbalanced
int balance = getBalance(node);

// If this node becomes unbalanced, then there are 4 cases

// Left Left Case


if (balance > 1 && getBalance(node.left) >= 0) {
return rightRotation(node);
}

// Left Right Case


if (balance > 1 && getBalance(node.left) < 0) {
node.left = leftRotation(node.left);
return rightRotation(node);
}

// Right Right Case


if (balance < -1 && getBalance(node.right) <= 0) {
return leftRotation(node);
}

// Right Left Case


if (balance < -1 && getBalance(node.right) > 0) {
node.right = rightRotation(node.right);
return leftRotation(node);
}

return node;
}

private AVLNode minValueNode(AVLNode node) {


AVLNode current = node;

while (current.left != null) {


current = current.left;
}

return current;
}
public void update(int key, int newValue) {
root = update(root, key, newValue);
}

private AVLNode update(AVLNode node, int key, int newValue) {


if (node == null) {
return null;
}

if (node.equal(key)) {
// Update the value of the node with the specified key
node.setData(newValue);
} else if (node.lessThan(key)) {
node.right = update(node.right, key, newValue);
} else {
node.left = update(node.left, key, newValue);
}

// Update height of the current node


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

// Get the balance factor of this ancestor node to check whether this node
became unbalanced
int balance = getBalance(node);

// If this node becomes unbalanced, then there are 4 cases

// Left Left Case


if (balance > 1 && getBalance(node.left) >= 0) {
return rightRotation(node);
}

// Left Right Case


if (balance > 1 && getBalance(node.left) < 0) {
node.left = leftRotation(node.left);
return rightRotation(node);
}

// Right Right Case


if (balance < -1 && getBalance(node.right) <= 0) {
return leftRotation(node);
}

// Right Left Case


if (balance < -1 && getBalance(node.right) > 0) {
node.right = rightRotation(node.right);
return leftRotation(node);
}

return node;
}

private int max(int key1, int key2) {


if (key1 <= key2) return key2;
return key1;
}
private int getBalance(AVLNode node) {
if (node == null) return 0;

return height(node.left) - height(node.right);


}
private AVLNode leftRotation(AVLNode node) {
if (node == null) return null;

AVLNode returnNode = node.right;


node.right = returnNode.left;
returnNode.left = node;

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


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

return returnNode;
}
private AVLNode rightRotation(AVLNode node) {
if (node == null) return null;

AVLNode returnNode = node.left;


node.left = returnNode.right;
returnNode.right = node;

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


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

return returnNode;
}

public void printPreOrder() {


System.out.print("\n");
System.out.print("printPreOrder, size = " + this.size);
System.out.print("\n");
printPreOrder(this.root);
}
private void printPreOrder(AVLNode node) {
if (node == null) return;

node.printInfo();
printPreOrder(node.left);
printPreOrder(node.right);
}

public void printInOrder() {


System.out.print("\n");
System.out.print("printInOrder, size = " + this.size);
System.out.print("\n");
printInOrder(this.root);
}
private void printInOrder(AVLNode node) {
if (node == null) return;

printInOrder(node.left);
node.printInfo();
printInOrder(node.right);
}
public void bfs() {
if (root == null) {
System.out.println("Tree is empty");
return;
}

Queue<AVLNode> queue = new LinkedList<>();


queue.add(root);

System.out.println("\nBFS traversal, size = " + this.size);


while (!queue.isEmpty()) {
AVLNode current = queue.poll();
current.printInfo();
if (current.left != null) {
queue.add(current.left);
}
if (current.right != null) {
queue.add(current.right);
}
}
System.out.println();
}
}

You might also like