树结构
为什么需要树这种数据结构?
1、数组存储方式的分析
优点:
通过下标方式访问元素,速度快。对于有序数组,还可使用二分查找提高检索速度
缺点:
如果要检索具体某个值,或者插入值(按一定顺序)会整体移动,效率较低

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

3、树存储方式的分析
能提高数据存储,读取的效率, 比如利用 二叉排序树(Binary Sort Tree),既可以保证数据的检索速度,同时也可以保证数据的插入,删除,修改的速度

树的常用术语
- 节点
- 根节点
- 父节点
- 子结点
- 叶子节点(没有子结点的节点)
- 节点的权(节点值)
- 路径(从root节点找到该节点的路线)
- 层
- 子树
- 树的高度(最大层数)
- 森林:多科子树构成森林

二叉树
-
树有很多种,每个节点最多只能有两个子节点的一种形式称为二叉树
-
二叉树的子节点分为左节点和右节点
![在这里插入图片描述]()
-
如果该二叉树的所有叶子节点都在最后一层,并且结点总数= 2n -1 , n 为层数,则我们称为满二叉树
![在这里插入图片描述]()
-
若设二叉树的深度为k,除第 k 层外,其它各层 (1~k-1) 的结点数都达到最大个数,第k 层所有的结点都连续集中在最左边,这就是完全二叉树。
![在这里插入图片描述]()
1、二叉树的遍历
二叉树的遍历分为
- 前序遍历: 先输出父节点,再遍历左子树和右子树
- 中序遍历: 先遍历左子树,再输出父节点,再遍历右子树
- 后序遍历: 先遍历左子树,再遍历右子树,最后输出父节点
看输出父节点的顺序,就确定是前序,中序还是后序
三种遍历方式思路分析

代码实现
public class BinaryTreeDemo {
public static void main(String[] args) {
HeroNode root = new HeroNode(1,"宋江");
HeroNode node2 = new HeroNode(2,"吴用");
HeroNode node4 = new HeroNode(4,"林冲");
HeroNode node3 = new HeroNode(3,"卢俊义");
HeroNode node5 = new HeroNode(5,"关胜");
//手动创建二叉树
root.setLeft(node2);
root.setRight(node3);
node3.setLeft(node5);
node3.setRight(node4);
System.out.println("前序遍历");
root.preOrder();
System.out.println("中序遍历");
root.infixOrder();
System.out.println("后序遍历");
root.postOrder();
}
}
class HeroNode{
private int no;
private String name;
//默认为null,左子结点和右子结点
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 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;
}
/**
* 前序遍历
*/
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);
}
}
//定义二叉树
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("当前二叉树为空,无法便利");
}
}
}
2、二叉树查找指定节点
要求
- 请编写前序查找,中序查找和后序查找的方法。
- 并分别使用三种查找方式,查找 heroNO = 5 的节点
- 并分析各种查找方式,分别比较了多少次
思路分析

代码实现
/**
* 前序遍历查找
* 找到就返回该node,没找到则返回null
*/
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;
}else {
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 (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 (resNode != null){
return resNode;
}
if (this.no == no){
return this;
}
return resNode;
}
3、二叉树删除指定节点
要求
- 如果删除的节点是叶子节点,则删除该节点
- 如果删除的节点是非叶子节点,则删除该子树
- 测试,删除掉 5号叶子节点 和 3号子树.
思路分析图

代码实现
public void delNode(int no){
if (root != null){
if (root.getNo() != no){
root.delNode(no);
}else {
root = null;
}
}else {
System.out.println("该树为空树,不可删除");
}
}
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);
}
}
4、顺序存储二叉树
从数据存储来看,数组存储方式和树的存储方式可以相互转换,即数组可以转换成树,树也可以转换成数组,

要求
- 右图的二叉树的结点,要求以数组的方式来存放 arr : [1, 2, 3, 4, 5, 6, 7]
- 要求在遍历数组 arr时,仍然可以以前序遍历,中序遍历和后序遍历的方式完成结点的遍历
顺序存储二叉树的特点
- 顺序二叉树通常只考虑完全二叉树
- 第n个元素的左子节点为 2 * n + 1
- 第n个元素的右子节点为 2 * n + 2
- 第n个元素的父节点为 (n-1) / 2
- n : 表示二叉树中的第几个元素(按0开始编号如图所示)
代码实现
public class ArrayBinaryTreeDemo {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7};
ArrBinaryTree arrBinaryTree = new ArrBinaryTree(arr);
arrBinaryTree.preOrder();
}
}
//编写以恶个ArrayBinaryTree,实现顺序存储二叉树遍历
class ArrBinaryTree{
private int[] arr;//存储数据节点的数组
public ArrBinaryTree(int[] arr) {
this.arr = arr;
}
public void preOrder(){
preOrder(0);
}
//编写一个方法,完成顺序存储二叉树的一个前序遍历
public void preOrder(int index){
//如果数组为空,或者arr.length=0
if (arr == null || arr.length == 0){
System.out.println("数组为空,不能遍历");
}
System.out.println(arr[index]);
//向左递归遍历
if ((index * 2+1)< arr.length){
preOrder(index * 2+1);
}
if ((index * 2+2)< arr.length){
preOrder(index*2+2);
}
}
}
线索化二叉树
- n个结点的二叉链表中含有n+1 【公式 2n-(n-1)=n+1,解释该公式:一个节点有两个指针,然后每个节点需要用掉一个指针就是2n-n,但是根节点不需要用指针,所以应该是2n-(n-1)】 个空指针域。利用二叉链表中的空指针域,存放指向该结点在某种遍历次序下的前驱和后继结点的指针(这种附加的指针称为"线索")
- 这种加上了线索的二叉链表称为线索链表,相应的二叉树称为线索二叉树(Threaded BinaryTree)。根据线索性质的不同,线索二叉树可分为前序线索二叉树、中序线索二叉树和后序线索二叉树三种
- 一个结点的前一个结点,称为前驱结点
- 一个结点的后一个结点,称为后继结点
实现线索化二叉树的思路分析

中序遍历的结果:{8, 3, 10, 1, 14, 6}
说明: 当线索化二叉树后,Node节点的 属性 left 和 right ,有如下情况:
- left 指向的是左子树,也可能是指向的前驱节点. 比如 ① 节点 left 指向的左子树, 而 ⑩ 节点的 left 指向的就是前驱节点
- right指向的是右子树,也可能是指向后继节点,比如 ① 节点right 指向的是右子树,而⑩ 节点的right 指向的是后继节点
代码实现
package com.imagpie.tree.threadedbinarytree;
/**
* 线索化二叉树
*/
public class ThreaderBinaryTree {
public static void main(String[] args) {
//测试中序线索二叉树的功能是否正确
HeroNode root = new HeroNode(1, "tom");
HeroNode node2 = new HeroNode(3, "jack");
HeroNode node3 = new HeroNode(6, "smith");
HeroNode node4 = new HeroNode(8, "mary");
HeroNode node5 = new HeroNode(10, "king");
HeroNode node6 = new HeroNode(14, "dim");
//手动创建二叉树
root.setLeft(node2);
root.setRight(node3);
node2.setLeft(node4);
node2.setRight(node5);
node3.setLeft(node6);
//测试线索化
BinaryTree threadedBinaryTree = new BinaryTree();
threadedBinaryTree.setRoot(root);
threadedBinaryTree.threadedNodes(root);
//以10号节点做测试
HeroNode leftNode = node5.getLeft();
System.out.println(leftNode);
}
}
//创建HeroNode
class HeroNode {
private int no;
private String name;
//默认为null,左子结点和右子结点
private HeroNode left;
private HeroNode right;
//如果leftType==0则表示指向的是左子树,如果是1则表示指向前驱结点
//如果rightType==0则表示指向的是右子树,如果是1则表示指向后继节点
private int leftType;
private int rightType;
public int getLeftType() {
return leftType;
}
public void setLeftType(int leftType) {
this.leftType = leftType;
}
public int getRightType() {
return rightType;
}
public void setRightType(int rightType) {
this.rightType = rightType;
}
public HeroNode(int no, String name) {
this.no = no;
this.name = name;
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", 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;
}
//递归删除节点
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 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);
}
/**
* 前序遍历查找
* 找到就返回该node,没找到则返回null
*/
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;
} else {
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 (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 (resNode != null) {
return resNode;
}
if (this.no == no) {
return this;
}
return resNode;
}
}
//定义二叉树,实现了线索化功能的二叉树
class BinaryTree {
private HeroNode root;
//为了实现线索化,需要创建要给指向当前接待你的前驱结点的指针
//在递归进行线索化时,pre总是保留前一个节点
private HeroNode pre = null;
public void setRoot(HeroNode root) {
this.root = root;
}
//编写对二叉树进行中序线索化的方法
public void threadedNodes(HeroNode node) {
//如果node=null,则不能线索化
if (node == null){
return;
}
//1、先线索化左子树
threadedNodes(node.getLeft());
//2、线索化当前节点
//处理当前节点的前驱结点
if (node.getLeft() == null){
//让当前节点的左指针指向前驱结点
node.setLeft(pre);
//修改当前节点的左指针类型,指向前驱结点
node.setLeftType(1);
}
//处理后继节点
if (pre != null && pre.getRight() == null){
//让前驱结点的右指针指向当前节点
pre.setRight(node);
//修改当前节点的右指针类型
pre.setRightType(1);
}
//每处理一个节点后,就要当当前节点为下一个节点的前驱结点
pre = node;
//3、再线索化右子树
threadedNodes(node.getRight());
}
//递归删除节点
public void delNode(int no) {
if (root != null) {
if (root.getNo() != no) {
root.delNode(no);
} else {
root = null;
}
} 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.postOrderSearch(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;
}
}
}
上面遍历的方法是有问题的
遍历线索化二叉树
思路分析
说明:对前面的中序线索化的二叉树, 进行遍历
分析:因为线索化后,各个结点指向有变化,因此原来的遍历方式不能使用,这时需要使用新的方式遍历线索化二叉树,各个节点可以通过线型方式遍历,因此无需使用递归方式,这样也提高了遍历的效率。 遍历的次序应当和中序遍历保持一致。
代码实现
//遍历线索化二叉树的方法
public void threadedList(){
//定义一个变量,临时存储当前遍历的节点,从root开始遍历
HeroNode node = root;
while (node != null){
//当标记为0时正常遍历
while (node.getLeftType() == 0){
node = node.getLeft();
}
//当标记不为0,则表示标记肯定是1,此时需要输出该节点
System.out.println(node);
//然后循环处理该节点往后的节点,如果后面的节点的标记都是1的话,则直接获取当前节点的后继节点直接输出即可
while (node.getRightType() == 1){
node = node.getRight();
System.out.println(node);
}
//如果此时标记不为1了,则正常遍历
node = node.getRight();
}
}




浙公网安备 33010602011771号