• 树形结构-二叉树


    树形结构

    树形结构简介

    树结构是一种非线性储存结构,存储的是具有“一对多”关系的数据元素的集合
    在这里插入图片描述

    树的相关术语

    • 结点(Node)
      • 使用树结构存储的每一个数据元素被成为“结点”
    • 结点的度(Degree of Node)
      • 某个结点所拥有的子树的个数
    • 树的深度(Degree of Tree)
      • 树种的结点最大底层数
    • 叶子结点(Leaf Node)
      • 度为0的结点,也叫最终结点
    • 分支结点(Branch Node)
      • 度不为0的结点,也叫非最终结点,内部结点
    • 孩子(Child)
      • 也可称为子树或者子结点,表示当前结点的直接上层结点
    • 双亲(parent)
      • 也可称为父结点,表示结点的直接上层关系
    • 根结点(Root Node)
      • 没有双亲的结点,在一个树形结构种只有一个跟结点
    • 祖先(ancestor)
      • 从当前结点上层的所有结点
    • 子孙(children)
      • 当前结点下次的所有结点
    • 兄弟(Brother)
      • 同一个父结点的孩子

    二叉树简介

    二叉树(Binary Tree) 是树形结构的重要类型,许多实际问题抽象出来的数据结构往往是二叉树形式,即使是一般的树也能被简单的转换为二叉树,而二叉树的存储结构及其算法是比较简单的,因此二叉树显得很重要,

    二叉树的特点:每个结点最多有两颗子树,而且有左右之分

    二叉树分类

    满二叉树

    除了最后一层外,每一层上的所有结点都有两个字结点

    在这里插入图片描述

    完全二叉树

    完全二叉树,除了最后一层可能不满意外,其他各层都达到层结点的最大数,最后一层不满,该层所有结点的是靠左排

    完全二叉树

    在这里插入图片描述

    非完全二叉树

    在这里插入图片描述

    二叉树的遍历

    • 前序遍历
      • 根-左-右
    • 中序遍历
      • 左-根-右
    • 后续遍历
      • 左-右-根
    • 层序遍历
      • 从上至下逐层遍历
    前序遍历

    在这里插入图片描述

    中序遍历

    在这里插入图片描述

    后续遍历

    在这里插入图片描述

    层序遍历

    在这里插入图片描述

    二叉树排序

    二叉树排序分析

    利用二叉树结构以及遍历方式可以基于二叉树的元素处理排序

    在这里插入图片描述

    二叉树排序实现
    创建二叉树排序器类
    /**
     * 基于二叉树结构实现元素排序处理的排序器
     */
    public class BinaryTreeSort<E extends Integer> {
    
        Node<E> root;//根结点
        int size;
    
        /**
         * 将元素添加到排序器中
         */
        public void add (E element){
            //实例化结点对象
            Node<E> node = new Node<>(element);
    
            //判断当前二叉树中是否右根结点,如果没有那么新结点则为根结点
            if (this.root == null){
                this.root = node;
            }else {
                this.root.addNode(node);
            }
            size++;
    
        }
        /**
         * 对元素排序
         *
         */
        public void sort(){
            if (this.root ==null){
                return;
            }
            this.root.inorderTraversal();
    
        }
    
    
    
    
        /**
         * 定义结点类
         */
       public class Node<E extends Integer>{
            private E item;//存放元素
    
            private Node left; //存放的左子树
    
            private Node right;//存放的右子树
    
            public Node() {
            }
    
    
            public Node(E item) {
                this.item = item;
            }
            /**
             * 添加结点
             */
            public void addNode(Node node){
                //完成新结点的元素与当前结点中的元素判断
                //如果新结点中的元素小于当前结点中的元素,那么新结点则放在当前结点的左子树中
                if (node.item.intValue() < this.item.intValue()){
                    if (this.left == null)
                        this.left = node;
                    else
                        this.left.addNode(node);
                }else {
                    //如果新结点中的元素大于当前结点中的元素,那么新结点则放在当前结点的右子树中
                    if (this.right == null)
                        this.right = node;
                    else
                        this.right.addNode(node);
                }
            }
    
            //使用中序遍历二叉树
            public void inorderTraversal(){
                //先找最左侧的哪个结点
                if (this.left != null){
                    this.left.inorderTraversal();
                }
                System.out.println(this.item);
    
                if (this.right != null){
                    this.right.inorderTraversal();
                }
            }
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    测试
    public class TreeDemo {
        public static void main(String[] args) {
            BinaryTreeSort binaryTreeSort = new BinaryTreeSort();
            binaryTreeSort.add(12);
            binaryTreeSort.add(4);
            binaryTreeSort.add(5);
            System.out.println("大小:"+binaryTreeSort.size);
            binaryTreeSort.sort();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    结果

    在这里插入图片描述

  • 相关阅读:
    【修车案例】一波形一案例(10)
    【前端设计】SDC中生成时钟create_generated_clock语法解析
    网络编程中利用信号处理技术消灭僵尸进程
    MacOS安装redis
    WorkPlus私有化部署IM即时通讯平台,构建高效安全的局域网办公环境
    Android修行手册 - TabLayout全解析 - 小红点和自定义样式
    Consumer位移管理-Kafka从入门到精通(十一)
    【AI应用】海康威视iVMS-4200软件安装
    rocketmq消息发送源码学习
    LocalDateTime用法
  • 原文地址:https://blog.csdn.net/qq_44715376/article/details/127676459