基本概念:
从根结点开始比较,比该结点大放在右子结点;比该结点小放在左子结点
中序遍历:从小到大排序
代码:
//二叉排序树
class BinarySortTree {
private Node root;
public void add(Node node) {
if (root == null)
root = node;
else root.add(node);
}
public void infixOrder() {
if (root == null)
System.out.println("tree is empty");
else
root.infixOrder();
}
public void delete(int value) {
Node parent = root.searchParent(value);
Node node = root.searchNode(value);
if (node == null) {//没找到待删除的结点
System.out.println("未找到该结点");
return;
}
//待删除的是根节点
else if (node == root) {
if (root.left == null && root.right == null)//该树只有一个结点
root = null;
else if (root.left == null)//根节点只有右子