目录
一.为什么需要树这种数据结构
1) 数组存储方式的分析
优点:通过下标方式访问元素,速度快。对于有序数组,还可使用二分查找提高检索速度。
缺点:如果要检索具体某个值,或者插入值(按一定顺序)会整体移动,效率较低。
2) 链式存储方式的分析
优点:在一定程度上对数组存储方式有优化(比如:插入一个数值节点,只需要将插入节点,链接到链表中即可, 删除效率也很好)。
缺点:在进行检索时,效率仍然较低,比如(检索某个值,需要从头节点开始遍历。
3)树存储方式的分析
能提高数据存储,读取的效率, 比如利用 二叉排序树(Binary Sort Tree),既可以保证数据的检索速度,同时也 可以保证数据的插入,删除,修改的速度。
二.二叉树
1.二叉树的概念
- 1)树有很多种,每个节点最多只能有两个子节点的一种形式称为二叉树.
- 2)如果该二叉树的所有叶子节点都在最后一层,并且结点总数= 2^n -1 , n 为层数,则我们称为满二叉树。
- 3)如果该二叉树的所有叶子节点都在最后一层或者倒数第二层,而且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续,我们称为完全二叉树
2.二叉树的遍历
- 1) 前序遍历: 先输出父节点,再遍历左子树和右子树
- 2) 中序遍历:先遍历左子树,再输出父节点,再遍历右子树
- 3) 后序遍历: 先遍历左子树,再遍历右子树,最后输出父节点
前序遍历:
- 先输出当前节点(初始的时候是root节点)
- 如果左子节点不为空,则递归继续前序遍历
- 如果右子节点不为空,则递归继续前序遍历3.中序遍历
中序遍历:
- 如果当前节点的左子节点不为空,则递归中序遍历
- 输出当前节点
- 如果当前节点的右子节点不为空,则递归中序遍历
后序遍历:
- 如果当前节点的左子节点不为空,则递归后序遍历
- 如果当前节点的右子节点不为空,则递归后序遍历
- 输出当前节点
创建一个二叉树
前序遍历:1 2 6 3 4 5
中序遍历:2 6 1 4 3 5
后序遍历:6 2 4 5 3 1
3.代码实现
package com.ws.binary;
public class BinaryTree {
//创建根节点
private StudentNode root;
public void setRoot(StudentNode root) {
this.root = root;
}
/**
* 前序遍历
*/
public void preOrder() {
if (root != null) {
root.preOrder();
} else {
System.out.println("二叉树为空~无法遍历");
}
}
/**
* 中序遍历
*/
public void inOrder() {
if (root != null) {
root.inOrder();
} else {
System.out.println("二叉树为空~无法遍历");
}
}
/**
* 后序遍历
*/
public void postOrder() {
if (root != null) {
root.postOrder();
} else {
System.out.println("二叉树为空~无法遍历");
}
}
/**
* 前序查找节点
*/
public StudentNode preOrderQueryNode(int node) {
if (root != null) {
return root.preOrderQueryNode(node);
} else {
return null;
}
}
/**
* 中序查找节点
*/
public StudentNode inOrderQueryNode(int node) {
if (root != null) {
return root.inOrderQueryNode(node);
} else {
return null;
}
}
/**
* 后序查找节点
*/
public StudentNode postOrderQueryNode(int node) {
if (root != null) {
return root.postOrderQueryNode(node);
} else {
return null;
}
}
/**
* 删除指定节点
* @param node
*/
public void delNode(int node) {
if (root != null) {
//如果根节点就是要删除的节点
if (root.no == node) {
root = null;
} else {
root.delNode(node);
}
} else {
System.out.println("二叉树为空~删除失败");
}
}
}
class StudentNode {
//学号
public int no;
//姓名
public String name;
//左节点
public StudentNode left;
//右节点
public StudentNode right;
//构造器
public StudentNode(int no, String name) {
this.no = no;
this.name = name;
}
@Override
public String toString() {
return "StudentNode{" +
"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 inOrder() {
//如果左节点不为空 想左遍历
if (this.left != null) {
this.left.inOrder();
}
//输出当前节点
System.out.println(this);
//如果右节点不为空 向右遍历
if (this.right != null) {
this.right.inOrder();
}
}
/**
* 后序遍历
*/
public void postOrder() {
//如果左节点不为空 想左遍历
if (this.left != null) {
this.left.postOrder();
}
//如果右节点不为空 向右遍历
if (this.right != null) {
this.right.postOrder();
}
//输出当前节点
System.out.println(this);
}
/**
* 前序查找节点
*/
public StudentNode preOrderQueryNode(int node) {
//先进行判断 若等于当前节点 直接返回
if (this.no == node) {
return this;
}
//如果左节点不为空 想左遍历
StudentNode resNode = null;
if (this.left != null) {
resNode = this.left.preOrderQueryNode(node);
//判断是否找到 resNode != null说明找到 直接退出
if (resNode != null) {
return resNode;
}
}
//如果右节点不为空 向右遍历
if (this.right != null) {
resNode = this.right.preOrderQueryNode(node);
//判断是否找到 resNode != null说明找到 直接退出
if (resNode != null) {
return resNode;
}
}
return resNode;
}
/**
* 中序查找节点
*/
public StudentNode inOrderQueryNode(int node) {
StudentNode resNode = null;
//如果左节点不为空 想左遍历
if (this.left != null) {
resNode = this.left.inOrderQueryNode(node);
//判断是否找到 resNode != null说明找到 直接退出
if (resNode != null) {
return resNode;
}
}
//先进行判断 若等于当前节点 直接返回
if (this.no == node) {
return this;
}
//如果右节点不为空 向右遍历
if (this.right != null) {
resNode = this.right.inOrderQueryNode(node);
//判断是否找到 resNode != null说明找到 直接退出
if (resNode != null) {
return resNode;
}
}
return resNode;
}
/**
* 后序查找节点
*/
public StudentNode postOrderQueryNode(int node) {
StudentNode resNode = null;
//如果左节点不为空 想左遍历
if (this.left != null) {
resNode = this.left.postOrderQueryNode(node);
//判断是否找到 resNode != null说明找到 直接退出
if (resNode != null) {
return resNode;
}
}
//如果右节点不为空 向右遍历
if (this.right != null) {
resNode = this.right.postOrderQueryNode(node);
//判断是否找到 resNode != null说明找到 直接退出
if (resNode != null) {
return resNode;
}
}
//先进行判断 若等于当前节点 直接返回
if (this.no == node) {
return this;
}
return resNode;
}
/**
*删除指定节点
* 如果删除的节点是叶子节点,则删除该节点
* 如果删除的节点是非叶子节点,则删除该子树
* @param node
*/
public void delNode(int node) {
//如果左节点不为空 且就是要删除的节点
if (this.left != null && this.left.no == node) {
// //要删除的节点是个叶子
// if (this.left.left == null && this.left.right == null) {
// this.left = null;
// }
// //要删除的节点只有左子节点
// if (this.left.left != null && this.left.right == null) {
// this.left = this.left.left;
// }
// //要删除的节点只有右子节点
// if (this.left.left == null && this.left.right != null) {
// this.left = this.left.right;
// }
// //要删除的节点只有子节点 和右节点
// if (this.left.left != null && this.left.right != null) {
// this.left = this.left.right;
// }
this.left = null;
return;
}
//再判断右子节点
if (this.right != null && this.right.no == node) {
this.right = null;
return;
}
//向左递归
if (this.left != null) {
this.left.delNode(node);
}
//向右递归
if (this.right != null) {
this.right.delNode(node);
}
}
}
4.测试代码
package com.ws.binary;
import com.ws.binary.BinaryTree;
public class Test {
public static void main(String[] args) {
BinaryTree binaryTree = new BinaryTree();
StudentNode root = new StudentNode(1, "zs");
binaryTree.setRoot(root);
StudentNode root2 = new StudentNode(2, "ls");
StudentNode root3 = new StudentNode(3, "ww");
StudentNode root4 = new StudentNode(4, "cl");
StudentNode root5 = new StudentNode(5, "sq");
StudentNode root6 = new StudentNode(6, "jb");
root.left = root2;
root.right = root3;
root2.right = root6;
root3.left = root4;
root3.right = root5;
//前序
binaryTree.preOrder();
System.out.println();
binaryTree.delNode(3);
binaryTree.preOrder();
//中序
// binaryTree.inOrder();
//后序
// binaryTree.postOrder();
// for (int i = 1; i < 7; i++) {
// System.out.println(binaryTree.postOrderQueryNode(i));
// }
}
}
三.顺序存储二叉树
1.什么是顺序存储二叉树
2.顺序存储二叉树的特点:
1) 顺序二叉树通常只考虑完全二叉树
2) 第 n 个元素的左子节点为 2 * n + 1
3) 第 n 个元素的右子节点为 2 * n + 2
4) 第 n 个元素的父节点为 (n-1) / 2
3.前序遍历数组代码实现
package com.ws.binary;
public class ArrBinaryTree {
int[] arr;
//构造器
public ArrBinaryTree(int[] arr) {
this.arr = arr;
}
//重载
public void preOder() {
this.preOrder(0);
}
/**
* 前序遍历
* @param index 数组的下标
*/
public void preOrder(int index) {
if (arr == null || arr.length == 0) {
System.out.println("数组为空~无法遍历");
}
System.out.print(arr[index] + " ");
//向左遍历
if (2 * index +1 < arr.length) {
preOrder(2 * index +1);
}
//向右遍历
if (2 * index +2 < arr.length) {
preOrder(2 * index +2);
}
}
}
4.测试代码
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7};
ArrBinaryTree arrBinaryTree = new ArrBinaryTree(arr);
arrBinaryTree.preOrder();
}
四.线索化二叉树
1.先看一个问题
将数列 {1, 3, 6, 8, 10, 14 } 构建成一颗二叉树.
2 线索二叉树基本介绍
1) n 个结点的二叉链表中含有 n+1 【公式 2n-(n-1)=n+1】 个空指针域。利用二叉链表中的空指针域,存放指向 该结点在某种遍历次序下的前驱和后继结点的指针(这种附加的指针称为"线索")
2) 这种加上了线索的二叉链表称为线索链表,相应的二叉树称为线索二叉树。根据线索性质 的不同,线索二叉树可分为前序线索二叉树、中序线索二叉树和后序线索二叉树三种
3) 一个结点的前一个结点,称为前驱结点
4) 一个结点的后一个结点,称为后继结点
3 .线索二叉树应用案例
思路分析: 中序遍历的结果:{8, 3, 10, 1, 14, 6}
说明: 当线索化二叉树后,Node 节点的 属性 left 和 right ,有如下情况:
1) left 指向的是左子树,也可能是指向的前驱节点. 比如 ① 节点 left 指向的左子树, 而 ⑩ 节点的 left 指向的 就是前驱节点.
2) right 指向的是右子树,也可能是指向后继节点,比如 ① 节点 right 指向的是右子树,而⑩ 节点的 right 指向的是后继节点.
4.中序线索化二叉树代码实现
package com.ws.binary;
public class ThreadedBinaryTree {
//创建根节点
private Node root;
//pre总是指向当前节点的前驱节点
private Node pre;
public void setRoot(Node root) {
this.root = root;
}
//重载
public void Threaded() {
this.Threaded(root);
}
/**
* 中序线索化二叉树
* @param node 当前节点咩咩咩咩吗
*/
public void Threaded(Node node) {
if (node == null) {
return;
}
//先向左线索化
if (node.left != null) {
Threaded(node.left);
}
//线索化当前节点
//前驱
if (node.left == null) {
node.left = pre;
node.leftType = 1;
}
//后继 利用pre指向下个节点
if (pre != null && pre.right == null) {
pre.right = node;
pre.rightType = 1;
}
//pre 后移
pre = node;
//向右线索会啊
if (node.right != null) {
Threaded(node.right);
}
}
}
class Node {
//编号
public int no;
//左节点
public Node left;
//右节点
public Node right;
//0 代表left指向的是左子树 1 代表left指向的是前驱节点
public int leftType;
public int rightType;
//构造器
public Node(int no) {
this.no = no;
}
@Override
public String toString() {
return "Node{" +
"no=" + no +
'}';
}
}
5.测试代码
public static void main(String[] args) {
ThreadedBinaryTree threadedBinaryTree = new ThreadedBinaryTree();
Node root = new Node(1);
threadedBinaryTree.setRoot(root);
Node root2 = new Node(3);
Node root3 = new Node(6);
Node root4 = new Node(8);
Node root5 = new Node(10);
Node root6 = new Node(14);
root.left = root2;
root.right = root3;
root2.left = root4;
root2.right = root5;
root3.left = root6;
threadedBinaryTree.Threaded();
System.out.println("前驱节点:" + root5.left + " " + "后继节点" + root5.right);
}
结果:
7.遍历线索化二叉树代码
/**
* 遍历线索化二叉树
*/
public void threadedList() {
Node node = root;
while (node != null) {
//循环找到线索二叉树leftType = 1的节点 第一次找到的一定是线索二叉树的第一个元素
while (node.leftType == 0) {
node = node.left;
}
//输出当前节点
System.out.println(node);
//如果这个节点的右节点是后继节点 就一直输出
while (node.leftType == 1) {
node = node.right;
System.out.println(node);
}
//否则 替换该节点
node = node.right;
}
}
测试结果: