-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathAVLTree.java
144 lines (141 loc) · 4.25 KB
/
AVLTree.java
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
144
package edu.miracosta.cs113;
public class AVLTree<E extends Comparable<E>> extends BinarySearchTreeWithRotate<E>
{
private static class AVLNode<E> extends BinaryTree.Node<E>
{
public static final int LEFT_HEAVY = -1;
public static final int BALANCED = 0;
public static final int RIGHT_HEAVY = 1;
private int balance;
public AVLNode(E item)
{
super(item);
balance = BALANCED;
}
public String toString()
{
return balance + ": " + super.toString();
}
}
private boolean increase;
@Override
public boolean add(E item)
{
increase = false;
root = add((AVLNode<E>) root, item);
return addReturn;
}
private AVLNode<E> add(AVLNode<E> localRoot, E item)
{
if(localRoot == null)
{
addReturn = true;
increase = true;
return new AVLNode<E>(item);
}
if(item.compareTo(localRoot.data) == 0)
{
increase = false;
addReturn = false;
return localRoot;
}
else if(item.compareTo(localRoot.data) < 0)
{
localRoot.left = add((AVLNode<E>) localRoot.left, item);
if (increase)
{
decrementBalance(localRoot);
if (localRoot.balance < AVLNode.LEFT_HEAVY)
{
increase = false;
return rebalanceLeft(localRoot);
}
}
return localRoot;
}
else
{
localRoot.right = add((AVLNode<E>) localRoot.right, item);
if (increase)
{
incrementBalance(localRoot);
if (localRoot.balance > AVLNode.RIGHT_HEAVY)
{
increase = false;
return rebalanceRight(localRoot);
}
}
return localRoot;
}
}
private void decrementBalance(AVLNode<E> node)
{
node.balance--;
if (node.balance == AVLNode.BALANCED)
{
increase = false;
}
}
private void incrementBalance(AVLNode<E> node)
{
node.balance++;
if(node.balance == AVLNode.BALANCED)
{
increase = false;
}
}
private AVLNode<E> rebalanceLeft(AVLNode<E> localRoot)
{
AVLNode<E> leftChild = (AVLNode<E>) localRoot.left;
if(leftChild.balance > AVLNode.BALANCED)
{
AVLNode<E> leftRightChild = (AVLNode<E>) leftChild.right;
if(leftRightChild.balance < AVLNode.BALANCED)
{
leftChild.balance = AVLNode.BALANCED;
leftRightChild.balance = AVLNode.BALANCED;
localRoot.balance = AVLNode.BALANCED;
}
else
{
leftChild.balance = AVLNode.LEFT_HEAVY;
leftRightChild.balance = AVLNode.BALANCED;
localRoot.balance = AVLNode.BALANCED;
}
localRoot.left = rotateLeft(leftChild);
}
else
{
leftChild.balance = AVLNode.BALANCED;
localRoot.balance = AVLNode.BALANCED;
}
return (AVLNode<E>) rotateRight(localRoot);
}
private AVLNode<E> rebalanceRight(AVLNode<E> localRoot)
{
AVLNode<E> rightChild = (AVLNode<E>) localRoot.right;
if(rightChild.balance < AVLNode.BALANCED)
{
AVLNode<E> rightLeftChild = (AVLNode<E>) rightChild.left;
if(rightLeftChild.balance > AVLNode.BALANCED)
{
rightChild.balance = AVLNode.BALANCED;
rightLeftChild.balance = AVLNode.BALANCED;
localRoot.balance = AVLNode.BALANCED;
}
else
{
rightChild.balance = AVLNode.RIGHT_HEAVY;
rightLeftChild.balance = AVLNode.BALANCED;
localRoot.balance = AVLNode.BALANCED;
}
localRoot.right = rotateRight(rightChild);
}
else
{
rightChild.balance = AVLNode.BALANCED;
localRoot.balance = AVLNode.BALANCED;
}
return (AVLNode<E>) rotateLeft(localRoot);
}
}