From b36d60295d257c034ea2c66eb93eec5dfcda1f85 Mon Sep 17 00:00:00 2001 From: Aquilll Date: Mon, 1 Oct 2018 23:43:27 +0530 Subject: [PATCH] AVLDeclaration.java: Add AVL Tree DS Closes https://github.com/NITSkmOS/Algorithms/issues/49 --- README.md | 1 + avl_tree/Java/AVLDeclaration.java | 53 +++++++++++++++++ avl_tree/Java/main.java | 94 +++++++++++++++++++++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 avl_tree/Java/AVLDeclaration.java create mode 100644 avl_tree/Java/main.java diff --git a/README.md b/README.md index 3c7fd842..9085b98f 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ This repository contains examples of various algorithms written on different pro |:----------------------------------------------------------------------------------------------- |:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|:-------------------------------------:| | [Queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)) | | [:octocat:](queue/Cpp) | | | | [Linear Linked List](https://en.wikipedia.org/wiki/Linked_list) | [:octocat:](linked_list/C) | | | | +| [AVL Tree](https://en.wikipedia.org/wiki/AVL_tree) | | | [:octocat:](avl_tree/Java) | | ## Sample Run diff --git a/avl_tree/Java/AVLDeclaration.java b/avl_tree/Java/AVLDeclaration.java new file mode 100644 index 00000000..f6ddbf65 --- /dev/null +++ b/avl_tree/Java/AVLDeclaration.java @@ -0,0 +1,53 @@ +public class AVLDeclaration { + private int data; + private int height; + private AVLDeclaration left; + private AVLDeclaration right; + + public AVLDeclaration(int data) { + this.data = data; + } + + public int getData() { + return data; + } + + public void setData(int data){ + this.data = data; + } + + public void setHeigth(int height){ + this.height = height; + } + + public int getHeight() { + return height; + } + + public AVLDeclaration getleft() { + return left; + } + + public void setleft(AVLDeclaration left) { + this.left = left; + } + + public AVLDeclaration getright() { + return right; + } + + public void setRight(AVLDeclaration left) { + this.right = right; + } + + + @Override + public String toString() { + return "AVLDeclaration{" + + "data=" + data + + ", height=" + height + + ", left=" + left + + ", right=" + right + + '}'; + } +} diff --git a/avl_tree/Java/main.java b/avl_tree/Java/main.java new file mode 100644 index 00000000..80146eb9 --- /dev/null +++ b/avl_tree/Java/main.java @@ -0,0 +1,94 @@ +public class AVLOPERATONS { + public static void main(String[] args) { + AVLDeclaration root = new AVLDeclaration(56); + AVLDeclaration left1 = new AVLDeclaration(48); + AVLDeclaration right1 = new AVLDeclaration(59); + AVLDeclaration left12 = new AVLDeclaration(32); + AVLDeclaration right12 = new AVLDeclaration(50); + AVLDeclaration right11 = new AVLDeclaration(60); + AVLDeclaration right111 = new AVLDeclaration(62); + + root.setHeigth(1); + root.setleft(left1); + root.setRight(right1); + left1.setleft(left12); + left1.setRight(right12); + right1.setRight(right11); + right1.setHeigth(2); + right11.setRight(right111); + right11.setHeigth(2); + right111.setHeigth(3); + left1.setHeigth(2); + left12.setHeigth(2); + right12.setHeigth(3); + } + + public int height(AVLDeclaration root){ + if(root == null) + return -1; + else + return root.getHeight(); + } + + + public AVLDeclaration leftLeftRotation(AVLDeclaration x){ + AVLDeclaration w = x.getleft(); + x.setleft(w.getright()); + w.setRight(x); + x.setHeigth(Math.max(height(x.getleft()),height(x.getright()))+1); + w.setHeigth(Math.max(height(w.getleft()),height(x))+1); + + return w; + } + + public AVLDeclaration rightRightRotation(AVLDeclaration x){ + AVLDeclaration w = x.getright(); + x.setRight(w.getleft()); + w.setleft(x); + x.setHeigth(Math.max(height(x.getleft()),height(x.getright()))+1); + w.setHeigth(Math.max(height(w.getright()),height(x))+1); + + return w; + } + + public AVLDeclaration leftRightrotation(AVLDeclaration z){ + z.setleft(rightRightRotation(z.getleft())); + return leftLeftRotation(z); + } + + public AVLDeclaration rightLeftRotation(AVLDeclaration z){ + z.setRight(leftLeftRotation(z.getright())); + return rightRightRotation(z); + } + + + public AVLDeclaration insert(AVLDeclaration root, AVLDeclaration parent, int data){ + if(root == null){ + root = new AVLDeclaration(data); + root.setHeigth(0); + root.setleft(null); + root.setRight(null); + } + else if(data < root.getData()){ + root.setleft(insert(root.getleft(), root, data)); + if(height(root.getleft()) - height(root.getright()) == 2){ + if(data < root.getleft().getData()) + root = leftLeftRotation(root); + else + root = leftRightrotation(root); + } + } + else if(data > root.getData()){ + root.setRight(insert(root.getright(), root, data)); + if(height(root.getright()) - height(root.getleft()) == 2){ + if(data < root.getright().getData()) + root = rightRightRotation(root); + else + root = rightLeftRotation(root); + } + } + root.setHeigth(Math.max(height(root.getleft()), height(root.getright())) + 1); + return root; + } + +}