Skip to content

Update and rename 3-set/java/implementations/treeset to 3-set/java/imple... #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions 3-set/java/implementations/treeset/AVLTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@

public class AVLTree {

//Function to compute height of a tree
public static int height(TreeNode current){
if(current == null)
return 0;
return current.height;
}

/* y x
/ \ / \
x leaf3 ---------> leaf1 y
/ \ / \
/ \ / \
leaf1 leaf2 leaf2 leaf3 */
/* Right Rotate */
public static TreeNode rightrotate(TreeNode y){
TreeNode x = y.left;
TreeNode leaf = x.right;

x.right = y;
y.left = leaf;

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

return x;
}
/* y x
/ \ / \
x leaf3 <--------- leaf1 y
/ \ / \
/ \ / \
leaf1 leaf2 leaf2 leaf3 */
/* Left Rotate */

public static TreeNode leftrotate(TreeNode x){
TreeNode y = x.right;
TreeNode leaf = y.left;

y.left = x;
x.right = leaf;

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

return y;
}


//Get Balance factor of TreeNode current
public static int getBalance(TreeNode current){
if(current == null)
return 0;
return height(current.left) - height(current.right);
}

//Performs insert operation
public static TreeNode insert(TreeNode current, int key){


//Condition for adding first node
if(current == null)
return new TreeNode(key);

//Normal BST insertion
if(key < current.key)
current.left = insert(current.left, key);
else
current.right = insert(current.right, key);

//Updating height of the ancestor node
current.height = Math.max(height(current.left), height(current.right)) + 1;

//getting balance factor to check if the node is unbalanced
int balance = getBalance(current);

//4 conditions of unbalancing
if(balance > 1 && key < current.left.key)
return rightrotate(current);
if(balance < -1 && key > current.right.key)
return leftrotate(current);
if(balance > 1 && key > current.left.key)
{
current.left = leftrotate(current.left);
return rightrotate(current);
}

if(balance < -1 && key < current.right.key)
{
current.right = rightrotate(current.right);
return leftrotate(current);
}

return current;
}

//Tree traversal preOrder
public static void preOrder(TreeNode root){
if(root != null)
{
System.out.println(root.key);
preOrder(root.left);

preOrder(root.right);
}
}

public static void main(String args[])
{
TreeNode root = null;
root = insert(root, 10);
root = insert(root, 20);
root = insert(root, 30);
root = insert(root, 40);
root = insert(root, 50);
root = insert(root, 25);
preOrder(root);

}




}