二叉树的简单实现(Java)

此文简单实现了二叉树的前中后序遍历、前中后序遍历查找以及删除节点。

1.定义节点

package tree;

import spares.SparesArray;

public class Node {
    private int no;
    private String name;

    private Node left;
    private Node right;

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

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    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;
    }

    public Node(int no, String name) {
        this.no = no;
        this.name = name;
    }

    /**
     * 前中后序遍历
     */
    public void preList() {

        System.out.println(this);

        if (this.left != null) {
            this.left.preList();
        }

        if (this.right != null) {
            this.right.preList();
        }

    }

    public void midList() {

        if (this.left != null) {
            this.left.midList();
        }

        System.out.println(this);

        if (this.right != null) {
            this.right.midList();
        }

    }

    public void lastList() {

        if (this.left != null) {
            this.left.lastList();
        }

        if (this.right != null) {
            this.right.lastList();
        }

        System.out.println(this);

    }

    /**
     * 前中后序遍历查找
     */
    public Node preSearch(int no) {
        if (this.no == no) {
            return this;
        }

        Node temp = null;

        if (this.left != null) {
            temp = this.left.preSearch(no);
        }
        if (temp != null) {
            return temp;
        }

        if (this.right != null) {
            temp = this.right.preSearch(no);
        }

        return temp;

    }

    public Node midSearch(int no) {

        Node temp = null;

        if (this.left != null) {
            temp = this.left.midSearch(no);
        }
        if (temp != null) {
            return temp;
        }

        if (this.no == no) {
            return this;
        }

        if (this.right != null) {
            temp = this.right.midSearch(no);
        }

        return temp;

    }

    public Node lastSearch(int no) {

        Node temp = null;

        if (this.left != null) {
            temp = this.left.lastSearch(no);
        }
        if (temp != null) {
            return temp;
        }

        if (this.right != null) {
            temp = this.right.lastSearch(no);
        }

        if (temp != null) {
            return temp;
        }

        if (this.no == no) {
            return this;
        }

        return temp;

    }

    /**
     * 根据编号删除节点
     */
    public void delNode(int no) {

        if (this.left != null && this.left.no == no) {
            this.left = null;
        }
        if (this.right != null && this.right.no == no) {
            this.right = null;
        }
        if (this.left != null) {
            this.left.delNode(no);
        }
        if (this.right != null) {
            this.right.delNode(no);
        }

    }

}

2.定义二叉树

package tree;

public class Tree {
    // 头结点
    private Node root;

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

    /**
     * 前中后序遍历
     */
    public void preListTree() {
        if (root != null) {
            root.preList();
        }else {
            System.out.println("二叉树为空");
        }
    }

    public void midListTree() {
        if (root != null) {
            root.midList();
        }else {
            System.out.println("二叉树为空");
        }
    }

    public void lastListTree() {
        if (root != null) {
            root.lastList();
        }else {
            System.out.println("二叉树为空");
        }
    }

    /**
     * 前中后序查找
     */
    public Node preSearchTree(int no) {
        if (root != null) {
            return root.preSearch(no);
        }else {
            System.out.println("空二叉树");
            return null;
        }
    }

    public Node midSearchTree(int no) {
        if (root != null) {
            return root.midSearch(no);
        }else {
            System.out.println("空二叉树");
            return null;
        }
    }

    public Node lastSearchTree(int no) {
        if (root != null) {
            return root.lastSearch(no);
        }else {
            System.out.println("空二叉树");
            return null;
        }
    }

    /**
     * 删除节点
     */
    public void delNodeTree(int no) {
        if (root != null) {
            if (root.getNo() == no) {
                root = null;
            }else {
                root.delNode(no);
            }
        }else {
            System.out.println("空二叉树无法删除");
        }
    }

}

3.简单测试

package tree;

public class Test {
    public static void main(String[] args) {
        Tree t = new Tree();

        Node root = new Node(0, "zzz");
        Node node1 = new Node(1, "aaa");
        Node node2 = new Node(2, "bbb");
        Node node3 = new Node(3, "ccc");
        Node node4 = new Node(4, "ddd");
        Node node5 = new Node(5, "eee");
        Node node6 = new Node(6, "fff");


        root.setLeft(node1);
        root.setRight(node2);
        node1.setLeft(node3);
        node1.setRight(node4);
        node2.setLeft(node5);
        node2.setRight(node6);

        t.setRoot(root);

        System.out.println(t.preSearchTree(6));

        t.delNodeTree(6);
        System.out.println();

        t.lastListTree();
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值