为什么需要树这种数据结构
数组存储方式的分析优点:通过下标方式访问元素,速度快。对于有序数组,还可使用二分查找提高检索速度。缺点:如果要检索具体某个值,或者插入值(按一定顺序)会整体移动,效率较低
链式存储方式的分析优点:在一定程度上对数组存储方式有优化(比如:插入一个数值节点,只需要将插入节点,链接到链表中即可, 删除效率也很好)。缺点:在进行检索时,效率仍然较低,比如(检索某个值,需要从头节点开始遍历)
树存储方式的分析�能提高数据存储,读取的效率, 比如利用 二叉排序树(Binary Sort Tree),既可以保证数据的检索速度,同时也可以保证数据的插入,删除,修改的速度。案例: [7, 3, 10, 1, 5, 9, 12]
树的常用术语:
1.节点
2.根节点
3.父节点
4.子节点
5.叶子节点 (没有子节点的节点)
6.节点的权(节点值)
7.路径(从root节点找到该节点的路线)
8.层
9.子树
10.树的高度(最大层数)
11.森林 :多颗子树构成森林
二叉树的概念
树有很多种,每个节点最多只能有两个子节点的一种形式称为二叉树。
二叉树的子节点分为左节点和右节点。
如果该二叉树的所有叶子节点都在最后一层,并且结点总数= 2^n -1 , n 为层数,则我们称为满二叉树。
如果该二叉树的所有叶子节点都在最后一层或者倒数第二层,而且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续,我们称为完全二叉树。
重点讲解一下二叉树的前序遍历,中序遍历和后序遍历。
二叉树遍历的说明
使用前序,中序和后序对下面的二叉树进行遍历
前序遍历: 先输出父节点,再遍历左子树和右子树
中序遍历: 先遍历左子树,再输出父节点,再遍历右子树
后序遍历: 先遍历左子树,再遍历右子树,最后输出父节点
小结: 看输出父节点的顺序,就确定是前序,中序还是后序
代码实现
package cn.icanci.datastructure.tree;
/**
* @Author: icanci
* @ProjectName: AlgorithmAndDataStructure
* @PackageName: cn.icanci.datastructure.tree
* @Date: Created in 2020/3/11 9:21
* @ClassAction: 二叉树
*/
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, "林冲");
//说明 先手动创建二叉树 后面递归创建
root.setLeft(node2);
root.setRight(node3);
node3.setRight(node4);
binaryTree.setRoot(root);
//测试 1 2 3 4
System.out.println("前序");
binaryTree.preOrder();
//测试 2 1 3 4
System.out.println("中序");
binaryTree.infixOrder();
//测试 2 4 3 1
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 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);
}
}
测试
前序
HeroNode{no=1, name='宋江'}
HeroNode{no=2, name='吴用'}
HeroNode{no=3, name='卢俊义'}
HeroNode{no=4, name='林冲'}
中序
HeroNode{no=2, name='吴用'}
HeroNode{no=1, name='宋江'}
HeroNode{no=3, name='卢俊义'}
HeroNode{no=4, name='林冲'}
后序
HeroNode{no=2, name='吴用'}
HeroNode{no=4, name='林冲'}
HeroNode{no=3, name='卢俊义'}
HeroNode{no=1, name='宋江'}
二叉树-查找指定节点
要求
请编写前序查找,中序查找和后序查找的方法。
并分别使用三种查找方式,查找 heroNO = 5 的节点
并分析各种查找方式,分别比较了多少次
代码实现
package cn.icanci.datastructure.tree;
/**
* @Author: icanci
* @ProjectName: AlgorithmAndDataStructure
* @PackageName: cn.icanci.datastructure.tree
* @Date: Created in 2020/3/11 9:21
* @ClassAction: 二叉树
*/
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, "林冲");
//说明 先手动创建二叉树 后面递归创建
root.setLeft(node2);
root.setRight(node3);
node3.setRight(node4);
binaryTree.setRoot(root);
//测试 1 2 3 4
System.out.println("前序");
binaryTree.preOrder();
//测试 2 1 3 4
System.out.println("中序");
binaryTree.infixOrder();
//测试 2 4 3 1
System.out.println("后序");
binaryTree.postOrder();
System.out.println("前序查找");
System.out.println(binaryTree.preOrderSearch(2));
System.out.println("中序查找");
System.out.println(binaryTree.infixOrderSearch(2));
System.out.println("后续查找");
System.out.println(binaryTree.postOrderSearch(2));
}
}
/**
* 定义二叉树
*/
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);
}
/**
* 前序遍历查找
*
* @param no 需要查找的编号
* @return 找到返回 HeroNode 没有找到就返回null
*/
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 = resNode.right.preOrderSearch(no);
}
return resNode;
}
/**
* 中序遍历查找
*
* @param no 需要查找的编号
* @return 找到返回 HeroNode 没有找到就返回null
*/
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;
}
/**
* 后序遍历查找
*
* @param no 需要查找的编号
* @return 找到返回 HeroNode 没有找到就返回null
*/
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);
}
System.out.println("后序次数");
if (this.no == no) {
return this;
}
return resNode;
}
}
测试
前序
HeroNode{no=1, name='宋江'}
HeroNode{no=2, name='吴用'}
HeroNode{no=3, name='卢俊义'}
HeroNode{no=4, name='林冲'}
中序
HeroNode{no=2, name='吴用'}
HeroNode{no=1, name='宋江'}
HeroNode{no=3, name='卢俊义'}
HeroNode{no=4, name='林冲'}
后序
HeroNode{no=2, name='吴用'}
HeroNode{no=4, name='林冲'}
HeroNode{no=3, name='卢俊义'}
HeroNode{no=1, name='宋江'}
前序查找
前序次数
前序次数
HeroNode{no=2, name='吴用'}
中序查找
中序次数
HeroNode{no=2, name='吴用'}
后续查找
后序次数
HeroNode{no=2, name='吴用'}
二叉树-删除节点
要求
如果删除的节点是叶子节点,则删除该节点
如果删除的节点是非叶子节点,则删除该子树.
测试,删除掉 5号叶子节点 和 3号子树.
代码
package cn.icanci.datastructure.tree;
/**
* @Author: icanci
* @ProjectName: AlgorithmAndDataStructure
* @PackageName: cn.icanci.datastructure.tree
* @Date: Created in 2020/3/11 9:21
* @ClassAction: 二叉树
*/
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, "林冲");
//说明 先手动创建二叉树 后面递归创建
root.setLeft(node2);
root.setRight(node3);
node3.setRight(node4);
binaryTree.setRoot(root);
//测试 1 2 3 4
System.out.println("前序");
binaryTree.preOrder();
//测试 2 1 3 4
// System.out.println("中序");
// binaryTree.infixOrder();
//测试 2 4 3 1
// System.out.println("后序");
// binaryTree.postOrder();
// System.out.println("前序查找");
// System.out.println(binaryTree.preOrderSearch(2));
// System.out.println("中序查找");
// System.out.println(binaryTree.infixOrderSearch(2));
// System.out.println("后续查找");
// System.out.println(binaryTree.postOrderSearch(2));
//删除节点
binaryTree.deleteNode(2);
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;
}
}
/**
* 删除节点
*
* @param no 需要删除的节点
*/
public void deleteNode(int no) {
if (root != null) {
if (root.getNo() == no) {
root = null;
} else {
root.deleteNode(no);
}
} else {
root.deleteNode(no);
}
}
}
/**
* 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 + '\'' +
'}';
}
/**
* 删除节点
*
* @param no 需要删除的节点
*/
public void deleteNode(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.deleteNode(no);
}
if (this.right != null) {
this.right.deleteNode(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);
}
/**
* 前序遍历查找
*
* @param no 需要查找的编号
* @return 找到返回 HeroNode 没有找到就返回null
*/
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 = resNode.right.preOrderSearch(no);
}
return resNode;
}
/**
* 中序遍历查找
*
* @param no 需要查找的编号
* @return 找到返回 HeroNode 没有找到就返回null
*/
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;
}
/**
* 后序遍历查找
*
* @param no 需要查找的编号
* @return 找到返回 HeroNode 没有找到就返回null
*/
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);
}
System.out.println("后序次数");
if (this.no == no) {
return this;
}
return resNode;
}
}
测试结果
前序
HeroNode{no=1, name='宋江'}
HeroNode{no=2, name='吴用'}
HeroNode{no=3, name='卢俊义'}
HeroNode{no=4, name='林冲'}
删除之后
HeroNode{no=1, name='宋江'}
HeroNode{no=3, name='卢俊义'}
HeroNode{no=4, name='林冲'}