- 为什么需要树
- 树示意图及常用术语
- 二叉树
- 二叉树概念
- 前序中序后序遍历二叉树
- 代码实现前序中序后序遍历二叉树
- 二叉树查找指定结点
- 代码实现二叉树查找指定结点
- 二叉树删除结点
- 代码实现二叉树删除结点
- 顺序存储二叉树
- 概念
- 特点
- 代码实现
- 线索化二叉树
- 基本介绍
- 思路图解
- 遍历线索化二叉树思路
- 代码实现
- 数组存储方式分析:
优点:通过下标方式访问元素,速度快。对于有序数组还可以使用二分查找提高检索速度。
缺点:如果要检索具体某个值,或者插入值(按一定顺序)会整体移动,效率较低。
- 链式存储方式分析:
优点:在一定程度上对数组存储方式有优化(比如:插入一个数值节点,只需要将插入节点,链接到链表中即可,删除效率也很好)。
缺点:在进行检索时,效率仍然较低,比如(检索某个值,需要从头节点开始遍历)。
- 树存储方式分析:
能提高数据存储、读取的效率,比如利用二叉排序树(Binary Sort Tree),既可以保证数据的检索速度,同时也可以保证数据的插入、删除、修改的速度。
- 树有很多种,每个节点最多只能有两个子节点的一种形式称为二叉树。
- 二叉树的子节点分为左节点和右节点。
- 如果二叉树的所有叶子节点都在最后一层,并且结点总数=2^n-1,n为层数,则称为满二叉树。
- 如果二叉树的所有叶子节点都在最后一层或者倒数第二层,而且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续,称为完全二叉树。
前序遍历:先输出父节点,再遍历左子树和右子树
中序遍历:先遍历左子树,再输出父节点,再遍历右子树
后序遍历:先遍历左子树,再遍历右子树,最后输出父节点
小结:看输出父节点的顺序,就确定是前序、中序还是后序
package com.datastructures.tree;
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("前序遍历");
binaryTree.preOrder();
System.out.println("中序遍历");
binaryTree.infixOrder();
System.out.println("后序遍历");
binaryTree.postOrder();
}
}
//定义一个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("二叉树为空,无法遍历");
}
}
}
//先创建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 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;
}
@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.postOrder();
}
//递归向右子树后序遍历
if (this.right != null) {
this.right.postOrder();
}
//输出当前结点
System.out.println(this);
}
}
二叉树查找指定结点
代码实现二叉树查找指定结点
package com.datastructures.tree;
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("前序遍历");
// binaryTree.preOrder();
// System.out.println("中序遍历");
// binaryTree.infixOrder();
// System.out.println("后序遍历");
// binaryTree.postOrder();
int no = 5;
System.out.println("前序遍历查找");
HeroNode resNode = binaryTree.preOrderSearch(no);
if (resNode != null) {
System.out.printf("找到:no = %d, name = %s", resNode.getNo(), resNode.getName());
} else {
System.out.printf("没有找到no = %d 的英雄", no);
}
System.out.println();
System.out.println("中序遍历查找");
resNode = null;
resNode = binaryTree.infixOrderSearch(no);
if (resNode != null) {
System.out.printf("找到:no = %d, name = %s", resNode.getNo(), resNode.getName());
} else {
System.out.printf("没有找到no = %d 的英雄", no);
}
System.out.println();
System.out.println("后序遍历查找");
resNode = null;
resNode = binaryTree.postOrderSearch(no);
if (resNode != null) {
System.out.printf("找到:no = %d, name = %s", resNode.getNo(), resNode.getName());
} else {
System.out.printf("没有找到no = %d 的英雄", no);
}
}
}
//定义一个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 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 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;
}
@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.postOrder();
}
//递归向右子树后序遍历
if (this.right != null) {
this.right.postOrder();
}
//输出当前结点
System.out.println(this);
}
//前序遍历查找
public HeroNode preOrderSearch(int no) {
System.out.println("进入前序遍历查找");
//比较当前结点
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;
}
System.out.println("进入中序遍历查找");
//比较当前结点
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;
}
System.out.println("进入后序遍历查找");
//与当前结点比较
if (this.no == no) {
return this;
}
return resNode;
}
}
二叉树删除结点
要求:
- 如果删除的结点是叶子结点,则删除该结点
- 如果删除的结点是非叶子结点,则删除该子树
package com.datastructures.tree;
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("删除前,前序遍历");
binaryTree.preOrder();
// binaryTree.delNode(5);
binaryTree.delNode(3);
System.out.println("删除后,前序遍历");
binaryTree.preOrder();
//测试
// System.out.println("前序遍历");
// binaryTree.preOrder();
// System.out.println("中序遍历");
// binaryTree.infixOrder();
// System.out.println("后序遍历");
// binaryTree.postOrder();
// int no = 5;
// System.out.println("前序遍历查找");
// HeroNode resNode = binaryTree.preOrderSearch(no);
// if (resNode != null) {
// System.out.printf("找到:no = %d, name = %s", resNode.getNo(), resNode.getName());
// } else {
// System.out.printf("没有找到no = %d 的英雄", no);
// }
//
// System.out.println();
// System.out.println("中序遍历查找");
// resNode = null;
// resNode = binaryTree.infixOrderSearch(no);
// if (resNode != null) {
// System.out.printf("找到:no = %d, name = %s", resNode.getNo(), resNode.getName());
// } else {
// System.out.printf("没有找到no = %d 的英雄", no);
// }
//
// System.out.println();
// System.out.println("后序遍历查找");
// resNode = null;
// resNode = binaryTree.postOrderSearch(no);
// if (resNode != null) {
// System.out.printf("找到:no = %d, name = %s", resNode.getNo(), resNode.getName());
// } else {
// System.out.printf("没有找到no = %d 的英雄", no);
// }
}
}
//定义一个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 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;
}
}
//删除结点
public void delNode(int no) {
//判断root是否为空 root是否为要被删除的结点
if (root != null) {
if (root.getNo() == no) {
root = null;
} else {
root.delNode(no);
}
} else {
System.out.println("空树,不能删除");
}
}
}
//先创建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 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;
}
@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.postOrder();
}
//递归向右子树后序遍历
if (this.right != null) {
this.right.postOrder();
}
//输出当前结点
System.out.println(this);
}
//前序遍历查找
public HeroNode preOrderSearch(int no) {
System.out.println("进入前序遍历查找");
//比较当前结点
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;
}
System.out.println("进入中序遍历查找");
//比较当前结点
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;
}
System.out.println("进入后序遍历查找");
//与当前结点比较
if (this.no == no) {
return this;
}
return resNode;
}
//递归删除结点
//如果删除的结点是叶子结点,则删除该结点
//如果删除的结点是非叶子结点,则删除该子树
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);
}
}
}
顺序存储二叉树
概念
从数据存储来看,数组存储方式和树的存储方式可以互相转换,即数组可以转换成树,树也可以转换成数组。在堆排序中会使用到顺序存储二叉树。
- 顺序二叉树通常只考虑完全二叉树
- 第n个元素的左子节点为 2 * n + 1
- 第n个元素的右子结点为 2 * n + 2
- 第n个元素的父节点为 (n - 1) / 2
- n:表示二叉树中的第几个元素(从0开始编号—跟数组保持一致)
package com.datastructures.tree;
public class ArrBinaryTreeDemo {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7};
//创建一个ArrBinaryTree
ArrBinaryTree arrBinaryTree = new ArrBinaryTree(arr);
System.out.println("前序遍历:");
arrBinaryTree.preOrder(); // 1 2 4 5 3 6 7
System.out.println("中序遍历:");
arrBinaryTree.infixOrder(0); // 4 2 5 1 6 3 7
System.out.println("后序遍历:");
arrBinaryTree.postOrder(0); // 4 5 2 6 7 3 1
}
}
//编写一个ArrBinaryTree,实现顺序存储二叉树遍历
class ArrBinaryTree {
private int[] arr; //存储数据结点的数组
public ArrBinaryTree(int[] arr) {
this.arr = arr;
}
//重载preOrder
public void preOrder() {
this.preOrder(0);
}
//编写一个方法,完成顺序存储二叉树的前序遍历
//index表示数组的下标,即n
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);
}
}
//中序遍历
public void infixOrder(int index) {
//如果数组为空,或者 arr.length = 0
if (arr == null || arr.length == 0) {
System.out.println("数组为空,不能按照二叉树的前序遍历");
}
//向左递归遍历
if ((index * 2 + 1) < arr.length) {
infixOrder(index * 2 + 1);
}
//输出当前元素
System.out.println(arr[index]);
//向右递归遍历
if ((index * 2 + 2) < arr.length) {
infixOrder(index * 2 + 2);
}
}
//后序遍历
public void postOrder(int index) {
//如果数组为空,或者 arr.length = 0
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.println(arr[index]);
}
}
线索化二叉树
基本介绍
- n个结点的二叉链表中含有n+1(公式2n-(n-1)=n+1)个空指针域。利用二叉链表中的空指针域,存放指向该结点在某种遍历次序下的前驱和后继结点的指针(这种附加的指针称为“线索”)
- 这种加上了线索的二叉链表称为线索链表,相应的二叉树称为线索二叉树(Threaded BinaryTree)。根据线索性质的不同,线索二叉树可分为前序线索二叉树、中序线索二叉树和后序线索二叉树三种
- 一个结点的前一个结点,称为前驱结点
- 一个结点的后一个结点,称为后继节点
**说明:**对进行中序线索化的二叉树,进行遍历
分析:因为线索化后,各个结点指向有所变化,因此原来的遍历方式不能使用,否则会死递归,这时需要使用新的方式遍历线索化二叉树,各个结点可以通过线性方式遍历,无需使用递归方式,可以提高效率。遍历后的顺序应当和使用线索化的方式的遍历(前序遍历、中序遍历、后序遍历)保持一致。



