-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVL1.java
150 lines (138 loc) · 5.88 KB
/
AVL1.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
145
146
147
148
149
150
/**
* Your implementation of the AVL tree rotations.
*/
public class AVL<T extends Comparable<? super T>> {
/**
* DO NOT ADD ANY GLOBAL VARIABLES!
*/
/**
* Updates the height and balance factor of a node using its
* setter methods.
*
* Recall that a null node has a height of -1. If a node is not
* null, then the height of that node will be its height instance
* data. The height of a node is the max of its left child's height
* and right child's height, plus one.
*
* The balance factor of a node is the height of its left child
* minus the height of its right child.
*
* This method should run in O(1).
* You may assume that the passed in node is not null.
*
* This method should only be called in rotateLeft(), rotateRight(),
* and balance().
*
* @param currentNode The node to update the height and balance factor of.
*/
public void updateHeightAndBF(AVLNode<T> currentNode) {
// WRITE YOUR CODE HERE (DO NOT MODIFY METHOD HEADER)!
int leftHeight = -1;
int rightHeight = -1;
if (currentNode.getLeft() != null) {
leftHeight = currentNode.getLeft().getHeight();
}
if (currentNode.getRight() != null) {
rightHeight = currentNode.getRight().getHeight();
}
currentNode.setHeight(Math.max(leftHeight, rightHeight) + 1);
currentNode.setBalanceFactor(leftHeight - rightHeight);
}
/**
* Method that rotates a current node to the left. After saving the
* current's right node to a variable, the right node's left subtree will
* become the current node's right subtree. The current node will become
* the right node's left subtree.
*
* Don't forget to recalculate the height and balance factor of all
* affected nodes, using updateHeightAndBF().
*
* This method should run in O(1).
*
* You may assume that the passed in node is not null and that the subtree
* starting at that node is right heavy. Therefore, you do not need to
* perform any preliminary checks, rather, you can immediately perform a
* left rotation on the passed in node and return the new root of the subtree.
*
* This method should only be called in balance().
*
* @param currentNode The current node under inspection that will rotate.
* @return The parent of the node passed in (after the rotation).
*/
public AVLNode<T> rotateLeft(AVLNode<T> currentNode) {
// WRITE YOUR CODE HERE (DO NOT MODIFY METHOD HEADER)!
AVLNode<T> rightNode = currentNode.getRight();
currentNode.setRight(rightNode.getLeft());
rightNode.setLeft(currentNode);
updateHeightAndBF(currentNode);
updateHeightAndBF(rightNode);
return rightNode;
}
/**
* Method that rotates a current node to the right. After saving the
* current's left node to a variable, the left node's right subtree will
* become the current node's left subtree. The current node will become
* the left node's right subtree.
*
* Don't forget to recalculate the height and balance factor of all
* affected nodes, using updateHeightAndBF().
*
* This method should run in O(1).
*
* You may assume that the passed in node is not null and that the subtree
* starting at that node is left heavy. Therefore, you do not need to perform
* any preliminary checks, rather, you can immediately perform a right
* rotation on the passed in node and return the new root of the subtree.
*
* This method should only be called in balance().
*
* @param currentNode The current node under inspection that will rotate.
* @return The parent of the node passed in (after the rotation).
*/
public AVLNode<T> rotateRight(AVLNode<T> currentNode) {
// WRITE YOUR CODE HERE (DO NOT MODIFY METHOD HEADER)!
AVLNode<T> leftNode = currentNode.getLeft();
currentNode.setLeft(leftNode.getRight());
leftNode.setRight(currentNode);
updateHeightAndBF(currentNode);
updateHeightAndBF(leftNode);
return leftNode;
}
/**
* This is the overarching method that is used to balance a subtree
* starting at the passed in node. This method will utilize
* updateHeightAndBF(), rotateLeft(), and rotateRight() to balance
* the subtree. In part 2 of this assignment, this balance() method
* will be used in your add() and remove() methods.
*
* The height and balance factor of the current node is first recalculated.
* Based on the balance factor, a no rotation, a single rotation, or a
* double rotation takes place. The current node is returned.
*
* You may assume that the passed in node is not null. Therefore, you do
* not need to perform any preliminary checks, rather, you can immediately
* check to see if any rotations need to be performed.
*
* This method should run in O(1).
*
* @param cur The current node under inspection.
* @return The AVLNode that the caller should return.
*/
public AVLNode<T> balance(AVLNode<T> currentNode) {
// WRITE YOUR CODE HERE (DO NOT MODIFY METHOD HEADER)!
/* First, we update the height and balance factor of the current node. */
updateHeightAndBF(currentNode);
if ( currentNode.getBalanceFactor() < -1 ) {
if ( currentNode.getRight().getBalanceFactor() >=1 ) {
currentNode.setRight(rotateRight(currentNode.getRight()));
}
currentNode = rotateLeft(currentNode);
} else if ( currentNode.getBalanceFactor() > 1) {
if ( currentNode.getLeft().getBalanceFactor() <= -1 ) {
currentNode.setLeft(rotateLeft(currentNode.getLeft()));
}
currentNode = rotateRight(currentNode);
}
return currentNode;
}
}