数据结构与算法--树

数据结构与算法--树

  • 一、树结构的基础部分
    • 1.1 树的基本介绍
    • 1.2 二叉树
      • 1.2.1二叉树遍历
      • 1.2.2 二叉树-查找指定节点
      • 1.2.3 二叉树-删除节点
    • 1.3 二叉树代码汇总
    • 1.4 顺序存储二叉树
      • 1.4.1 顺序存储二叉树的概念
      • 1.4.2 顺序存储二叉树的遍历
    • 1.5 顺序存储二叉树代码汇总
    • 1.6 线索化二叉树
  • 二、树结构的实际应用
  • 三、多路查找树

一、树结构的基础部分

1.1 树的基本介绍

为什么需要树这种数据结构

  • 数组存储方式的分析
    • 优点:通过下标方式访问元素,速度块。对于有序数组,还可以使用二分查找提高检索速度
    • 缺点:如果要检索具体某个值,或者插入值(按一定顺序)会整体移动,效率较低
  • 链式存储方式分析
    • 优点:在一定程度上对数组存储方式有优化(比如:插入一个数值节点,只需要将插入节点,连接到链表中即可,删除效率也很好)。
    • 缺点:在进行检索时,效率仍然较低,每次都需要从头节点开始遍历
  • 树存储方式的分析
    • 能提高数据存储,读取的效率,比如利用二叉排序树(BinarySortTree),既可以保证数据的检索速度,也可以保证数据的插入、删除、修改的速度。

树的常用术语
数据结构与算法--树_第1张图片

  • 树的常用术语(结合示意图理解):
    • 1)节点
    • 2)根节点
    • 3)父节点
    • 4)子节点
    • 5)叶子节点 (没有子节点的节点)
    • 6)节点的权(节点值)
    • 7)路径(从root节点找到该节点的路线)
    • 8)层
    • 9)子树
    • 10)树的高度(最大层数)
    • 11)森林 :多颗子树构成森林

1.2 二叉树

概念

  1. 树有很多种,每个节点最多只能有两个子节点的一种形式称为二叉树。
  2. 二叉树的子节点严格的分为左节点和右节点。
  3. 示意图

数据结构与算法--树_第2张图片

  1. 如果该二叉树的所有叶子节点都在最后一层,并且节点总数=2^n-1,n为层数,则我们称满二叉树
  2. 如果该二叉树的所有叶子节点都在最后一层或者倒数第二层,而且最后一次的叶子节点在左边连续,倒数第二层的叶子节点在右边连续,我们称为完全二叉树
  3. 示意图

数据结构与算法--树_第3张图片

1.2.1二叉树遍历

  • 三种遍历方式
    • 前序遍历:先输出父节点,再遍历左子树和右子树
    • 中序遍历:先遍历左子树,再输出父节点,再遍历右子树
    • 后续遍历:先遍历左子树,再遍历右子树,最后输出父节点

代码实现

  • 节点类
//创建HeroNode节点
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;
    }

    @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.infixOrder();
        }
        if(this.right != null){
            this.right.infixOrder();
        }
        System.out.println(this);
    }
    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;
    }
}
  • 二叉树
//定义BinaryTree二叉树
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 class BinaryTreeDemo {
    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();
        HeroNode root = new HeroNode(1, "宋江");
        HeroNode node2 = new HeroNode(2, "吴用");
        HeroNode node3 = new HeroNode(3, "卢俊义");
        HeroNode node4 = new HeroNode(4, "林冲");
        HeroNode node5 = new HeroNode(5, "关胜");
        //先手动创建二叉树
        root.setLeft(node2);
        root.setRight(node3);
        node3.setRight(node4);
        node3.setLeft(node5);
        binaryTree.setRoot(root);
        //测试
        System.out.println("前序遍历");//1,2,3,5,4
        binaryTree.preOrder();
        System.out.println("中序遍历");//2,1,5,3,4
        binaryTree.infixOrder();
        System.out.println("后续遍历");//2,5,4,3,1
        binaryTree.postOrder();
    }
}

1.2.2 二叉树-查找指定节点

  • 三种查找方式
    • 前序查找:先判断当前节点,再向左递归前序查找,最后向右递归查找
    • 中序查找:先向左递归查找,再判断当前节点,最后向右递归查找
    • 后续查找:先向左递归查找,再向右递归查找,最后判断当前节点

代码实现

  • 英雄类中
    //前序遍历查找
    public HeroNode preOrderSearch(int no){
        if(this.no == no){
            return this;
        }
        HeroNode resNode = null;
        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 HeroNode infixOrderSearch(int no){
        HeroNode 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 HeroNode postOrderSearch(int no){
        HeroNode 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(this.no == no){
            return this;
        }
        return resNode;
    }
  • 二叉树类中
    //遍历查找
    public HeroNode preOrderSearch(int no){
        if(root != null){
            return root.preOrderSearch(no);
        }else {
            return null;
        }
    }
    public HeroNode infixOrderSearch(int no){
        if(root != null){
            return root.infixOrderSearch(no);
        }else {
            return null;
        }
    }
    public HeroNode postOrderSearch(int no){
        if(root != null){
            return root.postOrderSearch(no);
        }else {
            return null;
        }
    }
  • 测试代码
        System.out.println("前序查找");
        HeroNode resNode = binaryTree.preOrderSearch(5);
        System.out.println(resNode);
        System.out.println("中序查找");
        resNode = binaryTree.infixOrderSearch(5);
        System.out.println(resNode);
        System.out.println("后续查找");
        resNode = binaryTree.postOrderSearch(5);
        System.out.println(resNode);

1.2.3 二叉树-删除节点

要求

  1. 如果删除的节点是叶子节点,则删除该节点
  2. 如果删除的节点是非叶子节点,则删除该子树
  3. 测试,删掉5号叶子节点和3号子树

思路

  1. 首先处理如果是空树root,只有一个root节点,则等价将二叉树置空
  2. 因为我们的二叉树是单向的,所以我们是判断当前节点的子节点是否需要删除节点,而不能去判断当前这个节点是不是需要删除节点。
  3. 如果当前节点的左子节点不为空,并且左子节点就是要删除的节点,就置空,并且返回。
  4. 如果当前节点的右子节点不为空,并且右子节点就是要删除节点,就置空返回
  5. 如果第3和第4步都没有删除节点,那么就需要分别向两边递归删除。

代码

  • 在HeroNode类中增加方法
    //递归删除节点
    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.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("空树,不能删除");
        }
    }
  • 测试
        //测试删除代码
        System.out.println("删除前");
        binaryTree.preOrder();// 1,2,3,5,4
        binaryTree.delNode(5);
        System.out.println("删除后");
        binaryTree.preOrder();// 1,2,3,4
        binaryTree.delNode(3);
        binaryTree.preOrder();// 1,2

1.3 二叉树代码汇总

package tree.binaryTree;


public class BinaryTreeDemo {
    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();
        HeroNode root = new HeroNode(1, "宋江");
        HeroNode node2 = new HeroNode(2, "吴用");
        HeroNode node3 = new HeroNode(3, "卢俊义");
        HeroNode node4 = new HeroNode(4, "林冲");
        HeroNode node5 = new HeroNode(5, "关胜");
        //先手动创建二叉树
        root.setLeft(node2);
        root.setRight(node3);
        node3.setRight(node4);
        node3.setLeft(node5);
        binaryTree.setRoot(root);

        //测试 遍历代码
//        System.out.println("前序遍历");//1,2,3,5,4
//        binaryTree.preOrder();
//        System.out.println("中序遍历");//2,1,5,3,4
//        binaryTree.infixOrder();
//        System.out.println("后续遍历");//2,5,4,3,1
//        binaryTree.postOrder();

        //测试查找代码
        System.out.println("前序查找");
        HeroNode resNode = binaryTree.preOrderSearch(5);
        System.out.println(resNode);
        System.out.println("中序查找");
        resNode = binaryTree.infixOrderSearch(5);
        System.out.println(resNode);
        System.out.println("后续查找");
        resNode = binaryTree.postOrderSearch(5);
        System.out.println(resNode);

        //测试删除代码
        System.out.println("删除前");
        binaryTree.preOrder();// 1,2,3,5,4
        binaryTree.delNode(5);
        System.out.println("删除后");
        binaryTree.preOrder();// 1,2,3,4
        binaryTree.delNode(3);
        binaryTree.preOrder();// 1,2
    }
}
//定义BinaryTree二叉树
class BinaryTree{
    private HeroNode root;

    public void setRoot(HeroNode root) {
        this.root = root;
    }
    //删除节点
    public void delNode(int no){
        if(root != null){
            //如果只有一个root节点
            if(root.getNo() == no){
                root = null;
            }else {
                root.delNode(no);
            }
        }else {
            System.out.println("空树,不能删除");
        }
    }
    //前序遍历
    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 preOrderSearch(int no){
        if(root != null){
            return root.preOrderSearch(no);
        }else {
            return null;
        }
    }
    public HeroNode infixOrderSearch(int no){
        if(root != null){
            return root.infixOrderSearch(no);
        }else {
            return null;
        }
    }
    public HeroNode postOrderSearch(int no){
        if(root != null){
            return root.postOrderSearch(no);
        }else {
            return null;
        }
    }
}
//创建HeroNode节点
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 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.left != null){
            this.left.delNode(no);
        }
        if(this.right != null){
            this.right.delNode(no);
        }
    }
    //前序遍历查找
    public HeroNode preOrderSearch(int no){
        if(this.no == no){
            return this;
        }
        HeroNode resNode = null;
        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 HeroNode infixOrderSearch(int no){
        HeroNode 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 HeroNode postOrderSearch(int no){
        HeroNode 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(this.no == no){
            return this;
        }
        return resNode;
    }
    @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.infixOrder();
        }
        if(this.right != null){
            this.right.infixOrder();
        }
        System.out.println(this);
    }
    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;
    }
}

1.4 顺序存储二叉树

1.4.1 顺序存储二叉树的概念

基本说明

  • 从数据存储来看,数组存储方式和树的存储方式可以相互转换,即数组可以转换成树,树也可以转换成数组
  • 示意图
    数据结构与算法--树_第4张图片

要求

  1. 上图的二叉树的节点,要以数组的方式来存放arr:[1,2,3,4,5,6,7]
  2. 要求在遍历数组arr时,仍然可以以前序遍历,中序遍历和后序遍历的方式完成节点的遍历

特点

  1. 顺序二叉树通常只考虑完全二叉树
  2. 第n个元素的左子节点为 2*n+1
  3. 第n个元素的右子节点为 2*n+2
  4. 第n个元素的父节点为 (n-1)/2
  5. n:从0开始表示二叉树中的第几个元素

1.4.2 顺序存储二叉树的遍历

  • 需求:给你一个数组{1,2,3,4,5,6,7},要求以二叉树前序遍历的方式进行遍历。前序遍历的结果应当为1,2,4,5,3,6,7

代码实现

  • 前序遍历
//编写一个 ArrayBinaryTree 实现顺序存储二叉树遍历
class ArrayBinaryTree{
    private int[] arr; //存储数据节点的数组
    public ArrayBinaryTree(int[] arr){
        this.arr = arr;
    }
    //编写一个方法,完成顺序存储二叉树的前序遍历
    public void preOrder(int index){
        if(arr == null || arr.length == 0){
            System.out.println("数组为空,不能按照二叉树的前序遍历");
        }
        //输出当前这个元素
        System.out.print(arr[index]+" ");
        //向左递归遍历
        if((index*2+1) < arr.length){
            preOrder(2*index + 1);
        }
        //向右递归遍历
        if((index*2 + 2) < arr.length){
            preOrder(2*index+2);
        }
    }
    //重载一下前序遍历
    public void preOrder(){
        this.preOrder(0);
    }
}
  • 中序遍历
    //中序遍历
    public void infixOrder(int index){
        if(arr == null || arr.length == 0){
            System.out.println("数组为空,不能按照二叉树的前序遍历");
        }
        //向左递归遍历
        if((index*2+1) < arr.length){
            infixOrder(index*2+1);
        }
        //输出当前元素
        System.out.print(arr[index]+ " ");
        //向右递归遍历
        if((index*2+2) < arr.length){
            infixOrder(index*2+2);
        }
    }
    public void infixOrder(){
        this.infixOrder(0);
    }
  • 后序遍历
    //后序遍历
    public void postOrder(int index){
        if(arr == null || arr.length == 0){
            System.out.println("数组为空,不能按照二叉树的前序遍历");
        }
        //向左递归遍历
        if((index*2+1) < arr.length){
            postOrder(index*2+1);
        }
        //向右递归遍历
        if((index*2+2) < arr.length){
            postOrder(index*2+2);
        }
        //输出当前元素
        System.out.print(arr[index]+ " ");
    }
    public void postOrder(){
        this.postOrder(0);
    }
  • 测试代码
public class ArrayBinaryTreeDemo {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7};
        ArrayBinaryTree arrayBinaryTree = new ArrayBinaryTree(arr);
        //测试前序遍历,理论结果是 1,2,4,5,3,6,7
        arrayBinaryTree.preOrder();
        System.out.println();
        //中序遍历,理论结果 4,2,5,1,6,3,7
        arrayBinaryTree.infixOrder();
        System.out.println();
        //后序遍历,理论结果 4,5,2,6,7,3,1
        arrayBinaryTree.postOrder();
    }
}

1.5 顺序存储二叉树代码汇总

package tree.arrayBinaryTree;

public class ArrayBinaryTreeDemo {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7};
        ArrayBinaryTree arrayBinaryTree = new ArrayBinaryTree(arr);
        //测试前序遍历,理论结果是 1,2,4,5,3,6,7
        arrayBinaryTree.preOrder();
        System.out.println();
        //中序遍历,理论结果 4,2,5,1,6,3,7
        arrayBinaryTree.infixOrder();
        System.out.println();
        //后序遍历,理论结果 4,5,2,6,7,3,1
        arrayBinaryTree.postOrder();
    }
}
//编写一个 ArrayBinaryTree 实现顺序存储二叉树遍历
class ArrayBinaryTree{
    private int[] arr; //存储数据节点的数组
    public ArrayBinaryTree(int[] arr){
        this.arr = arr;
    }
    //编写一个方法,完成顺序存储二叉树的前序遍历
    public void preOrder(int index){
        if(arr == null || arr.length == 0){
            System.out.println("数组为空,不能按照二叉树的前序遍历");
        }
        //输出当前这个元素
        System.out.print(arr[index]+" ");
        //向左递归遍历
        if((index*2+1) < arr.length){
            preOrder(2*index + 1);
        }
        //向右递归遍历
        if((index*2 + 2) < arr.length){
            preOrder(2*index+2);
        }
    }
    //重载一下前序遍历
    public void preOrder(){
        this.preOrder(0);
    }
    //中序遍历
    public void infixOrder(int index){
        if(arr == null || arr.length == 0){
            System.out.println("数组为空,不能按照二叉树的前序遍历");
        }
        //向左递归遍历
        if((index*2+1) < arr.length){
            infixOrder(index*2+1);
        }
        //输出当前元素
        System.out.print(arr[index]+ " ");
        //向右递归遍历
        if((index*2+2) < arr.length){
            infixOrder(index*2+2);
        }
    }
    public void infixOrder(){
        this.infixOrder(0);
    }
    //后序遍历
    public void postOrder(int index){
        if(arr == null || arr.length == 0){
            System.out.println("数组为空,不能按照二叉树的前序遍历");
        }
        //向左递归遍历
        if((index*2+1) < arr.length){
            postOrder(index*2+1);
        }
        //向右递归遍历
        if((index*2+2) < arr.length){
            postOrder(index*2+2);
        }
        //输出当前元素
        System.out.print(arr[index]+ " ");
    }
    public void postOrder(){
        this.postOrder(0);
    }
}

1.6 线索化二叉树

二、树结构的实际应用

三、多路查找树

你可能感兴趣的:(数据结构与算法,数据结构,java)