用Java实现一个简单的二叉树

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 一个简单二叉树结构
 * @author tang
 *
 */
public class Tree {

    private Node    root;

    public void add(Node node) {
        if (root == null) {
            root = node;
        } else {
            Node temp = root;
            while (true) {
                if (node.getValue() <= temp.getValue()) {// 往左找
                    if (temp.getLeft() != null) {
                        temp = temp.getLeft();
                    } else {// 找到了
                        temp.setLeft(node);
                        node.setParent(temp);
                        break;
                    }
                } else {// 往右找
                    if (temp.getRight() != null) {
                        temp = temp.getRight();
                    } else {// 找到了
                        temp.setRight(node);
                        node.setParent(temp);
                        break;
                    }
                }
            }
        }
    }

    public boolean remove(Node node) {
        Node find = find(root, node);
        if (find != null) {
            Node parent = find.getParent();
            if (parent == null) {// 如果要删除的节点是根节点

                Node left = find.getLeft();
                left.setParent(null);
                root=left;

                Node right = find.getRight();
                Node leftMaxNode = getMaxNode(left);
                leftMaxNode.setRight(right);
                right.setParent(leftMaxNode);

            } else if (parent.getLeft() == find) {// 如果要删除的节点在父节点的左侧

                Node left = find.getLeft();
                left.setParent(parent);
                parent.setLeft(left);

                Node right = find.getRight();
                Node leftMaxNode = getMaxNode(left);
                leftMaxNode.setRight(right);
                right.setParent(leftMaxNode);

            } else {// 如果要删除的节点在父节点的右侧

                Node right = find.getRight();
                right.setParent(parent);
                parent.setRight(right);

                Node left = find.getLeft();
                Node rightMinNode = getMinNode(right);
                rightMinNode.setLeft(left);
                left.setParent(rightMinNode);
            }
            find.setLeft(null);
            find.setRight(null);
            find.setParent(null);
            return true;
        }
        return false;
    }

    private Node getMinNode(Node node) {
        Node min = node;
        if (min != null) {
            while (min.getLeft() != null) {
                min = min.getLeft();
            }
        }
        return min;
    }

    private Node getMaxNode(Node node) {
        Node max = node;
        if (max != null) {
            while (max.getRight() != null) {
                max = max.getRight();
            }
        }
        return max;
    }

    private Node find(Node node, Node target) {
        if (node == target) {
            return node;
        } else {
            Node left = node.getLeft();
            if (left != null) {
                return find(left, target);
            }
            Node right = node.getRight();
            if (right != null) {
                return find(right, target);
            }
            return null;
        }
    }

    public boolean contains(Node node) {
        Node find = find(root, node);
        if (find == null) {
            return false;
        } else {
            return true;
        }
    }

    private void toList(List<Node> list, Node node) {
        if (node != null) {
            Node left = node.getLeft();
            if (left != null) {
                toList(list, left);
            }

            list.add(node);

            Node right = node.getRight();
            if (right != null) {
                toList(list, right);
            }
        }
    }

    private List<Node> toList() {
        List<Node> list = new ArrayList<>();
        toList(list, root);
        return list;
    }

    public Iterator<Node> iterator() {
        return toList().iterator();
    }

    public int size() {
        return toList().size();
    }

    @Override
    public String toString() {
        return toList().toString();
    }

    public static void main(String[] args) {

        Tree tree = new Tree();
        List<Node> list = new ArrayList<>();
        list.add(new Node(10));
        list.add(new Node(5));
        list.add(new Node(15));
        list.add(new Node(3));
        list.add(new Node(8));
        list.add(new Node(12));
        list.add(new Node(20));

        list.add(new Node(1));
        list.add(new Node(4));
        list.add(new Node(6));
        list.add(new Node(9));
        list.add(new Node(11));
        list.add(new Node(13));
        list.add(new Node(16));
        list.add(new Node(20));

        for (Node treeNode : list) {
            tree.add(treeNode);
        }

        System.out.println(tree);
        tree.remove(list.get(1));
        tree.remove(list.get(0));
        System.out.println(tree);
    }

    /**
     * 二叉树节点
     * @author tang
     */
    public static class Node {

        private Node    parent;
        private Node    left;
        private Integer     value;
        private Node    right;

        public Node(Integer value) {
            this.value = value;
        }

        public Node getParent() {
            return parent;
        }

        public void setParent(Node parent) {
            this.parent = parent;
        }

        public Node getLeft() {
            return left;
        }

        public void setLeft(Node left) {
            this.left = left;
        }

        public Integer getValue() {
            return value;
        }

        public Node getRight() {
            return right;
        }

        public void setRight(Node right) {
            this.right = right;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((value == null) ? 0 : value.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Node other = (Node) obj;
            if (value == null) {
                if (other.value != null)
                    return false;
            } else
                if (!value.equals(other.value))
                    return false;
            return true;
        }

        @Override
        public String toString() {
            return value.toString();
        }
    }
}

这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值