Java数据结构《二叉树》的手动生成、前序中序后序遍历和查找以及删除节点

  • Post author:
  • Post category:java


package tree;

public class BinaryTreeDemo {
    public static void main(String[] args) {
        HeroNode root = new HeroNode(1,"宋江");
        HeroNode node1 = new HeroNode(2,"吴用");
        HeroNode node2 = new HeroNode(3,"卢俊义");
        HeroNode node3 = new HeroNode(4,"武松");
        HeroNode node4 = new HeroNode(5,"鲁智深");
        //手动生成树
        root.left = node1;
        root.right = node2;
        node2.left = node3;
        node2.right = node4;
        BinaryTree binaryTree = new BinaryTree();
        binaryTree.setRoot(root);
        System.out.println("前序遍历~~~~");
        binaryTree.preList();
//        System.out.println("中序遍历~~~~");
//        binaryTree.infixList();
//        System.out.println("后序遍历~~~~");
//        binaryTree.postList();
//        HeroNode res1 = binaryTree.preSearch(4);
//        if (res1 == null) {
//            System.out.println("前序没找到");
//        }else {
//            System.out.println("前序找到的结果为:" + res1);
//        }
//        HeroNode res2 = binaryTree.infixSearch(3);
//        if (res2 == null) {
//            System.out.println("中序序没找到");
//        }else {
//            System.out.println("中序找到的结果为:" + res2);
//        }
//        HeroNode res3 = binaryTree.postSearch(10);
//        if (res3 == null) {
//            System.out.println("后序没找到");
//        }else {
//            System.out.println("后序找到的结果为:" + res3);
//        }
        binaryTree.delete(10);
        binaryTree.preList();




    }
}
class BinaryTree{
    private HeroNode root;

    public void setRoot(HeroNode root) {
        this.root = root;
    }
    //前序遍历
    public void preList(){
        if (root == null) {
            System.out.println("该树为空");
            return;
        }
        root.preOrderList();
    }
    //中序遍历
    public void infixList(){
        if (root == null) {
            System.out.println("该树为空");
            return;
        }
        root.infixOrderList();
    }
    //后序遍历
    public void postList(){
        if (root == null) {
            System.out.println("该树为空");
            return;
        }
        root.postOrderList();
    }
    //前序查找
    public HeroNode preSearch(int no){
        if (root == null) {
            return null;
        }
        return root.preSearch(no);
    }
    //中序查找
    public HeroNode infixSearch(int no){
        if (root == null) {
            return null;
        }
        return root.infixSearch(no);
    }
    //后序查找
    public HeroNode postSearch(int no){
        if (root == null) {
            return null;
        }
        return root.postSearch(no);
    }
    //删除
    public void delete(int no){
        if (root == null) {
            System.out.println("该树为空,删除失败");
            return;
        }
        if (root.getNo() == no){
            root = null;
            System.out.println("删除成功");
        }else {
            root.delete(no);
        }
    }
}
class HeroNode{
    private int no;
    private String name;
    HeroNode left;
    HeroNode right;

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

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

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
    //前序遍历
    public void preOrderList(){
        System.out.println(this);
        if (this.left != null) {
            this.left.preOrderList();
        }
        if (this.right != null) {
            this.right.preOrderList();
        }
    }
    //中序遍历
    public void infixOrderList(){
        if (this.left != null) {
            this.left.infixOrderList();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrderList();
        }
    }
    //后序遍历
    public void postOrderList(){
        if (this.left != null) {
            this.left.postOrderList();
        }
        if (this.right != null) {
            this.right.postOrderList();
        }
        System.out.println(this);
    }
    //前序查找
    public HeroNode preSearch(int no){
        HeroNode resNode = null;
        if (this.no == no){
            return this;
        }
        if (this.left != null) {
            resNode = this.left.preSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.right != null) {
            resNode = this.right.preSearch(no);
        }
        return resNode;
    }
    //中序查找
    public HeroNode infixSearch(int no){
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.infixSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.no == no){
            return this;
        }
        if (this.right != null) {
            resNode = this.right.infixSearch(no);
        }
        return resNode;
    }
    //后序查找
    public HeroNode postSearch(int no){
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.postSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.right != null) {
            resNode = this.right.postSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.no == no){
            return this;
        }
        return null;
    }
    //删除
    public void delete(int no){
        if (this.left != null && this.left.no == no) {
            this.left = null;
            System.out.println("删除成功");
            return;
        }
        if (this.right != null && this.right.no == no){
            this.right = null;
            System.out.println("删除成功");
            return;
        }
        if (this.left != null) {
            this.left.delete(no);
        }
        if (this.right != null){
            this.right.delete(no);
        }
    }
}



版权声明:本文为qq_43163746原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。