1. 二叉树
1.1 为什么需要二叉树
- 数组存储方式
优点:通过下标方式访问元素,速度快。对于有序数组,可以使用二分查找提高检索速度
缺点:如果要检索具体某个值,或插入值会整体移动,效率低
- 链式存储方式
优点:插入,删除节点,只需要链接到链表即可,效率很好
缺点,进行检索时,需要从头节点开始遍历,效率较低
- 树存储方式
能提高数据存储,读取的效率,比如利用二叉排序树(Binary Sort Tree),既可以保证数据的检索速度,也可以保证数据的插入删除速度
1.2 树的概念
二叉树
子节点分为左节点和右节点
满二叉树
所有叶子节点都在最后一层,节点总数为2^n-1,n为层树,称为满二叉树
完全二叉树
该二叉树的所有叶子节点都在最后一层或倒数第二层,而且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续,称为完全二叉树
1.3 二叉树的遍历
- 前序遍历:先输出父节点,再遍历左子树和右子树
- 中序遍历:先遍历左子树,再输出父节点,再遍历右子树
- 后序遍历:先遍历左子树,再遍历右子树,最后输入父节点
1.4 二叉树的查找
1.5 二叉树的删除
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();
// System.out.println("前序查找");
// HeroNode resNode = binaryTree.preOrderSearch(5);
// if (resNode != null) {
// System.out.println(resNode);
// } else {
// System.out.println("没有找到");
// }
// System.out.println("后序查找");
// resNode = binaryTree.postOrderSearch(5);
// System.out.println("中序查找");
// resNode = binaryTree.infixOrderSearch(5);
//删除
System.out.println("删除前");
binaryTree.preOrder();
binaryTree.delNode(5);
System.out.println("删除后");
binaryTree.preOrder();
}
}
//定义二叉树
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) {
if (root != null) {
//如果只有一个root节点,判断root是不是要判断的节点
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;//默认null
private HeroNode right;//默认null
//前序遍历
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(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 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);
}
}
}
2. 顺序存储二叉树
2.1 顺序存储二叉树的概念
- 从数据存储来看,数组存储方式和树的存储方式可以相互转换
- 顺序二叉树通常只考虑完全二叉树
- 第n个元素的左子节点为2n+1
- 第n个元素的右子节点为2n+2
- 第n个元素的父节点为(n-1)/2
2.2 顺序存储二叉树的遍历
- 八大排序算法中的堆排序,会使用到顺序存储二叉树
public class ArrayBinaryTreeDemo {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7};
ArrayBinaryTree arrayBinaryTree = new ArrayBinaryTree(arr);
arrayBinaryTree.preOrder();
}
}
//ArrayBinaryTree类实现顺序存储二叉树遍历
class ArrayBinaryTree {
private int[] arr;//存储数据节点的数组
public ArrayBinaryTree(int[] arr) {
this.arr = arr;
}
public void preOrder() {
this.preOrder(0);
}
//顺序存储二叉树的前序遍历,index数组的下标
public void preOrder(int index) {
if (arr == null || arr.length == 0) {
System.out.println("数据为空");
}
System.out.println(arr[index]);
//向左递归遍历
if ((2 * index + 1) < arr.length) {
preOrder(2 * index + 1);
}
//向右递归
if ((2 * index + 2) < arr.length) {
preOrder(2 * index + 2);
}
}
}
3. 线索化二叉树
3.1 基本介绍
- n个节点的二叉链表中含有n+1个空指针域,利用二叉链表中的空指针域,存放指向该节点在某次遍历次序下的前驱和后继节点的指针,这种附加的指针称为“线索”
- 这种加上了线索的二叉链表称为线索链表,相应的二叉树称为线索二叉树(Threaded BinaryTree)。根据线索性质的不同,可以分为前序线索二叉树、中序线索二叉树和后续线索二叉树
- 一个节点的前一个节点,称为前驱节点
- 一个节点的后一个节点,称为后继节点
3.2 线索化二叉树的生成
3.3 线索化二叉树的遍历
public class ThreadedBinaryTreeDemo {
public static void main(String[] args) {
Node root = new Node(1, "tom");
Node node2 = new Node(3, "jack");
Node node3 = new Node(6, "smith");
Node node4 = new Node(8, "mary");
Node node5 = new Node(10, "king");
Node node6 = new Node(14, "dim");
root.setLeft(node2);
root.setRight(node3);
node2.setLeft(node4);
node2.setRight(node5);
node3.setLeft(node6);
ThreadedBinaryTree binaryTree = new ThreadedBinaryTree();
binaryTree.setRoot(root);
binaryTree.threadedNodes(root);
Node leftNode = node5.getLeft();
System.out.println("10号节点的前驱节点" + leftNode);
System.out.println("10号节点的后继节点" + node5.getRight());
System.out.println("线索化的方式遍历线索化二叉树");
binaryTree.threadedList();
}
}
//定义线索化二叉树
class ThreadedBinaryTree {
private Node root;
//指向前驱节点的指针
private Node pre = null;
public void setRoot(Node root) {
this.root = root;
}
//遍历线索化二叉树的方法
public void threadedList() {
//存储当前遍历的节点,从root开始
Node node = root;
while (node != null) {
//循环找到leftType=1的节点
while (node.getLeftType() == 0) {
node = node.getLeft();
}
System.out.println(node);
//当前节点的右指针指向的是后继节点,一直输出
while (node.getRightType() == 1) {
//当前节点的后继节点
node = node.getRight();
System.out.println(node);
}
//替换遍历的节点
node = node.getRight();
}
}
//对二叉树进行中序线索化
public void threadedNodes(Node node) {
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 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 Node preOrderSearch(int no) {
if (root != null) {
return root.preOrderSearch(no);
} else {
return null;
}
}
//中序查找
public Node infixOrderSearch(int no) {
if (root != null) {
return root.infixOrderSearch(no);
} else {
return null;
}
}
//前序查找
public Node postOrderSearch(int no) {
if (root != null) {
return root.postOrderSearch(no);
} else {
return null;
}
}
//删除节点
public void delNode(int no) {
if (root != null) {
//如果只有一个root节点,判断root是不是要判断的节点
if (root.getNo() == no) {
root = null;
} else {
root.delNode(no);
}
} else {
System.out.println("这是空树");
}
}
}
//Node节点
class Node {
private int no;
private String name;
private Node left;//默认null
private Node right;//默认null
//lefType=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 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 Node(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 Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
@Override
public String toString() {
return "Node{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
//前序遍历查找
public Node preOrderSearch(int no) {
System.out.println("前序开始查找");
//比较当前节点
if (this.no == no) {
return this;
}
Node 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 Node infixOrderSearch(int no) {
Node 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 Node postOrderSearch(int no) {
Node 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);
}
}
}