AVL Tree is an extended version of Binary search tree(BST), AVL Tree is self balancing BST, where the difference between heights of left and right subtrees cannot be more than one for all nodes(-1, 0, 1).
The advantage of AVL tree over BST is its Time Complexity.
1 & 2) Please read the comments for better understanding of the concept
class Node {
int key, height;
Node left, right;
Node(int k) {
key = k;
height = 1;
}
}
class AVLTree {
Node root;
// function to get height of the tree
int height(Node N) {
if (N == null)
return 0;
return N.height;
}
// function to get maximum of two integer numbers
int max(int x, int y) {
return (x > y) ? x : y;
}
// Right Rotation
Node RotateRight(Node y) {
Node x = y.left;
Node T2 = x.right;
// Performing rotation
x.right = y;
y.left = T2;
// Updating heights
y.height = max(height(y.left), height(y.right)) + 1;
x.height = max(height(x.left), height(x.right)) + 1;
// Return new root
return x;
}
// Left Rotation
Node RotateLeft(Node x) {
Node y = x.right;
Node T2 = y.left;
// Performing rotation
y.left = x;
x.right = T2;
// Updating heights
x.height = max(height(x.left), height(x.right)) + 1;
y.height = max(height(y.left), height(y.right)) + 1;
// Return new root
return y;
}
//Balance factor of node N
int getBalanceFactor(Node N) {
if (N == null)
return 0;
return height(N.left) - height(N.right);
}
Node insert(Node node, int key) {
// normal 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 // Duplicate keys not allowed
return node;
// Update height of this ancestor node
node.height = 1 + max(height(node.left),
height(node.right));
//To check whether this node became unbalanced
int balance = getBalanceFactor(node);
// If the node becomes unbalanced, then there are 4 cases as we
know
// Left Left case
if (balance > 1 && key < node.left.key)
return RotateRight(node);
// Right Right Case
if (balance < -1 && key > node.right.key)
return RotateLeft(node);
// Left Right Case
if (balance > 1 && key > node.left.key) {
node.left = RotateLeft(node.left);
return RotateRight(node);
}
// Right Left Case
if (balance < -1 && key < node.right.key) {
node.right = RotateRight(node.right);
return RotateLeft(node);
}
return node;
}
Node minValueNode(Node node)
{
Node current = node;
// loop down to find the leftmost leaf
while (current.left != null)
current = current.left;
return current;
}
Node Delete(Node root, int key)
{
// Normal BST Delete
if (root == null)
return root;
// If the key to be deleted is smaller than the root's key, then it
should be in left subtree
if (key < root.key)
root.left = Delete(root.left, key);
// If the key to be deleted is greater than root's key, then it is
in right subtree
else if (key > root.key)
root.right = deleteNode(root.right, key);
// if key is same as root's key, it should be deleted
else
{
// node with only one child or no child
if ((root.left == null) || (root.right == null))
{
Node temp = null;
if (temp == root.left)
temp = root.right;
else
temp = root.left;
// No child case
if (temp == null)
{
temp = root;
root = null;
}
else // One child case
root = temp;
}
else
{
Node temp = minValueNode(root.right);
// Copy the successor of inorder data to this node
root.key = temp.key;
root.right = Delete(root.right, temp.key);
}
}
if (root == null)
return root;
root.height = max(height(root.left), height(root.right)) + 1;
int balance = getBalanceFactor(root);
// Left Left Case
if (balance > 1 && getBalanceFactor(root.left) >=
0)
return RotateRight(root);
// Left Right Case
if (balance > 1 && getBalanceFactor(root.left) <
0)
{
root.left = RotateLeft(root.left);
return RotateRight(root);
}
// Right Right Case
if (balance < -1 && getBalanceFactor(root.right) <=
0)
return RotateLeft(root);
// Right Left Case
if (balance < -1 && getBalanceFactor(root.right) >
0)
{
root.right = RotateRight(root.right);
return RotateLeft(root);
}
return root;
}
void inorder(Node node)
{
if (node == null)
return;
//recursion on left child
inorder(node.left);
// then print the data of node
System.out.print(node.key + " ");
// recursion on right child
inorder(node.right);
}
public static void main(String[] args) {
AVLTree tree = new AVLTree();
//Constructing tree as given in the question(2)
tree.root = tree.insert(tree.root, 8);
tree.root = tree.insert(tree.root, 12);
tree.root = tree.insert(tree.root, 14);
tree.root = tree.insert(tree.root, 18);
tree.root = tree.insert(tree.root, 20);
tree.root = tree.insert(tree.root, 23);
tree.root = tree.insert(tree.root,
15);
tree.root = tree.insert(tree.root, 13);
tree.root = tree.insert(tree.root, 7);
tree.root = tree.insert(tree.root, 16);
tree.inOrder(tree.root);
Scanner sc = new
Scanner(System.in);
System.out.ptintln(" Enter 3
elements to be deleted from the avl tree: ");
for(int i = 0; i < 1;
i++){
tree.root = tree.Delete(tree.root, sc.nextInt());
}
System.out.println("After Deletion
:");
tree.inorder(tree.root);
}
}
3) please read the comments for better understanding
class Node {
String key;
int height;
Node left, right;
Node(String k) {
key = k;
height = 1;
}
}
Class AVLString{
public AVLNode search(String key)
{
// Tree is empty
if (root == null)
{
return null;
}
// Tree is not empty
AVLNode p = root;
try
{while (true)
{
if(p == null && p.getData().equals(key))
{
break;
}
else if (key.compareTo(p.getData()) < 0)
{
p = p.getLeftChild();
}
else
{
p = p.getRightChild();
}
}
}
catch(NullPointerException e)
{
e.printStackTrace();
}
return p;
}
public void insert(String data)
{
try
{
// Case: tree is empty
if (root == null)
{
root = new AVLNode(data);
return;
}
// Case: tree is not empty
AVLNode p = root;
// move p down as far as we can
while(true)
{
if (data.equals(p.getData()) && p.getLeftChild() !=
null)
{
p = p.getLeftChild();
}
else if(data.compareTo(p.getData()) < 0 &&
p.getLeftChild() != null)
{
p = p.getLeftChild();
}
else if (data.compareTo(p.getData()) > 0 &&
p.getRightChild() != null)
{
p = p.getRightChild();
}
else
{
break;
}
}
// insert new node to the tree as a child of node p
AVLNode newNode= new AVLNode(data);
if(data.equals(p.getData()))
{
p.setLeftChild(newNode);
}
else if (data.compareTo(p.getData()) < 0)
{
p.setLeftChild(newNode);
}
else
{
p.setRightChild(newNode);
}
rebalanceInsertionPath(newNode);
}
catch(NullPointerException n)
{
n.printStackTrace();
}
}
void inorder(Node node)
{
if (node == null)
return;
//recursion on left child
inorder(node.getLeftChild());
// then print the data of node
System.out.print(node.key + " ");
// recursion on right child
inorder(node.getRightChild());
}
public static void main(String[] args) throws
Exception
{
AVLString tree = new AVLString();
// pass the path to the file as a parameter
FileReader fr =
new FileReader("your file path");
int i;
while ((i=fr.read()) != -1)
tree.insert((String) i);
tree.inorder(tree.root);
}
}
The Height of AVL Tree is always O(log n) where n is number of nodes and time complexity of most of operations is O(log n).
Hope the answer is helpful, All the best!
Using JAVA Lab Exercises 1. Complete the class AVLTree that extends BST. It should have four...
using java to write,show me the output. please write some common. You CAN NOT use inbuild functions for Tree ADT operations. using code below to finsih public class Main { public static void main(String[] args) { BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.root.right.left = new Node(6); tree.root.right.right = new Node(7); tree.root.left.left.left = new Node(8); tree.root.left.left .right= new Node(9);...
Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...
write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the words in order (based on ASCII/UNICODE order) on the screen (or to output text file). Note that you may need to make some changes to BST.java. Sample test: ----jGRASP exec: java -ea removeDuplicates Original Text: a B 2 n w C q K l 0...
*****************************In Java***************************************In Java***********************************In Java************************* In this problem, you will implement various algorithms operating on binary search trees. We have provided with you a standard implementation of a generic BST in BinarySearchTree.java. Note that this class is an abstract class, which means that some of its methods are not implemented. In previous assignments, you have implemented interfaces which specified methods that you needed to write. Very similarly, an abstract class is a class with some unimplemented methods (it can be thought...
Could someone please summarize the following for my programming class? They are study questions for java What an association list is. How to test if an association list is empty. How to find the value associated with a key in an association list. How to add a key-value pair to an association list. How to delete a key-value pair from an association list. How efficient an association list is (using O notation). What a circular list is. What a circular...
BST JAVA FILE import java.util.*; public class BST <E extends Comparable <E>> { private TreeNode<E> overallRoot; public BST() { overallRoot = null; } // ************ ADD ************ // public void add(E addThis) { if (overallRoot == null) { overallRoot = new TreeNode<>(addThis); } else { add(overallRoot, addThis); } } private TreeNode<E> add(TreeNode<E> node, E addThis) { if...
class AVLTree The following functions are the minimum requirements for the AVL class. You can add any function from Assignment 2 to this class. You should modify the BSTree insert function so that the tree remains balanced after each insertion. Required Public Member Functions void insert(const string &): Insert an item to the binary search tree and perform rotation if necessary. int balanceFactor(Node*): Return the balance factor of a given node. void printBalanceFactors(): Traverse and print the tree in inorder...
Assignment on Java programing 1. Write programs for the following exercises in Java. Each file should have short description of the implemented class and for files with main method the problem it is solving. Make sure your files have appropriate names. Programs should write output to the Console. b) BST: Implement Binary Search Tree ADT with insert(int key), delete(int key), Node find(int key), and in-order traverse() where it prints the value of the key. Your operations should use recursion. The...
Question B1 You are given the following Java classes: public class Queue { private static class Node { Object object; Node next; Node () { object = null; next = null; } Node (Object object, Node next) { this.object = object; this.next = next; private Node header; private int size = 0; // size shows the no of elements in queue public Object dequeue () { if (size == 0 ) { return null; else { Object remove_object = header.object;...
Problemi: Create a new Java Application that has the following methods: 1. A method that generate a binary search tree (BST) where the number of nodes and the range of the values are from a file 2. A recursive method to print the BST in preorder traversal 3. A recursive method to print the BST in post-order traversal 4. A recursive method to print the BST in in-order traversal 5. A method to find the largest value in a BST...