forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVLTree.java
More file actions
143 lines (125 loc) · 3.58 KB
/
AVLTree.java
File metadata and controls
143 lines (125 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.thealgorithms.datastructures.trees;
/**
* AVL Tree (Adelson-Velsky and Landis Tree) implementation.
* A self-balancing Binary Search Tree where the difference between heights
* of left and right subtrees cannot be more than one for all nodes.
*
* @author Raghu0703
*/
public final class AVLTree {
static class Node {
int data;
int height;
Node left;
Node right;
Node(int data) {
this.data = data;
this.height = 1;
this.left = null;
this.right = null;
}
}
private Node root;
public AVLTree() {
this.root = null;
}
private int height(Node node) {
return node == null ? 0 : node.height;
}
private int getBalance(Node node) {
return node == null ? 0 : height(node.left) - height(node.right);
}
private void updateHeight(Node node) {
node.height = Math.max(height(node.left), height(node.right)) + 1;
}
private Node rightRotate(Node y) {
Node x = y.left;
Node t2 = x.right;
x.right = y;
y.left = t2;
updateHeight(y);
updateHeight(x);
return x;
}
private Node leftRotate(Node x) {
Node y = x.right;
Node t2 = y.left;
y.left = x;
x.right = t2;
updateHeight(x);
updateHeight(y);
return y;
}
public void insert(int value) {
root = insertRec(root, value);
}
private Node insertRec(Node node, int value) {
if (node == null) {
return new Node(value);
}
if (value < node.data) {
node.left = insertRec(node.left, value);
} else if (value > node.data) {
node.right = insertRec(node.right, value);
} else {
return node;
}
updateHeight(node);
int balance = getBalance(node);
if (balance > 1 && value < node.left.data) {
return rightRotate(node);
}
if (balance < -1 && value > node.right.data) {
return leftRotate(node);
}
if (balance > 1 && value > node.left.data) {
node.left = leftRotate(node.left);
return rightRotate(node);
}
if (balance < -1 && value < node.right.data) {
node.right = rightRotate(node.right);
return leftRotate(node);
}
return node;
}
public boolean search(int value) {
return searchRec(root, value);
}
private boolean searchRec(Node node, int value) {
if (node == null) {
return false;
}
if (value == node.data) {
return true;
}
return value < node.data ? searchRec(node.left, value) : searchRec(node.right, value);
}
public boolean isEmpty() {
return root == null;
}
public int getHeight() {
return height(root);
}
public boolean isBalanced() {
return isBalancedRec(root);
}
private boolean isBalancedRec(Node node) {
if (node == null) {
return true;
}
int balance = getBalance(node);
return Math.abs(balance) <= 1 && isBalancedRec(node.left) && isBalancedRec(node.right);
}
public String inorder() {
StringBuilder sb = new StringBuilder();
inorderRec(root, sb);
return sb.toString().trim();
}
private void inorderRec(Node node, StringBuilder sb) {
if (node != null) {
inorderRec(node.left, sb);
sb.append(node.data).append(" ");
inorderRec(node.right, sb);
}
}
}