Skip to content

Commit 26febd6

Browse files
author
robert
committed
fix BST bug
1 parent 7d9c1ab commit 26febd6

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/com/algorithm/tree/BinarySeachTree.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public BinaryTreeNode<T> findMin(BinaryTreeNode<T> node) {
123123
* @return
124124
*/
125125
public BinaryTreeNode<T> findMax() {
126-
return findMin(root);
126+
return findMax(root);
127127
}
128128

129129
/**
@@ -138,7 +138,7 @@ public BinaryTreeNode<T> findMax(BinaryTreeNode<T> node) {
138138
} else if (node.right == null) {
139139
return node;
140140
}
141-
return findMin(node.right);
141+
return findMax(node.right);
142142
}
143143

144144
/**
@@ -168,8 +168,10 @@ public BinaryTreeNode<T> remove(BinaryTreeNode<T> cur, BinaryTreeNode<T> x) {
168168
} else if (compareresult < 0) {
169169
cur.right = remove(cur.right, x);
170170
} else if (cur.left != null && cur.right != null) {
171-
cur.right = remove(cur.right, findMin(cur.right));
172-
171+
BinaryTreeNode<T> minnode = findMin(cur.right);
172+
cur.right = remove(cur.right, minnode);
173+
cur.element = minnode.element;
174+
minnode = null;
173175
} else {
174176
cur = (cur.left == null) ? cur.right : cur.left;
175177
}

0 commit comments

Comments
 (0)