【数据结构】二叉树及前中后序遍历

简介: 【数据结构】二叉树及前中后序遍历

一、二叉树

1、为什么需要树

1)数组存储方式的分析

优点:通过下标方式访问元素,速度快。对于有序数组,还可使用二分查找提高检索速度。

缺点:如果要检索具体某个值,或者插入值(按一定顺序)会整体移动,效率较低

2)链式存储方式的分析

优点:在一定程度上对数组存储方式有优化(比如:插入一个数值节点,只需要将插入节点,链按到链表中即可,删除效率也很好)。

缺点:在进行检索时,效率仍然较低,比如(检索某个值,需要从头节点开始遍历)

3)树存储方式的分析

可以提高数据存储,读取的效率

比如利用二叉排序树(Binary Sort Tree),既可以保证数据的检索速度,同时也可以保证数据的插入,删除,修改的速度。

2、树的常用术语

  1. 节点
  2. 根节点
  3. 父节点
  4. 子节点
  5. 叶子节点:没有子节点的节点
  6. 节点的权:节点值
  7. 路径:从root 节点找到该节点的路线
  8. 子树
  9. 树的高度(最大层数)
  10. 森林 :多颗子树构成森林

3、二叉树概述

  1. 二叉树:每个节点最多只能有两个子节点(左节点和右节点)
  2. 满二叉树:二叉树的所有叶子节点都在最后一层,并且结点总数=2^n-1(n 为层数)
  3. 完全二叉树:二叉树的所有叶子节点都在最后一层或者倒数第二层,而且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续

4、二叉树遍历概述

  1. 前序遍历:先输出父节点,再遍历左子树和右子树
  2. 中序遍历:先遍历左子树,再输出父节点,再遍历右子树
  3. 后序遍历:先遍历左子树,再遍历右子树,最后输出父节点

小结:看输出父节点的顺序,就确定是前序,中序还是后序

5、二叉树代码实现

package work.rexhao.tree;

/**
 * 二叉树的遍历和查找
 *
 * @author 王铭颢
 * @Date 2022/7/5 10:23
 */
public class BinaryTreeDemo {
   
    public static void main(String[] args) {
   
        BinaryTree bt = new BinaryTree();
        System.out.print("先序遍历: ");
        BinaryTree.preOrder(bt.head);//先序遍历:12453
        System.out.println();
        System.out.print("中序遍历: ");
        BinaryTree.infixOrder(bt.head);//中序遍历:42513
        System.out.println();
        System.out.print("后序遍历: ");
        BinaryTree.postOrder(bt.head);//后续遍历:45231
        System.out.println();
        System.out.println("------------");
        System.out.println("先序查找3: " + BinaryTree.preOrderSearch(bt.head, 3));
        System.out.println("先序查找6: " + BinaryTree.preOrderSearch(bt.head, 6));
        System.out.println("中序查找3: " + BinaryTree.infixOrderSearch(bt.head, 3));
        System.out.println("中序查找6: " + BinaryTree.infixOrderSearch(bt.head, 6));
        System.out.println("后序查找3: " + BinaryTree.postOrderSearch(bt.head, 3));
        System.out.println("后序查找6: " + BinaryTree.postOrderSearch(bt.head, 6));
        System.out.println("------------");
        BinaryTree.delTree(bt.head, 2);
        BinaryTree.infixOrder(bt.head);//中序遍历:13
        System.out.println();
        BinaryTree.delTree(bt.head, 6);
        BinaryTree.infixOrder(bt.head);//中序遍历:13
        System.out.println();
        System.out.println("------------");
        bt = new BinaryTree();
        BinaryTree.delNode(bt.head, 2);
        BinaryTree.infixOrder(bt.head);//中序遍历:453
        System.out.println();
    }
}

/**
 * 二叉树
 */
class BinaryTree {
   
    TreeNode head;

    public BinaryTree() {
   
        head = new TreeNode(1);
        head.leftNode = new TreeNode(2);
        head.rightNode = new TreeNode(3);
        head.leftNode.leftNode = new TreeNode(4);
        head.leftNode.rightNode = new TreeNode(5);
        /*
        初始化树结构示意图
                 1
               /  \
              2    3
            /  \
           4    5

        先序遍历:12453
        中序遍历:42513
        后续遍历:45231
         */
    }

    /**
     * 先序遍历
     */
    public static void preOrder(TreeNode tn) {
   
        if (tn != null) {
   
            System.out.print(tn.getData());
            preOrder(tn.leftNode);
            preOrder(tn.rightNode);
        }
    }

    /**
     * 中序遍历
     */
    public static void infixOrder(TreeNode tn) {
   
        if (tn != null) {
   
            infixOrder(tn.leftNode);
            System.out.print(tn.getData());
            infixOrder(tn.rightNode);
        }
    }

    /**
     * 后序遍历
     */
    public static void postOrder(TreeNode tn) {
   
        if (tn != null) {
   
            postOrder(tn.leftNode);
            postOrder(tn.rightNode);
            System.out.print(tn.getData());
        }
    }

    /**
     * 先序查找
     */
    public static boolean preOrderSearch(TreeNode tn, int target) {
   
        if (tn == null) return false;
        if (target == tn.getData()) {
   
            return true;
        } else if (preOrderSearch(tn.leftNode, target)) {
   
            return preOrderSearch(tn.leftNode, target);
        } else {
   
            return preOrderSearch(tn.rightNode, target);
        }
    }

    /**
     * 中序查找
     */
    public static boolean infixOrderSearch(TreeNode tn, int target) {
   
        if (tn == null) return false;
        if (infixOrderSearch(tn.leftNode, target)) {
   
            return infixOrderSearch(tn.leftNode, target);
        } else if (target == tn.getData()) {
   
            return true;
        } else {
   
            return infixOrderSearch(tn.rightNode, target);
        }
    }

    /**
     * 后序查找
     */
    public static boolean postOrderSearch(TreeNode tn, int target) {
   
        if (tn == null) return false;
        if (postOrderSearch(tn.leftNode, target)) {
   
            return postOrderSearch(tn.leftNode, target);
        } else if (postOrderSearch(tn.rightNode, target)) {
   
            return postOrderSearch(tn.rightNode, target);
        } else {
   
            return target == tn.getData();
        }
    }

    /**
     * 删除子树
     * 叶子结点:删除节点
     * 非叶子节点:删除子树
     * 注:不删除根节点
     */
    public static void delTree(TreeNode tn, int target) {
   
        if (tn == null) {
   
            return;
        }
        if (tn.leftNode != null) {
   
            if (tn.leftNode.getData() == target) {
   
                tn.leftNode = null;
                return;
            } else {
   
                delTree(tn.leftNode, target);
            }
        }
        if (tn.rightNode != null) {
   
            if (tn.rightNode.getData() == target) {
   
                tn.rightNode = null;
                return;
            } else {
   
                delTree(tn.rightNode, target);
            }
        }
    }

    /**
     * 删除节点
     * 叶子节点:删除
     * 非叶子节点(一个子节点):替代原节点
     * 非叶子节点(两个子节点):左节点代替原节点
     */
    public static void delNode(TreeNode tn, int target) {
   
        if (tn == null) return;
        if (tn.leftNode != null) {
   
            if (tn.leftNode.getData() == target) {
   
                if (tn.leftNode.rightNode == null && tn.leftNode.leftNode == null) {
   
                    // 叶子节点
                    tn.leftNode = null;
                } else if (tn.leftNode.rightNode != null && tn.leftNode.leftNode != null) {
   
                    // 两个节点
                    tn.leftNode = tn.leftNode.leftNode;
                } else {
   
                    // 一个节点
                    if (tn.leftNode.leftNode != null) {
   
                        // 左节点
                        tn.leftNode = tn.leftNode.leftNode;
                    } else {
   
                        // 右节点
                        tn.leftNode = tn.leftNode.rightNode;
                    }
                }
                return;
            } else {
   
                delNode(tn.leftNode, target);
            }
        }
        if (tn.rightNode != null) {
   
            if (tn.rightNode.getData() == target) {
   
                if (tn.rightNode.rightNode == null && tn.rightNode.leftNode == null) {
   
                    // 叶子节点
                    tn.rightNode = null;
                } else if (tn.rightNode.rightNode != null && tn.rightNode.leftNode != null) {
   
                    // 两个节点
                    tn.rightNode = tn.rightNode.leftNode;
                } else {
   
                    // 一个节点
                    if (tn.rightNode.leftNode != null) {
   
                        // 左节点
                        tn.rightNode = tn.rightNode.leftNode;
                    } else {
   
                        // 右节点
                        tn.rightNode = tn.rightNode.rightNode;
                    }
                }
                return;
            } else {
   
                delNode(tn.rightNode, target);
            }
        }
    }

}

/**
 * 二叉树的节点
 */
class TreeNode {
   
    private int data;
    TreeNode leftNode;
    TreeNode rightNode;

    TreeNode(int data) {
   
        this.data = data;
    }

    public int getData() {
   
        return data;
    }

    public void setData(int data) {
   
        this.data = data;
    }


}

二、顺序存储二叉树

1、顺序存储二叉树的说明

从数据存储来看,数组存储方式和树的存储方式可以相互转换,即数组可以转换成树,树也可以转换成数组

2、顺序存储二叉树的特点

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

3、顺序存储二叉树代码实现

package work.rexhao.tree;

/**
 * 顺序存储二叉树
 *
 * @author 王铭颢
 * @Date 2022/7/5 14:56
 */
public class ArrBinaryTreeDemo {
   
    public static void main(String[] args) {
   
        ArrBinaryTree abt = new ArrBinaryTree(7);
        for (int i = 0; i < abt.data.length; i++) {
   
            abt.data[i] = i + 1;
        }
        System.out.print("前序:");
        ArrBinaryTree.preOrder(abt, 0);
        System.out.println();
        System.out.print("中序:");
        ArrBinaryTree.infixOrder(abt, 0);
        System.out.println();
        System.out.print("后序:");
        ArrBinaryTree.postOrder(abt, 0);
        System.out.println();

    }
}

class ArrBinaryTree {
   
    int[] data;

    ArrBinaryTree(int maxSize) {
   
        data = new int[maxSize];
    }

    /**
     * 先序遍历
     */
    public static void preOrder(ArrBinaryTree abt, int index) {
   
        // 数组越界
        if (index >= abt.data.length) {
   
            return;
        }
        System.out.print(abt.data[index]);
        preOrder(abt, index * 2 + 1);
        preOrder(abt, index * 2 + 2);
    }

    /**
     * 中序遍历
     */
    public static void infixOrder(ArrBinaryTree abt, int index) {
   
        // 数组越界
        if (index >= abt.data.length) {
   
            return;
        }
        infixOrder(abt, index * 2 + 1);
        System.out.print(abt.data[index]);
        infixOrder(abt, index * 2 + 2);
    }

    /**
     * 后序遍历
     */
    public static void postOrder(ArrBinaryTree abt, int index) {
   
        // 数组越界
        if (index >= abt.data.length) {
   
            return;
        }
        postOrder(abt, index * 2 + 1);
        postOrder(abt, index * 2 + 2);
        System.out.print(abt.data[index]);
    }
}

3、后序线索二叉树

...

目录
相关文章
|
29天前
|
存储 机器学习/深度学习
【数据结构】二叉树全攻略,从实现到应用详解
本文介绍了树形结构及其重要类型——二叉树。树由若干节点组成,具有层次关系。二叉树每个节点最多有两个子树,分为左子树和右子树。文中详细描述了二叉树的不同类型,如完全二叉树、满二叉树、平衡二叉树及搜索二叉树,并阐述了二叉树的基本性质与存储方式。此外,还介绍了二叉树的实现方法,包括节点定义、遍历方式(前序、中序、后序、层序遍历),并提供了多个示例代码,帮助理解二叉树的基本操作。
44 13
【数据结构】二叉树全攻略,从实现到应用详解
|
26天前
|
存储 算法 C语言
数据结构基础详解(C语言): 二叉树的遍历_线索二叉树_树的存储结构_树与森林详解
本文从二叉树遍历入手,详细介绍了先序、中序和后序遍历方法,并探讨了如何构建二叉树及线索二叉树的概念。接着,文章讲解了树和森林的存储结构,特别是如何将树与森林转换为二叉树形式,以便利用二叉树的遍历方法。最后,讨论了树和森林的遍历算法,包括先根、后根和层次遍历。通过这些内容,读者可以全面了解二叉树及其相关概念。
|
26天前
|
存储 机器学习/深度学习 C语言
数据结构基础详解(C语言): 树与二叉树的基本类型与存储结构详解
本文介绍了树和二叉树的基本概念及性质。树是由节点组成的层次结构,其中节点的度为其分支数量,树的度为树中最大节点度数。二叉树是一种特殊的树,其节点最多有两个子节点,具有多种性质,如叶子节点数与度为2的节点数之间的关系。此外,还介绍了二叉树的不同形态,包括满二叉树、完全二叉树、二叉排序树和平衡二叉树,并探讨了二叉树的顺序存储和链式存储结构。
|
26天前
|
存储 C语言
数据结构基础详解(C语言): 树与二叉树的应用_哈夫曼树与哈夫曼曼编码_并查集_二叉排序树_平衡二叉树
本文详细介绍了树与二叉树的应用,涵盖哈夫曼树与哈夫曼编码、并查集以及二叉排序树等内容。首先讲解了哈夫曼树的构造方法及其在数据压缩中的应用;接着介绍了并查集的基本概念、存储结构及优化方法;随后探讨了二叉排序树的定义、查找、插入和删除操作;最后阐述了平衡二叉树的概念及其在保证树平衡状态下的插入和删除操作。通过本文,读者可以全面了解树与二叉树在实际问题中的应用技巧和优化策略。
|
2月前
|
算法
【初阶数据结构篇】二叉树算法题
二叉树是否对称,即左右子树是否对称.
|
2月前
|
存储
【初阶数据结构篇】实现链式结构二叉树(二叉链)下篇
要改变root指针的指向,将本来指向根节点的root指针改为空,所以传二级指针(一级指针也可以,只不过在调用完记得把root置为空)。
|
2月前
|
存储 测试技术
【初阶数据结构篇】实现链式结构二叉树(二叉链)上篇
先构建根结点,再对左右子树构建,每次需要时申请一个结点空间即可,否则返回空指针。
|
5天前
|
算法 安全 测试技术
golang 栈数据结构的实现和应用
本文详细介绍了“栈”这一数据结构的特点,并用Golang实现栈。栈是一种FILO(First In Last Out,即先进后出或后进先出)的数据结构。文章展示了如何用slice和链表来实现栈,并通过golang benchmark测试了二者的性能差异。此外,还提供了几个使用栈结构解决的实际算法问题示例,如有效的括号匹配等。
golang 栈数据结构的实现和应用
01_设计一个有getMin功能的栈
01_设计一个有getMin功能的栈
|
5天前
|
前端开发
07_用队列实现栈
07_用队列实现栈