BAEL-3454: Reformat and optimize Code according to review notes

This commit is contained in:
maryarm 2020-01-28 19:47:17 +03:30
parent 6e869cdd4e
commit 6ae2b2df96
1 changed files with 27 additions and 26 deletions

View File

@ -2,10 +2,11 @@ package com.baeldung.algorithms.balancedbinarytree;
public class AVLTree { public class AVLTree {
public static class Node { public class Node {
int key; int key;
int height; int height;
Node left, right; Node left;
Node right;
Node(int key) { Node(int key) {
this.key = key; this.key = key;
@ -18,11 +19,11 @@ public class AVLTree {
Node current = root; Node current = root;
while (current != null) { while (current != null) {
if (current.key == key) { if (current.key == key) {
return current; break;
} }
current = current.key < key ? current.right : current.left; current = current.key < key ? current.right : current.left;
} }
return null; return current;
} }
public void insert(int key) { public void insert(int key) {
@ -37,8 +38,8 @@ public class AVLTree {
return root; return root;
} }
public int height(){ public int height() {
return root == null ? -1: root.height; return root == null ? -1 : root.height;
} }
private Node insert(Node node, int key) { private Node insert(Node node, int key) {
@ -76,41 +77,41 @@ public class AVLTree {
return node; return node;
} }
private Node mostLeftChild(Node node) private Node mostLeftChild(Node node) {
{
Node current = node; Node current = node;
/* loop down to find the leftmost leaf */ /* loop down to find the leftmost leaf */
while (current.left != null) while (current.left != null) {
current = current.left; current = current.left;
}
return current; return current;
} }
private Node rebalance(Node n) { private Node rebalance(Node z) {
updateHeight(n); updateHeight(z);
int balance = getBalance(n); int balance = getBalance(z);
if (balance > 1) { if (balance > 1) {
if (height(n.right.right) > height(n.right.left)) { if (height(z.right.right) > height(z.right.left)) {
n = rotateLeft(n); z = rotateLeft(z);
} else { } else {
n.right = rotateRight(n.right); z.right = rotateRight(z.right);
n = rotateLeft(n); z = rotateLeft(z);
} }
} else if (balance < -1) { } else if (balance < -1) {
if (height(n.left.left) > height(n.left.right)) if (height(z.left.left) > height(z.left.right)) {
n = rotateRight(n); z = rotateRight(z);
else { } else {
n.left = rotateLeft(n.left); z.left = rotateLeft(z.left);
n = rotateRight(n); z = rotateRight(z);
} }
} }
return n; return z;
} }
private Node rotateRight(Node y) { private Node rotateRight(Node y) {
Node x = y.left; Node x = y.left;
Node t2 = x.right; Node z = x.right;
x.right = y; x.right = y;
y.left = t2; y.left = z;
updateHeight(y); updateHeight(y);
updateHeight(x); updateHeight(x);
return x; return x;
@ -118,9 +119,9 @@ public class AVLTree {
private Node rotateLeft(Node y) { private Node rotateLeft(Node y) {
Node x = y.right; Node x = y.right;
Node t2 = x.left; Node z = x.left;
x.left = y; x.left = y;
y.right = t2; y.right = z;
updateHeight(y); updateHeight(y);
updateHeight(x); updateHeight(x);
return x; return x;