JAVA语言版--手写二叉搜索树

package tree;

/**
 * 二叉查找树
 */
public class MyBinaryTree {

    private TreeNode root;

    /**
     * 节点
     */
    public static class TreeNode {
        Integer data;
        TreeNode leftNode;
        TreeNode rightNode;
        TreeNode parentNode;

        public TreeNode(Integer data) {
            this.data = data;
        }

        public TreeNode(Integer data, TreeNode leftNode, TreeNode rightNode) {
            this.data = data;
            this.leftNode = leftNode;
            this.rightNode = rightNode;
        }

        public Integer getData() {
            return data;
        }

        public void setData(Integer data) {
            this.data = data;
        }

        public TreeNode getLeftNode() {
            return leftNode;
        }

        public void setLeftNode(TreeNode leftNode) {
            this.leftNode = leftNode;
        }

        public TreeNode getRightNode() {
            return rightNode;
        }

        public void setRightNode(TreeNode rightNode) {
            this.rightNode = rightNode;
        }

        public TreeNode getParentNode() {
            return parentNode;
        }

        public void setParentNode(TreeNode parentNode) {
            this.parentNode = parentNode;
        }

        @Override
        public String toString() {
            return "TreeNode{" +
                    "data=" + data +
                    '}';
        }
    }

    public void add(Integer data) {
        root = insert(root, data);
    }

    private TreeNode insert(TreeNode node, Integer data) {
        if (node == null) {
            return new TreeNode(data);
        }

        if (data > node.data) {
            node.rightNode = insert(node.rightNode, data);
            node.rightNode.parentNode = node;
        } else if (data < node.data) {
            node.leftNode = insert(node.leftNode, data);
            node.leftNode.parentNode = node;
        }

        return node;
    }

    public void listTree() {
        listTreeNode(root);
    }

    private void listTreeNode(TreeNode node) {
        if (node == null) {
            return;
        }
        System.out.print(node.data + " ");
        listTreeNode(node.leftNode);
        listTreeNode(node.rightNode);
    }

    public Integer find(Integer data) {
        return findTreeNode(root, data).data;
    }

    public TreeNode findTreeNode(TreeNode node, Integer data) {
        if (data > node.data) {
            return findTreeNode(node.rightNode, data);
        } else if (data < node.data) {
            return findTreeNode(node.leftNode, data);
        } else if (node != null && node.data.equals(data)) {
            return node;
        }
        return null;
    }

    public boolean remove(Integer data) {
        TreeNode node = findTreeNode(root, data);
        if (node == null) {
            return false;
        }

        TreeNode parNode = node.parentNode;
        boolean isLeft = false;
        if (node.data.equals(parNode.leftNode.data)) {
            isLeft = true;
        }

        if (node.leftNode == null && node.rightNode == null) {
            if (isLeft) {
                parNode.leftNode = null;
            } else {
                parNode.rightNode = null;
            }
            return true;
        } else if (node.rightNode != null && node.leftNode == null) {
            if (isLeft) {
                parNode.leftNode = node.rightNode;
            } else {
                parNode.rightNode = node.rightNode;
            }
            return true;
        } else if (node.leftNode != null && node.rightNode == null) {
            if (isLeft) {
                parNode.leftNode = node.leftNode;
            } else {
                parNode.rightNode = node.leftNode;
            }
            return true;
        } else if (node.leftNode != null && node.rightNode != null) {
            TreeNode minNode = findMinTreeNode(node.rightNode);
            remove(minNode.data);
            node.data = minNode.data;
            return true;
        }

        return false;
    }

    private TreeNode findMinTreeNode(TreeNode node) {
        TreeNode minNode = node;
        while (minNode != null) {
            if (minNode.leftNode == null) {
                return minNode;
            }
            minNode = minNode.leftNode;
        }
        return minNode;
    }

    public TreeNode getRoot() {
        return root;
    }

    public void setRoot(TreeNode root) {
        this.root = root;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值