数据结构与算法-二叉树(前序中序后续)(增删改查)


二叉树

一、二叉树是什么?

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、源码

1.Heronode类

代码如下(示例):

package BinaryTree;

public class Heronode {
    
    
    private int no;
    private String name;
    private Heronode left;
    private 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;
    }

    public Heronode getLeft() {
    
    
        return left;
    }

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

    public Heronode getRight() {
    
    
        return right;
    }

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

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

    //前序遍历 利用递归的方法
    public void preOrder() {
    
    
        System.out.println(this);  //先输出父节点
        //向左递归
        if (this.left != null) {
    
    
            this.left.preOrder();
        }
        //向右递归
        if (this.right != null) {
    
    
            this.right.preOrder();
        }
    }

    //中序遍历
    public void infixOrder() {
    
    
        if (this.left != null) {
    
    
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
    
    
            this.right.infixOrder();
        }
    }

    //后续遍历
    public void postOrder() {
    
    
        if (this.left != null) {
    
    
            this.left.postOrder();
        }
        if (this.right != null) {
    
    
            this.right.postOrder();
        }
        System.out.println(this);
    }

    //前序查找
    public Heronode proOrderSearch(int no) {
    
    
        Heronode resultnode = null;
        if (this.no == no) {
    
    
            return this;
        }
        if (this.left != null) {
    
    
            resultnode = this.left.proOrderSearch(no);
        }
        if (resultnode != null) {
    
    
            return resultnode;
        }
        if (this.right != null) {
    
    
            resultnode = this.right.proOrderSearch(no);
        }
        return resultnode;
    }

    //中序查找
    public Heronode infixOrderSearch(int no) {
    
    
        Heronode resultnode = null;
        if (this.left != null) {
    
    
            resultnode = this.left.infixOrderSearch(no);
        }
        if (resultnode != null) {
    
    
            return resultnode;
        }
        if (this.no == no) {
    
    
            return this;
        }
        if (this.right != null) {
    
    
            resultnode = this.right.infixOrderSearch(no);
        }
        return resultnode;
    }

    //后序查找
    public Heronode postOrderSearch(int no) {
    
    
        Heronode resultnode = null;
        if (this.left != null) {
    
    
            resultnode = this.left.postOrderSearch(no);
        }
        if (resultnode != null) {
    
    
            return resultnode;
        }
        if (this.right != null) {
    
    
            resultnode = this.right.postOrderSearch(no);
        }
        if (resultnode != null) {
    
    
            return resultnode;
        }
        if (this.no == no) {
    
    
            return this;
        }
        return resultnode;
    }

    //删除某个节点
    public void deletenode(int no) {
    
    
        if (this.left != null && this.left.no == no) {
    
    
            this.left = null;
            return;
        }
        if (this.right != null && this.right.no == no) {
    
    
            this.right = null;
            return;
        }
        if (this.left != null) {
    
    
            this.left.deletenode(no);
        }
        if (this.right != null) {
    
    
            this.right.deletenode(no);
        }
    }

}

2.BinaryTree类

代码如下(示例):

package BinaryTree;

public class BinaryTree {
    
    
    private Heronode root;

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

    //前序遍历
    public void preOrder() {
    
    
        if (this.root != null) {
    
    
            this.root.preOrder();
        } else {
    
    
            System.out.println("二叉树为空,无法遍历");
        }
    }

    //中序遍历
    public void infixOrder() {
    
    
        if (this.root != null) {
    
    
            this.root.infixOrder();
        } else {
    
    
            System.out.println("二叉树为空,无法遍历");
        }
    }

    //后续遍历
    public void postOrder() {
    
    
        if (this.root != null) {
    
    
            this.root.postOrder();
        } else {
    
    
            System.out.println("二叉树为空,无法遍历");
        }
    }

    //前序查找
    public Heronode proOrderSearch(int no) {
    
    
        if (this.root != null) {
    
    
            return this.root.proOrderSearch(no);
        } else {
    
    
            return null;
        }
    }

    //中序查找
    public Heronode infixOrderSearch(int no) {
    
    
        if (this.root != null) {
    
    
            return this.root.infixOrderSearch(no);
        } else {
    
    
            return null;
        }
    }

    //后序查找
    public Heronode postOrderSearch(int no) {
    
    
        if (this.root != null) {
    
    
            return this.root.postOrderSearch(no);
        } else {
    
    
            return null;
        }
    }

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


3.BinaryTreeDemo

代码如下(示例):

package BinaryTree;

public class BinaryTreeDemo {
    
    
    public static void main(String[] args) {
    
    
        Heronode heronode1 = new Heronode(1,"james");
        Heronode heronode2 = new Heronode(2,"davis");
        Heronode heronode3 = new Heronode(3,"polo");
        Heronode heronode4 = new Heronode(4,"towns");
        Heronode heronode5 = new Heronode(5,"rose");
        BinaryTree binaryTree = new BinaryTree();
        binaryTree.setRoot(heronode1);
        heronode1.setLeft(heronode2);
        heronode1.setRight(heronode3);
        heronode3.setLeft(heronode5);
        heronode3.setRight(heronode4);
        System.out.println("前序遍历");
        binaryTree.preOrder();
        System.out.println("=============");
        Heronode resultheronode = binaryTree.proOrderSearch(5);
        if (resultheronode!=null){
    
    
            System.out.printf("找到编号为%d,姓名为%s\n",resultheronode.getNo(),resultheronode.getName());
        }else {
    
    
            System.out.printf("没有找到编号为%d",5);
        }
        System.out.println("=============");
        binaryTree.deletenode(5);
        System.out.println("删除后,前序遍历");
        binaryTree.preOrder();
    }
}


总结

理清楚思路,debug下就懂了

猜你喜欢

转载自blog.csdn.net/slighting1128/article/details/112250645