二叉树
介绍
为什么需要树这种数据结构?
数组存储方式、链式存储方式和树存储方式的对比:
- 数组存储方式:
优点 | 缺点 |
---|---|
通过下标方式访问元素,速度快。对于有序数组,还可使用二分查找提高检索速度。 | 如果要检索具体某个值,或者插入值(按一定顺序)会整体移动,效率较低。 |
- 链式存储方式:
优点 | 缺点 |
---|---|
在一定程度上对数组存储方式有优化(比如:插入一个数值节点,只需要将插入节点,链接到链表中即可, 删除效率也很好)。 | 在进行检索时,效率仍然较低,比如(检索某个值,需要从头节点开始遍历) |
- 树存储方式:
特点 |
---|
能提高数据存储,读取的效率, 比如利用 二叉排序树(Binary Sort Tree),既可以保证数据的检索速度,同时也可以保证数据的插入,删除,修改的速度。 |
树的一些基本术语
二叉树
树有很多种,每个节点最多只能有两个子节点的一种形式称为二叉树,二叉树的子节点分为左节点和右节点。
满二叉树和完全二叉树
满二叉树:如果该二叉树的所有叶子节点都在最后一层,并且结点总数= 2^n -1 , n为层数,则我们称为满二叉树。
完全二叉树:如果该二叉树的所有叶子节点都在最后一层或者倒数第二层,而且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续,我们称为完全二叉树。
二叉树的遍历
二叉树的遍历分为前序,中序和后序遍历三种,下面分别使用这三种遍历方式来对一下二叉树进行遍历:
节点类(提供get和set方法方便获取和设置子节点):
class Node {
private int no;
private String name;
private Node left;
private Node right;
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 "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
}
提供一个BinaryTree类来装Node,并用来指定root节点:
class BinaryTree {
private Node root;
public void setRoot(Node root) {
this.root = root;
}
}
BinaryTreeDemo类用来造树并测试:
public class BinaryTreeDemo {
public static void main(String[] args) {
BinaryTree binaryTree = new BinaryTree();
// 造节点
Node root = new Node(1, "北京");
Node node2 = new Node(2, "上海");
Node node3 = new Node(3, "深圳");
Node node4 = new Node(4, "广州");
Node node5 = new Node(5, "珠海");
Node node6 = new Node(6, "澳门");
// 设置节点间的关系
root.setLeft(node2);
node2.setRight(node5);
root.setRight(node3);
node3.setRight(node4);
node3.setLeft(node6);
// 设置根节点
binaryTree.setRoot(root);
}
前序遍历
先输出当前节点,再递归当前节点的左子树和右子树进行前序遍历(递归太多了有点绕,但是不难,建议搭配debug食用加深理解)。
代码实现
// 前序遍历
public void preOrder() {
// 先输出当前节点
System.out.println(this.toString());
// 如果当前节点的左子树不为空
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.toString());
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.toString());
}
结果
二叉树的查找
二叉树的查找同样分为三种,分别为前序查找,中序查找和后序查找。
前序查找
思路
- 给方法传入一个待查找的编号 。
- 如果当前节点的编号是要查找的编号,返回当前节点。
- 如果不是,先新建一个节点resNode用于保存递归查找返回的节点,然后判断当前节点的左子节点是否为空,不为空则向左递归查找。
- 如果找到了就返回这个节点,没有找到那么resNode还是null,则进行下一步。
- 判断当前节点的右子节点是否为空,不为空则向右递归查找。
- 不管找没找到,都返回resNode,只需最后在外面判断返回值是否为空即可。
代码实现
public Node preOrderSearch(int no) {
// 如果当前节点的编号是要查找的编号,返回当前节点
if (this.no == no) {
return this;
}
// 新建一个节点resNode用于保存递归查找返回的节点
Node resNode = null;
// 如果不是,就判断当前节点的左子节点是否为空,不为空则向左递归查找
if (this.left != null) {
resNode = this.left.preOrderSearch(no);
}
// 如果找到了就返回这个节点,没有找到那么resNode还是null,则进行下一步
if (resNode != null) {
return resNode;
}
// 判断当前节点的右子节点是否为空,不为空则向右递归查找
if (this.right != null) {
resNode = this.right.preOrderSearch(no);
}
// 不管找没找到,都返回resNode,只需最后在外面判断返回值是否为空即可
return resNode;
}
中序查找
思路
思路与前序查找类似,就调整一下语句顺序。
代码实现
public Node infixOrderSearch(int no) {
Node resNode = null;
if (this.left != null) {
resNode = this.left.infixOrderSearch(no);
}
if (resNode != null) {
return resNode;
}
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.infixOrderSearch(no);
}
if (resNode != null) {
return resNode;
}
if (this.right != null) {
resNode = this.right.infixOrderSearch(no);
}
if (resNode != null) {
return resNode;
}
if (this.no == no) {
return this;
}
return resNode;
}
结果测试
在main方法中写:
System.out.println("前序遍历查找");
Node heroNode = binaryTree.preOrderSearch(5);
if (heroNode != null) {
System.out.println("找到了:" + heroNode.toString());
} else {
System.out.println("没有找到该节点");
}
结果:
二叉树的删除
目前阶段先规定:
如果要删除的节点是叶子节点,就删除该节点。
如果要删除的节点是非叶子节点,就删除该子树。
思路
- 因为二叉树是单向的,所以我们该判断当前节点的子节点是否需要删除,而不是判断当前节点是否需要删除。
- 先判断是不是空树或者是不是只有一个根节点,只有一个根节点的话就等价于把二叉树置为空。
- 判断当前节点的左子节点是否不为空且是要删除的节点,是就删除(this.left == null),并且返回(结束递归),如果不是则进行下一步。
- 判断当前节点的右子节点是否不为空且是要删除的节点,是就删除(this.right== null),并且返回(结束递归),如果不是则进行下一步。
- 如果第3和4步骤都没有删除节点,则需要向当前节点的左子树进行递归删除。
- 如果第5步骤没有删除节点,则需要向当前节点的右子树进行递归删除。
代码实现
public void OrderDelete(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.OrderDelete(no);
}
if (this.right!= null){
this.right.OrderDelete(no);
}
}
结果测试
在main方法中写:
System.out.println("删除前:");
binaryTree.preOrder();
System.out.println("删除后:");
// 删除2号节点
binaryTree.OrderDelete(2);
binaryTree.preOrder();
结果:
可以看到2号节点上海及其5号子节点珠海都被删除,符合预期。
完整代码
public class BinaryTreeDemo {
public static void main(String[] args) {
BinaryTree binaryTree = new BinaryTree();
Node root = new Node(1, "北京");
Node node2 = new Node(2, "上海");
Node node3 = new Node(3, "深圳");
Node node4 = new Node(4, "广州");
Node node5 = new Node(5, "珠海");
Node node6 = new Node(6, "澳门");
root.setLeft(node2);
node2.setRight(node5);
root.setRight(node3);
node3.setRight(node4);
node3.setLeft(node6);
// 设置根节点
binaryTree.setRoot(root);
// 删除测试
System.out.println("删除前:");
binaryTree.preOrder();
System.out.println("删除后:");
binaryTree.OrderDelete(2);
binaryTree.preOrder();
// 查找测试
System.out.println("前序遍历查找");
Node heroNode = binaryTree.preOrderSearch(5);
if (heroNode != null) {
System.out.println("找到了:" + heroNode.toString());
} else {
System.out.println("没有找到该节点");
}
binaryTree.preOrder();
binaryTree.OrderDelete(3);
System.out.println("__________________");
binaryTree.preOrder();
// 遍历测试
System.out.println("前序遍历:"); //1,2,5,3,6,4
binaryTree.preOrder();
System.out.println("中序遍历:"); //2,5,1,6,3,4
binaryTree.infixOrder();
System.out.println("后序遍历:"); // 5,2,6,4,3,1
binaryTree.postOrder();
}
}
class BinaryTree {
private Node root;
public void setRoot(Node 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 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 OrderDelete(int no) {
if (root != null) {
if (root.getNo() == no) {
root = null;
} else {
root.OrderDelete(no);
}
} else {
System.out.println("这是一颗空树");
}
}
}
class Node {
private int no;
private String name;
private Node left;
private Node right;
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 "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
public void OrderDelete(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.OrderDelete(no);
}
if (this.right != null) {
this.right.OrderDelete(no);
}
}
public Node preOrderSearch(int no) {
// 如果当前节点的编号是要查找的编号,返回当前节点
if (this.no == no) {
return this;
}
// 新建一个节点resNode用于保存递归查找返回的节点
Node resNode = null;
// 如果不是,就判断当前节点的左子节点是否为空,不为空则向左递归查找
if (this.left != null) {
resNode = this.left.preOrderSearch(no);
}
// 如果找到了就返回这个节点,没有找到那么resNode还是null,则进行下一步
if (resNode != null) {
return resNode;
}
// 判断当前节点的右子节点是否为空,不为空则向右递归查找
if (this.right != null) {
resNode = this.right.preOrderSearch(no);
}
// 不管找没找到,都返回resNode,只需最后在外面判断返回值是否为空即可
return resNode;
}
public Node infixOrderSearch(int no) {
Node resNode = null;
if (this.left != null) {
resNode = this.left.infixOrderSearch(no);
}
if (resNode != null) {
return resNode;
}
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.infixOrderSearch(no);
}
if (resNode != null) {
return resNode;
}
if (this.right != null) {
resNode = this.right.infixOrderSearch(no);
}
if (resNode != null) {
return resNode;
}
if (this.no == no) {
return this;
}
return resNode;
}
public void preOrder() {
System.out.println(this.toString());
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.toString());
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.toString());
}
}