forked from NITSkmOS/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AVLDeclaration.java: Add AVL Tree DS
Closes NITSkmOS#49
- Loading branch information
Showing
3 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 + | ||
'}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |