java binary tree

package com.gxmedu.binary_tree;

/**
 * @author 郭学明
 * @version 1.0
 */
public class BinaryTree {
    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();
        Node root = binaryTree.new Node(1, "宋江");
        Node node2 = binaryTree.new Node(2, "吴用");
        Node node3 = binaryTree.new Node(3,"卢俊义");
        Node node4 = binaryTree.new Node(4,"林冲");
        Node node5 = binaryTree.new Node(5,"关胜");
        binaryTree.setRoot(root);
        root.setLeft(node2);
        root.setRight(node3);
        node3.setLeft(node5);
        node3.setRight(node4);
        binaryTree.preorder();
//        binaryTree.deleteNode(3);
//        binaryTree.deleteOneNode(3);
        System.out.println("======================");
        binaryTree.preorder();
        System.out.println("======================");
        binaryTree.inorder();
        System.out.println("======================");
        binaryTree.postorder();
        System.out.println("======================");
        Node result = binaryTree.preorderSearch(4);
        System.out.println(result);
    }
    private Node root;

    public Node getRoot() {
        return root;
    }

    public void setRoot(Node node){
        this.root = node;
    }
    public void preorder(){
        if(root != null){
            root.preorder();
        }else{
            System.out.println("树为空,无法遍历");
        }
    }
    public void inorder(){
        if(root != null){
            root.inorder();
        }else{
            System.out.println("树为空,无法遍历");
        }
    }
    public void postorder(){
        if(root != null){
            root.postorder();
        }else{
            System.out.println("树为空,无法遍历");
        }
    }
    public Node preorderSearch(int id){
        if(root != null){
            return root.preorderSearch(id);
        }else{
            return null;
        }
    }
    public Node inorderSearch(int id){
        if(root != null){
            return root.inorderSearch(id);
        }else{
            return null;
        }
    }
    public Node postorderSearch(int id){
        if(root != null){
            return root.postorderSearch(id);
        }else{
            return null;
        }
    }
    public void deleteNode(int id){
        if(root == null){
            System.out.println("树为空,无法删除");
        }else if(root.id == id){
            root = null;
        }else{
            root.deleteNode(id);
        }
    }
    public void deleteOneNode(int id){
        if(root == null){
            System.out.println("树为空,无法删除");
        }else if(root.id == id){
            root = root.inherit();
        }else{
            root.deleteOneNode(id);
        }
    }
    public class Node{
        private int id;
        private String name;
        private Node left;
        private Node right;
        /**
         *  遍历后续线索化二叉树会用到
         */
        private Node parent;
        private boolean traverseRightChildTree;

        public boolean isTraverseRightChildTree() {
            return traverseRightChildTree;
        }

        public void setTraverseRightChildTree(boolean traverseRightChildTree) {
            this.traverseRightChildTree = traverseRightChildTree;
        }

        public Node getParent() {
            return parent;
        }

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

        /**
         *  在线索二叉树中会用到
         */
        private int leftNodeType;
        private int rightNodeType;
        public int getLeftNodeType() {
            return leftNodeType;
        }

        public void setLeftNodeType(int leftNodeType) {
            this.leftNodeType = leftNodeType;
        }

        public int getRightNodeType() {
            return rightNodeType;
        }

        public void setRightNodeType(int rightNodeType) {
            this.rightNodeType = rightNodeType;
        }
        public void preorder(){
            System.out.println(this);
            if(this.left != null){
                this.left.preorder();
            }
            if(this.right != null){
                this.right.preorder();
            }
        }
        public void inorder(){
            if(this.left != null){
                this.left.inorder();
            }
            System.out.println(this);
            if(this.right != null){
                this.right.inorder();
            }
        }
        public void postorder(){
            if(this.left != null){
                this.left.postorder();
            }
            if(this.right != null){
                this.right.postorder();
            }
            System.out.println(this);
        }
        public Node preorderSearch(int id){
            if(this.id == id){
                return this;
            }
            /**
             *  还需要判断递归结果是否为空
             */
            Node result = null;
            if(this.left != null){
                result = this.left.preorderSearch(id);
            }
            if(result != null){
                return result;
            }
            if(this.right != null){
                result =  this.right.preorderSearch(id);
            }
            return result;
        }
        public Node inorderSearch(int id){
            Node result = null;
            if(this.left != null){
                result = this.left.inorderSearch(id);
            }
            if(result != null){
                return result;
            }
            if(this.id == id){
                return this;
            }
            if(this.right != null){
                result = this.right.inorderSearch(id);
            }
            return result;
        }
        public Node postorderSearch(int id){
            Node result = null;
            if(this.left != null){
                result = this.left.postorderSearch(id);
            }
            if(result != null){
                return result;
            }
            if(this.right != null){
                result = this.right.postorderSearch(id);
            }
            if(result != null){
                return result;
            }
            if(this.id == id){
                return this;
            }
            return result;
        }
        public void deleteNode(int id){
            if(this.left != null && this.left.id == id){
                this.left = null;
                return;
            }
            if(this.right != null && this.right.id == id){
                this.right = null;
                return;
            }
            if(this.left != null){
                this.left.deleteNode(id);
            }
            if(this.right != null){
                this.right.deleteNode(id);
            }
        }

        /**
         *  只删除一个节点,其位置将按左子节点到右子节点的顺序继承,这里需要额外写一个方法实现继承顺序
         * @param id
         */
        public void deleteOneNode(int id){
            if(this.left != null && this.left.id == id){
                    this.left = this.left.inherit();
                    return;
            }
            if(this.right != null && this.right.id == id){
                this.right = this.right.inherit();
                return;
            }
            if(this.left != null){
                this.left.deleteOneNode(id);
            }
            if(this.right != null){
                this.right.deleteOneNode(id);
            }

        }
        public Node inherit(){
            if(this.left != null){
                return this.left;
            }
            if(this.right != null){
                return this.right;
            }
            return null;
        }
        public Node(int id,String name){
            this.id = id;
            this.name = name;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Node getLeft() {
            return left;
        }

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

        public Node getRight() {
            return right;
        }

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

        @Override
        public String toString() {
            return "Node{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值