为什么需要树这种数据结构?
数组存储方式的分析:
优点:通过下标方式访问元素,速度快。对于有序数组,还可使用二分查找提高检索速度。
缺点:如果要检索具体某个值,或者插入值(按一定顺序)会整体移动,效率较低
链式存储方式的分析:
优点:在一定程度上对数组存储方式有优化
比如:插入一个数值节点,只需要将插入节点,链接到链表中即可, 删除效率也很好)。
缺点:在进行检索时,效率仍然较低,比如(检索某个值,需要从头节点开始遍历)
树存储方式的分析:
能提高数据存储,读取的效率, 比如利用 二叉排序树(Binary Sort Tree),既可以保证数据的检索速度,同时也可以保证数据的插入,删除,修改的速度。
二叉树
1、树有很多种,每个节点最多只能有两个子节点的一种形式称为二叉树。
2、二叉树的子节点分为左节点和右节点。如图:
3、如果该二叉树的所有叶子节点都在最后一层,并且结点总数= 2^n -1 , n 为层数,则我们称为满二叉树。
4、如果该二叉树的所有叶子节点都在最后一层或者倒数第二层,而且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续,我们称为完全二叉树。
二叉树的遍历:
前序遍历: 先输出父节点,再遍历左子树和右子树
中序遍历: 先遍历左子树,再输出父节点,再遍历右子树
后序遍历: 先遍历左子树,再遍历右子树,最后输出父节点
应用实例分析:
package com.datastructures.binarytree;
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.infixOreder();
System.out.println("后序遍历:");
binaryTree.postOrder();
}
}
//定义二叉树
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 infixOreder() {
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("二叉树为空!无法遍历!");
}
}
}
//创建结点
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);//输出当前结点
}
}
二叉树的遍历查找:
应用实例分析:
代码实现:
ackage com.datastructures.binarytree;
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("前序遍历查找:");//4次
HeroNode resNode = binaryTree.preOrderSearch(5);
if (resNode != null) {
System.out.printf("已找到 no=%d name=%s\n", resNode.getNo(), resNode.getName());
} else {
System.out.printf("没有找到 no = %d 的英雄\n", 5);
}
System.out.println("中序遍历查找:");//3次
resNode = binaryTree.infixOrderSearch(5);
if (resNode != null) {
System.out.printf("已找到 no=%d name=%s\n", resNode.getNo(), resNode.getName());
} else {
System.out.printf("没有找到 no = %d 的英雄\n", 5);
}
System.out.println("后序遍历查找:");//2次
resNode = binaryTree.postOrderSearch(5);
if (resNode != null) {
System.out.printf("已找到 no=%d name=%s\n", resNode.getNo(), resNode.getName());
} else {
System.out.printf("没有找到 no = %d 的英雄\n", 5);
}
}
}
//定义二叉树
class BinaryTree {
private HeroNode root;
public void setRoot(HeroNode root) {
this.root = root;
}
//前序遍历查找
public HeroNode preOrderSearch(int no) {
if (this.root != null) {
return this.root.preOrderSearch(no);
} else {
return null;
}
}
//中序遍历查找
public HeroNode infixOrderSearch(int no) {
if (this.root != null) {
return this.root.infixOrderSearch(no);
} else {
return null;
}
}
//后序遍历查找
public HeroNode postOrderSearch(int no) {
if (this.root != null) {
return this.root.postOrderSearch(no);
} else {
return null;
}
}
}
//创建结点
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 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;
}
}
二叉树删除节点:
要求:
- 如果删除的节点是叶子节点,则删除该节点
- 如果删除的节点是非叶子节点,则删除该子树
- 测试,删除掉 5 号叶子节点 和 3 号子树.
应用实例分析:
代码实现:
package com.datastructures.binarytree;
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(3);
System.out.println("删除后:");
binaryTree.preOrder();
}
}
//定义二叉树
class BinaryTree {
private HeroNode root;
public void setRoot(HeroNode root) {
this.root = root;
}
//递归删除节点
public void delNode(int no) {
if (root != null) {
if(root.getNo()==no){
root=null;
}else{
root.delNode(no);
}
} else {
System.out.println("空树 无法删除节点");
}
}
//前序遍历
public void preOrder() {
if (this.root != null) {
this.root.preOrder();
} else {
System.out.println("二叉树为空!无法遍历!");
}
}
//中序遍历
public void infixOreder() {
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("二叉树为空!无法遍历!");
}
}
}
//创建结点
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 + '\'' +
'}';
}
//递归删除节点
/*1、如果删除的节点是叶子节点,则删除该节点
* 2、如果删除的节点是非叶子节点,则删除该子树
* */
public void delNode(int no) {
//思路:
// 1、因为我们的二叉树是单向的 所以我们只能判断当前节点的子节点是否是需要删除的节点 而不能判断当前这个节点是不是需要时删除的节点
// 2、如果当前节点的左子节点不为空 并且左子节点就是要删除的节点 就将this.left=null;并且返回(结束递归删除)
if (this.left != null && this.left.no == no) {
this.left = null;
return;
}
// 3、如果当前节点的右子节点不为空 并且右子节点就是要删除的节点 就将this.right=null;并且返回(结束递归删除)
if (this.right != null && this.right.no == no) {
this.right = null;
return;
}
//4、如果2、3步没有删除节点 那么就需要向左子树进行递归删除
if (this.left != null) {
this.left.delNode(no);
}
//5、如果第4步也没有删除节点 则需要向右子树进行递归删除
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);//输出当前结点
}
}
顺序存储二叉树:
顺序存储二叉树的特点:
顺序二叉树通常只考虑完全二叉树
index : 表示二叉树中元素的下标(按0开始编号)
- 下标为index的元素的左子节点为 2 * index + 1
- 下标为index的元素的右子节点为 2 * index + 2
- 下标为index的元素的父节点为 (index-1) / 2
代码实现:
package com.datastructures.binarytree;
public class ArrBinaryTreeDemo {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7};
ArrBinaryTree arrBinaryTree = new ArrBinaryTree(arr);
arrBinaryTree.preOrder();//1 2 4 5 3 6 7
arrBinaryTree.infixOrder();//4 2 5 1 6 3 7
arrBinaryTree.postOrder();//4 5 2 6 7 3 1
}
}
//编写ArrBinaryTree 实现顺序存储二叉树
class ArrBinaryTree {
private int[] arr;
public ArrBinaryTree(int[] arr) {
this.arr = arr;
}
public void preOrder() {
this.preOrder(0);
}
public void infixOrder() {
this.infixOrder(0);
}
public void postOrder() {
this.postOrder(0);
}
//实现二叉树的前序遍历
//index:数组的下标
public void preOrder(int index) {
if (arr == null || arr.length == 0) {
System.out.println("数组为空 无法实现二叉树的前序遍历");
return;
}
//前序遍历 先输出当前节点
System.out.print(arr[index]);
//再递归左子树
if ((index * 2 + 1) < arr.length) {
preOrder(index * 2 + 1);
}
//再递归右子树
if ((index * 2 + 2) < arr.length) {
preOrder(index * 2 + 2);
}
}
//实现二叉树的中序遍历
//index:数组的下标
public void infixOrder(int index) {
if (arr == null || arr.length == 0) {
System.out.println("数组为空 无法实现二叉树的中序遍历");
return;
}
//中序遍历 先递归左子树
if ((index * 2 + 1) < arr.length) {
infixOrder(index * 2 + 1);
}
//输出当前节点
System.out.print(arr[index]);
//再递归右子树
if ((index * 2 + 2) < arr.length) {
infixOrder(index * 2 + 2);
}
}
//实现二叉树的后序遍历
//index:数组的下标
public void postOrder(int index) {
if (arr == null || arr.length == 0) {
System.out.println("数组为空 无法实现二叉树的后序遍历");
return;
}
//后序遍历 先递归左子树
if ((index * 2 + 1) < arr.length) {
postOrder(index * 2 + 1);
}
//再递归右子树
if ((index * 2 + 2) < arr.length) {
postOrder(index * 2 + 2);
}
//输出当前节点
System.out.print(arr[index]);
}
}
如果我们希望充分的利用 各个节点的左右指针, 让各个节点可以指向自己的前后节点,则考虑线索化二叉树。
线索化二叉树:
- n个结点的二叉链表中含有n+1 【公式 2n-(n-1)=n+1】个空指针域。利用二叉链表中的空指针域,存放指向该结点在某种遍历次序下的前驱和后继结点的指针(这种附加的指针称为"线索")。
- 这种加上了线索的二叉链表称为线索链表,相应的二叉树称为线索二叉树(ThreadedBinaryTree)。根据线索性质的不同,线索二叉树可分为前序线索二叉树、中序线索二叉树和后序线索二叉树三种。
- 一个结点的前一个结点,称为前驱结点;一个结点的后一个结点,称为后继结点
代码实现:
//定义能实现线索化的二叉树
class ThreadedBinaryTree {
private HeroNode root;
private HeroNode pre;//指向当前节点的前驱节点 递归过程中pre总是保留前一个节点
public void setRoot(HeroNode root) {
this.root = root;
}
//重载中序线索化
public void infixThreadedNodes() {
this.infixThreadedNodes(root);
}
//重载前序线索化
public void preThreadedNodes() {
this.preThreadedNodes(root);
}
//重载后序线索化
public void postThreadedNodes() {
this.postThreadedNodes(root);
}
//编写对二叉树进行中序线索化的方法
public void infixThreadedNodes(HeroNode node) {
if (node == null) {//节点为空 不能线索化
return;
}
//先线索化左子树
if (node.getLeftType() == 0) {
infixThreadedNodes(node.getLeft());
}
//线索化当前节点
//处理前驱节点
if (node.getLeft() == null) {
node.setLeft(pre);//如果当前节点的左指针为空则可以使用该空间存前驱节点 否则不能使用
node.setLeftType(1);//修改左指针的类型 说明存的是前驱节点
}
//处理后继节点
if (pre != null && pre.getRight() == null) {
pre.setRight(node);//让前驱节点的右指针指向当前节点 (画图更好理解)
pre.setRightType(1);//修改右指针的类型 说明存的是后继节点
}
//更新pre !!!
pre = node;//每处理一个节点后 让当前节点是下一个节点的前驱节点
//再线索化右子树
if (node.getRightType() == 0) {
infixThreadedNodes(node.getRight());
}
}
//编写对二叉树进行前序线索化的方法
public void preThreadedNodes(HeroNode node) {
if (node == null) {
return;
}
//线索化当前节点
//处理前驱节点 左指针为空 则将左指针指向前驱节点
if (node.getLeft() == null) {
node.setLeft(pre);
node.setLeftType(1);
}
//处理后继节点 前一个节点的后继节点指向当前节点
if (pre != null && pre.getRight() == null) {
pre.setRight(node);
pre.setRightType(1);
}
//更新pre
pre = node;
//线索化左子树
if (node.getLeftType() == 0) {
preThreadedNodes(node.getLeft());
}
//线索化右子树
if (node.getRightType() == 0) {
preThreadedNodes(node.getRight());
}
}
//编写对二叉树进行后序线索化的方法
public void postThreadedNodes(HeroNode node) {
if (node == null) {
return;
}
//线索化左子树
if (node.getLeftType() == 0) {
postThreadedNodes(node.getLeft());
}
//线索化右子树
if (node.getRightType() == 0) {
postThreadedNodes(node.getRight());
}
//线索化当前节点
if (node.getLeft() == null) {
node.setLeft(pre);
node.setLeftType(1);
}
if (pre != null && pre.getRight() == null) {
pre.setRight(node);
pre.setRightType(1);
}
//更新pre
pre = node;
}
}
遍历线索化二叉树:因为线索化后,各个结点指向有变化,因此原来的遍历方式不能使用,这时需要使用新的方式遍历线索化二叉树,各个节点可以通过线型方式遍历,因此无需使用递归方式,这样也提高了遍历的效率。
遍历中序线索化二叉树的代码:
//在ThreadedBinaryTree 类
//遍历中序线索化二叉树的方法
public void infixThreadedList() {
//定义一个变量存储当前遍历的节点 从root开始
HeroNode 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);
}//退出while时说明已经到了一个右指针不指向后继节点的节点
//替换这个遍历的节点
node = node.getRight();
}
}
遍历前序线索化二叉树的代码:
//在ThreadedBinaryTree 类
//遍历前序线索化二叉树的方法
public void preThreadedList() {
HeroNode node = root;
while (node != null) {
System.out.println(node);
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();
}
}
遍历后序线索化二叉树的代码:(待定)