数据结构与算法——二叉树

基本介绍

存储方式

数组存储方式的分析

优点:通过下标方式访问元素,速度快。对于有序数组,还可使用二分查找提高检索速度。
缺点:如果要检索具体某个值,或者插入值(按一定顺序)会整体移动,效率较低。
在这里插入图片描述

链表存储方式分析

优点:在一定程度上对数组存储方式有优化(比如:插入一个数值节点,只需要将插入节点,链接到链表中即可,删除效率也很好)。
缺点:在进行检索时,效率仍然较低,比如(检索某个值,需要从头节点开始遍历)。
在这里插入图片描述

树存储方式分析

能提高数据存储,读取的效率, 比如利用二叉排序树(Binary Sort Tree),既可以保证数据的检索速度,同时也可以保证数据的插入,删除,修改的速度。
在这里插入图片描述

常用术语

在这里插入图片描述

二叉树概念

树有很多种,每个节点最多只能有两个子节点的一种形式称为二叉树。二叉树的子节点分为左节点和右节点。
在这里插入图片描述
如果该二叉树的所有叶子节点都在最后一层,并且结点总数= 2n -1 , n 为层数,则我们称为满二叉树。
在这里插入图片描述
如果该二叉树的所有叶子节点都在最后一层或者倒数第二层,而且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续,我们称为完全二叉树。
在这里插入图片描述

遍历结点

在这里插入图片描述
结点类中定义的方法

    //前序遍历
    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 void preOrder() {
        if (this.root != null)
            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 Node preOrderSearch(int no) {
        Node resNode = null;
        //比较当前结点
        if (this.no == no) {
            return this;
        }
        //左子树递归查找
        if (this.left != null) {
            resNode = this.left.preOrderSearch(no);
        }
        if (resNode != null)    //在左子树找到就返回,上层的递归就不用向右子树找了
            return resNode;
        //右子树递归查找
        if (this.right != null) {
            resNode = this.right.preOrderSearch(no);
        }
        return resNode;
    }

    //中序遍历查找
    public Node infixOrderSearch(int no){
        Node resNode = null;
        //左子树递归查找
        if(this.left!=null){
            resNode = this.left.infixOrderSearch(no);
        }
        if (resNode != null)    //在左子树找到就返回,上层的递归就不用向右子树找了
            return resNode;
        //比较当前结点
        if (this.no == no) {
            return this;
        }
        //右子树递归查找
        if (this.right != null) {
            resNode = this.right.infixOrderSearch(no);
        }
        return resNode;
    }

    //后序遍历查找
    public Node postOrderSearch(int no){
        Node resNode = null;
        //左子树递归查找
        if(this.left!=null){
            resNode = this.left.postOrderSearch(no);
        }
        if (resNode != null)    //在左子树找到就返回,上层的递归就不用向右子树找了
            return resNode;
        //右子树递归查找
        if (this.right != null) {
            resNode = this.right.postOrderSearch(no);
        }
        if (resNode != null)    //在右子树找到就返回
            return resNode;
        //比较当前结点
        if(this.no == no){
            return this;
        }
        return null;
    }

二叉树类中引用的方法

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

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

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

删除结点

在这里插入图片描述
结点类中定义的方法

    //叶子结点直接删除,非叶子结点删除子树
    public void delNode(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.delNode(no);
        }
        //向右子树进行递归删除
        if(this.right!=null){
            this.right.delNode(no);
        }
    }

二叉树类中引用的方法

    //删除结点
    public void delNode(int no){
        if(root!=null){
            //判断root是不是要删除的结点
            if(root.getNo() ==no){
                root = null;
            }else{
                //递归删除
                root.delNode(no);
            }
        }else {
            System.out.println("无法删除,该树为空");
        }
    }

完整代码

package tree;

public class BinaryTreeDemo {

    public static void main(String[] args) {

        BinaryTree binaryTree = new BinaryTree();

        Node node1 = new Node(1, "a");
        Node node2 = new Node(2, "a");
        Node node3 = new Node(3, "a");
        Node node4 = new Node(4, "a");

        //先手动创建二叉树
        binaryTree.setRoot(node1);
        node1.setLeft(node2);
        node1.setRight(node3);
        node3.setRight(node4);
//
//        binaryTree.preOrder();  //1234
//        binaryTree.infixOrder();//2134
//        binaryTree.postOrder(); //2431

        System.out.println(binaryTree.preOrderSearch(3));
        System.out.println(binaryTree.infixOrderSearch(9));
        System.out.println(binaryTree.postOrderSearch(3));

    }

}

//定义BinaryTree二叉树
class BinaryTree {
    private Node root;

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

    //前序遍历
    public void preOrder() {
        if (this.root != null)
            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 Node preOrderSearch(int no){
        if(root!=null){
            return root.preOrderSearch(no);
        }else {
            return null;
        }
    }

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

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

    //删除结点
    public void delNode(int no){
        if(root!=null){
            //判断root是不是要删除的结点
            if(root.getNo() ==no){
                root = null;
            }else{
                //递归删除
                root.delNode(no);
            }
        }else {
            System.out.println("无法删除,该树为空");
        }
    }

}

//创建Node结点
class Node {

    private int no;
    private String name;
    private Node left;  //默认null
    private Node right; //默认null

    public Node(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 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 "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 Node preOrderSearch(int no) {
        Node resNode = null;
        //比较当前结点
        if (this.no == no) {
            return this;
        }
        //左子树递归查找
        if (this.left != null) {
            resNode = this.left.preOrderSearch(no);
        }
        if (resNode != null)    //在左子树找到就返回,上层的递归就不用向右子树找了
            return resNode;
        //右子树递归查找
        if (this.right != null) {
            resNode = this.right.preOrderSearch(no);
        }
        return resNode;
    }

    //中序遍历查找
    public Node infixOrderSearch(int no){
        Node resNode = null;
        //左子树递归查找
        if(this.left!=null){
            resNode = this.left.infixOrderSearch(no);
        }
        if (resNode != null)    //在左子树找到就返回,上层的递归就不用向右子树找了
            return resNode;
        //比较当前结点
        if (this.no == no) {
            return this;
        }
        //右子树递归查找
        if (this.right != null) {
            resNode = this.right.infixOrderSearch(no);
        }
        return resNode;
    }

    //后序遍历查找
    public Node postOrderSearch(int no){
        Node resNode = null;
        //左子树递归查找
        if(this.left!=null){
            resNode = this.left.postOrderSearch(no);
        }
        if (resNode != null)    //在左子树找到就返回,上层的递归就不用向右子树找了
            return resNode;
        //右子树递归查找
        if (this.right != null) {
            resNode = this.right.postOrderSearch(no);
        }
        if (resNode != null)    //在右子树找到就返回
            return resNode;
        //比较当前结点
        if(this.no == no){
            return this;
        }
        return null;
    }

    //递归删除结点
    //叶子结点直接删除,非叶子结点删除子树
    public void delNode(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.delNode(no);
        }
        //向右子树进行递归删除
        if(this.right!=null){
            this.right.delNode(no);
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值