Skip to content

Commit 3715c98

Browse files
committed
feat: 리밸런싱에 필요한 도구 리밸런스 메서드 정의
1 parent 2ad1eec commit 3715c98

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

yoonexample/src/main/java/search/binarysearchtree/AVLTreeNode.java

+19
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,23 @@ private BinaryTreeNode<E> rotateRL(BinaryTreeNode<E> binaryTreeNode) {
7171
parentNode.changeRightSubTree(rotateLL(childNode)); // 부분적 LL회전
7272
return rotateRR(parentNode); // RR회전
7373
}
74+
75+
private void rebalance() {
76+
int equilibriumFactor = getEquilibriumFactor(super.node);
77+
78+
if (equilibriumFactor > 1) { // LL OR LR
79+
if (getEquilibriumFactor(super.node.getLeftSubTree()) > 0) {
80+
super.node = rotateLL(super.node);
81+
} else {
82+
super.node = rotateLR(super.node);
83+
}
84+
}
85+
if (equilibriumFactor < -1) { // RR OR RL
86+
if (getEquilibriumFactor(super.node.getRightSubTree()) < 0) {
87+
super.node = rotateRR(super.node);
88+
} else {
89+
super.node = rotateRL(super.node);
90+
}
91+
}
92+
}
7493
}

yoonexample/src/main/java/search/binarysearchtree/LinkedBinarySearchTreeNode.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
public class LinkedBinarySearchTreeNode<E> implements BinarySearchTreeNode<E> {
88

99
private final Comparator<E> comparator;
10-
private BinaryTreeNode<E> node;
10+
protected BinaryTreeNode<E> node;
1111

1212
public LinkedBinarySearchTreeNode(E data, Comparator<E> comparator) {
1313
this.node = new LinkedBinaryTreeNode<>(data);

0 commit comments

Comments
 (0)