AVLTREE
AVLTREE
LinkedList;
import java.util.Queue;
public AVLTree() {
this.size = 0;
this.root = null;
}
if (node.lessThan(key)) {
node.right = insert(node.right, key);
} else {
node.left = insert(node.left, key);
}
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);
}
if (node.equal(key)) {
return true;
} else if (node.lessThan(key)) {
return search(node.right, key);
} else {
return search(node.left, key);
}
}
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);
// Get the balance factor of this ancestor node to check whether this node
became unbalanced
int balance = getBalance(node);
return node;
}
return current;
}
public void update(int key, int newValue) {
root = update(root, key, newValue);
}
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);
}
// Get the balance factor of this ancestor node to check whether this node
became unbalanced
int balance = getBalance(node);
return node;
}
return returnNode;
}
private AVLNode rightRotation(AVLNode node) {
if (node == null) return null;
return returnNode;
}
node.printInfo();
printPreOrder(node.left);
printPreOrder(node.right);
}
printInOrder(node.left);
node.printInfo();
printInOrder(node.right);
}
public void bfs() {
if (root == null) {
System.out.println("Tree is empty");
return;
}