X Tutup
Skip to content

Commit 32059e6

Browse files
committed
修改平衡二叉树删除时的平衡操作
1 parent 1f22532 commit 32059e6

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

.idea/misc.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/com/zejian/structures/Tree/AVLTree/AVLTree.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,11 @@ private AVLNode<T> remove(T data,AVLNode<T> p){
239239
AVLNode<T> currentNode=p.right;
240240
//判断需要那种旋转
241241
if(height(currentNode.left)>height(currentNode.right)){
242-
//LL
243-
p=singleRotateLeft(p);
242+
//RL
243+
p=doubleRotateWithRight(p);
244244
}else{
245-
//LR
246-
p=doubleRotateWithLeft(p);
245+
//RR
246+
p=singleRotateRight(p);
247247
}
248248
}
249249

@@ -256,11 +256,11 @@ else if(result>0){
256256
AVLNode<T> currentNode=p.left;
257257
//判断需要那种旋转
258258
if(height(currentNode.right)>height(currentNode.left)){
259-
//RR
260-
p=singleRotateRight(p);
259+
//LR
260+
p=doubleRotateWithLeft(p);
261261
}else{
262-
//RL
263-
p=doubleRotateWithRight(p);
262+
//LL
263+
p=singleRotateLeft(p);
264264
}
265265
}
266266
}
@@ -442,7 +442,8 @@ public static void main(String arg[]){
442442

443443
avlTree.printTree(avlTree.root);
444444

445-
avlTree.remove(7);
445+
avlTree.remove(11);
446+
avlTree.remove(8);
446447

447448
System.out.println("================");
448449

0 commit comments

Comments
 (0)
X Tutup