Repository navigation
Expand file tree
/
Copy pathAVLTree2.java
More file actions
141 lines (114 loc) · 3.18 KB
/
Copy pathAVLTree2.java
File metadata and controls
141 lines (114 loc) · 3.18 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
package com.tree.avl;
/**
* @author xjn
* @since 2020-01-06
*/
public class AVLTree2<E extends Comparable<E>> {
private class Node {
public E e;
public Node left;
public Node right;
public int height;
public Node(E e) {
this.e = e;
this.left = null;
this.right = null;
this.height = 0;
}
public Node(Node node) {
this.e = node.e;
this.left = node.left;
this.right = node.left;
this.height = node.height;
}
@Override
public String toString() {
return e.toString() + " ";
}
}
private Node root;
private int size;
public AVLTree2() {
this.root = null;
this.size = 0;
}
public boolean isEmpty() {
return this.size == 0;
}
public int getSize() {
return this.size;
}
private int getHeight(Node node) {
if (node == null) {
return 0;
}
return node.height;
}
private int getBalanceFactor(Node node) {
if (node == null) {
return 0;
}
return getHeight(node.left) - getHeight(node.right);
}
private boolean isBalanced() {
return isBalanced(root);
}
private boolean isBalanced(Node node) {
if (node == null) {
return true;
}
int balanceFactor = getBalanceFactor(node);
if (Math.abs(balanceFactor) >= 1) {
return false;
}
return isBalanced(node.left) && isBalanced(node.right);
}
public void add(E e) {
}
private Node addRecursive(Node node, E e) {
if (node == null) {
size++;
return new Node(e);
}
if (e.compareTo(node.e) < 0) {
node.left = addRecursive(node.left, e);
} else if (e.compareTo(node.e) > 0) {
node.right = addRecursive(node.right, e);
}
node.height = Math.max(getHeight(node.left), getHeight(node.right)) + 1;
//LL
if (getBalanceFactor(node) > 1 && getBalanceFactor(node.left) >= 0) {
return rotateRight(node);
}
//RR
if (getBalanceFactor(node) < -1 && getBalanceFactor(node.right) <= 0) {
return rotateLeft(node);
}
//LR
if(getBalanceFactor(node) > 1 && getBalanceFactor(node.left) < 0){
node.left = rotateLeft(node.left);
}
//RL
if(getBalanceFactor(node) < -1 && getBalanceFactor(node.right) > 0){
}
return null;
}
private Node rotateRight(Node y) {
Node x = y.left;
Node t3 = x.right;
x.right = y;
y.left = t3;
y.height = Math.max(getHeight(y.left), getHeight(y.right)) + 1;
x.height = Math.max(getHeight(x.left), getHeight(x.right)) + 1;
return x;
}
private Node rotateLeft(Node y) {
Node x = y.right;
Node t3 = x.left;
y.right = t3;
x.left = y;
y.height = Math.max(getHeight(y.left), getHeight(y.right)) + 1;
x.height = Math.max(getHeight(x.left), getHeight(x.right)) + 1;
return x;
}
}