1、为什么需要树这种数据结构?
- 数组存储方式分析
- 优点:通过
下标的方式访问元素
,速度快。对于有序数组,还可以使用二分查找
提高检索速度。 - 缺点:如果要检索具体某一个值,或者
插入值(按一定顺序)会整体移动
,效率极低 【示意图】
- 优点:通过
- 链式存储方式分析
- 优点:在一定程度上对数组存储方式有优化,如:
插入
一个数值节点,只需要将插入节点,链接到链表中即可,删除效率
也很好 - 缺点:在进行
检索
时,效率仍然较低,比如(检索某一个值,需要从头结点开始遍历) 【示意图】
- 优点:在一定程度上对数组存储方式有优化,如:
- 树存储方式分析
能提高数据存储
,读取
的效率,比如利用二叉排序树
,既可以保证数据的检索速度,同时也能保证数据的插入、删除、修改
的速度 【示意图】
- 树示意图
树的常用术语
- 节点
- 根节点
- 父节点
- 子节点
- 叶子节点(没有子节点的节点)
- 节点的权(节点值)
- 路径(从 root 节点到该节点的路线)
- 层
- 子树
- 树的高度(最大层数)
- 森林:多棵子树构成森林
2、二叉树的概念
-
二叉树的概念
-
树有很多种,每个节点
最多只能有两个子节点
的一种形式,称之为二叉树 -
二叉树的子节点分为左节点和右节点
-
示意图
-
如果该二叉树的所有
叶子节点
都在最后一层,并且节点总数= 2^n - 1 ,n 为层数,则我们称之为满二叉树
-
如果该二叉树的所有
叶子节点
都在最后一层或者倒数第二层,而且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续,我们称之为完全二叉树
-
3、 二叉树的遍历说明
使用前序、中序和后序
进行二叉树的遍历
1. 前序遍历:先输入父节点,再遍历左子树和右子树
2. 中序遍历:先遍历左子树,再输出父节点,在遍历右子树
3. 后序遍历:先遍历左子树,再遍历右子树,再输出父节点
💻 代码实现
package com.yao.tree;
/**
* @Author: Yao
* @Date: 2021/10/19 11:30
*/
public class BinaryTreeErgodic {
public static void main(String[] args) {
//定义二叉树
BinaryTree binaryTree = new BinaryTree();
HeroNode root = new HeroNode(1, "宋江");
HeroNode heroNode2 = new HeroNode(2, "吴用");
HeroNode heroNode3 = new HeroNode(3, "卢俊义");
HeroNode heroNode4 = new HeroNode(4, "林冲");
HeroNode heroNode5 = new HeroNode(5, "关胜");
binaryTree.setRoot(root);
root.setLeft(heroNode2);
root.setRight(heroNode3);
heroNode3.setLeft(heroNode5);
heroNode3.setRight(heroNode4);
System.out.println("先序遍历..");
binaryTree.preOrder();
System.out.println("中序遍历..");
binaryTree.midOrder();
System.out.println("后序遍历..");
binaryTree.postOrder();
}
}
//创建一个 BinaryTree 二叉树
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 midOrder(){
if (this.root != null){
this.root.midOrder();
}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 midOrder(){
//如果当前节点的左子树不为空,递归中序遍历
if (this.left != null){
this.left.midOrder();
}
//输出当前节点
System.out.println(this);
//如果当前节点的右子树不为空,递归中序遍历
if(this.right != null){
this.right.midOrder();
}
}
/**
* 二叉树的后序遍历
*/
public void postOrder(){
//如果当前节点的左子树不为空,递归后序遍历
if (this.left != null){
this.left.postOrder();
}
//如果当前节点的右子树不为空,递归后序遍历
if (this.right != null){
this.right.postOrder();
}
//输出当前节点
System.out.println(this);
}
}
4、 二叉树查找指定节点
- 编写前序查找、中序查找、后序查找的方法
- 分别使用三种查找方式,查找 heroNo = 5 的节点
- 分析三种查找方式
- 思路分析图解
💻 代码实现
package com.yao.tree;
/**
* @Author: Yao
* @Date: 2021/10/20 9:48
*/
public class BinaryTreeSearch {
public static void main(String[] args) {
BinaryTree2 binaryTree2 = new BinaryTree2();
HeroNode2 root = new HeroNode2(1, "宋江");
HeroNode2 heroNode2 = new HeroNode2(2, "吴用");
HeroNode2 heroNode3 = new HeroNode2(3, "卢俊义");
HeroNode2 heroNode4 = new HeroNode2(4, "林冲");
HeroNode2 heroNode5 = new HeroNode2(5, "关胜");
binaryTree2.setRoot(root);
root.setLeft(heroNode2);
root.setRight(heroNode3);
heroNode3.setLeft(heroNode5);
heroNode3.setRight(heroNode4);
//先序遍历查找 no=5
System.out.println(binaryTree2.preSearch(5));
//中序遍历查找 no=5
System.out.println(binaryTree2.midSearch(5));
//后序遍历查找 no=5
System.out.println(binaryTree2.postSearch(5));
}
}
//创建一颗二叉树
class BinaryTree2{
private HeroNode2 root;
public void setRoot(HeroNode2 root) {
this.root = root;
}
//二叉树前序查找
public HeroNode2 preSearch(int no){
if (this.root != null){
return this.root.preOrderSearch(no);
}else{
System.out.println("该二叉树为空.....");
return null;
}
}
//二叉树中序查找
public HeroNode2 midSearch(int no){
if (this.root != null){
return this.root.midOrderSearch(no);
}else{
System.out.println("该二叉树为空.....");
return null;
}
}
//二叉树后序查找
public HeroNode2 postSearch(int no){
if (this.root != null){
return this.root.postOrderSearch(no);
}else{
System.out.println("该二叉树为空.....");
return null;
}
}
}
//创建二叉树的节点
class HeroNode2{
private int no;
private String name;
private HeroNode2 left;
private HeroNode2 right;
public HeroNode2(int no, String name){
this.no = no;
this.name = name;
}
public void setNo(int no) {
this.no = no;
}
public void setName(String name) {
this.name = name;
}
public void setLeft(HeroNode2 left) {
this.left = left;
}
public void setRight(HeroNode2 right) {
this.right = right;
}
public int getNo() {
return no;
}
public String getName() {
return name;
}
public HeroNode2 getLeft() {
return left;
}
public HeroNode2 getRight() {
return right;
}
@Override
public String toString() {
return "HeroNode2{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
//二叉树的前序查找
public HeroNode2 preOrderSearch(int no){
HeroNode2 resNode = null;
//判断当前节点是否是要查找的节点,如果是就返回
System.out.println("进入先序查找遍历....");
if (this.no == no){
return this;
}
//如果不是,就判断当前节点的左子树是否为空,不为空就进行先序递归,如果找到就返回
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 HeroNode2 midOrderSearch(int no){
HeroNode2 resNode = null;
//先判断当前节点的左子树是否为空,如果不为空,就进行中序递归,找到就返回
if (this.left != null){
resNode = this.left.midOrderSearch(no);
}
if (resNode != null){
return resNode;
}
//当前节点的左子树没有找到,就与当前节点进行比较,找到就返回
System.out.println("进入中序查找遍历....");
if (this.no == no){
return this;
}
//当前节点没有找到,就判断当前节点的右子树是否为空,不为空就进行中序递归,找到就返回
if (this.right != null){
resNode = this.right.midOrderSearch(no);
}
return resNode;
}
//二叉树的后序查找
public HeroNode2 postOrderSearch(int no){
HeroNode2 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;
}
}
5、 二叉树删除节点
- 如果删除的节点是叶子节点,则删除该节点
- 如果删除的节点是非叶子节点,则删除该子树.
- 删除思路分析
💻 代码实现
package com.yao.tree;
/**
* @Author: Yao
* @Date: 2021/10/19 11:30
*/
public class BinaryTreeErgodic {
public static void main(String[] args) {
//定义二叉树
BinaryTree binaryTree = new BinaryTree();
HeroNode root = new HeroNode(1, "宋江");
HeroNode heroNode2 = new HeroNode(2, "吴用");
HeroNode heroNode3 = new HeroNode(3, "卢俊义");
HeroNode heroNode4 = new HeroNode(4, "林冲");
HeroNode heroNode5 = new HeroNode(5, "关胜");
binaryTree.setRoot(root);
root.setLeft(heroNode2);
root.setRight(heroNode3);
heroNode3.setLeft(heroNode5);
heroNode3.setRight(heroNode4);
System.out.println("先序遍历..");
binaryTree.preOrder();
System.out.println("中序遍历..");
binaryTree.midOrder();
System.out.println("后序遍历..");
binaryTree.postOrder();
System.out.println("------------------------");
System.out.println("先序遍历..");
binaryTree.preOrder();
binaryTree.delNode(3);
System.out.println("先序遍历..");
binaryTree.preOrder();
}
}
//创建一个 BinaryTree 二叉树
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 midOrder(){
if (this.root != null){
this.root.midOrder();
}else{
System.out.println("二叉树为空,无法遍历...");
}
}
//实现后序遍历
public void postOrder(){
if (this.root != null){
this.root.postOrder();
}else {
System.out.println("二叉树为空,无法遍历...");
}
}
public void delNode(int no){
//如果根节点不为空,并且就是要删除的节点,就直接删除
if (this.root != null && this.root.getNo() == no){
root = null;
return;
}else if(this.root != null){ //如果根节点不为空,并且不是要删除的节点,就进行判断左右子树
this.root.delNode(no);
}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 midOrder(){
//如果当前节点的左子树不为空,递归中序遍历
if (this.left != null){
this.left.midOrder();
}
//输出当前节点
System.out.println(this);
//如果当前节点的右子树不为空,递归中序遍历
if(this.right != null){
this.right.midOrder();
}
}
/**
* 二叉树的后序遍历
*/
public void postOrder(){
//如果当前节点的左子树不为空,递归后序遍历
if (this.left != null){
this.left.postOrder();
}
//如果当前节点的右子树不为空,递归后序遍历
if (this.right != null){
this.right.postOrder();
}
//输出当前节点
System.out.println(this);
}
//二叉树节点删除
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);
}
}
}
🎃 完结