一:二叉树
1:二叉树为什么会出现
数组的特点:查找快,增删慢
链表的特点:查找慢,增删快
二叉树集合的链表和数组的优点,既查找很快,而且增删快。
2:二叉树的常用术语
- 树的常用术语(结合示意图理解):
- 节点
- 根节点
- 父节点
- 子节点
- 叶子节点 (没有子节点的节点)
- 节点的权(节点值)
- 路径(从 root 节点找到该节点的路线)
- 层
- 子树
- 树的高度(最大层数)
- 森林 :多颗子树构成森林
3:二叉树的概念
- 树有很多种,每个节点最多只能有两个子节点的一种形式称为二叉树。
- 二叉树的子节点分为左节点和右节点
- 一棵二叉树的结点要么是叶子结点,要么它有两个子结点(如果一个二叉树的层数为K,且结点总数是(2^k) -1,则它就是满二叉树。
- 若设二叉树的深度为k,除第 k 层外,其它各层 (1~k-1) 的结点数都达到最大个数,第k 层所有的结点都连续集中在最左边,这就是完全二叉树。
4:二叉树的遍历与搜索
二叉树的遍历分为前序遍历,中序遍历,后序遍历。
主要的判断依据就是根节点的输出顺序。
代码实现如下:
TreeNode实体类
package com.github.tree;
/**
* @author lizhangyu
* @version 1.0
* @description
* @date 2021/3/11 14:23
*/
public class TreeNode {
private int no;
private TreeNode left;
private TreeNode right;
public TreeNode(int no) {
this.no = no;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public TreeNode getLeft() {
return left;
}
public void setLeft(TreeNode left) {
this.left = left;
}
public TreeNode getRight() {
return right;
}
public void setRight(TreeNode right) {
this.right = right;
}
@Override
public String toString() {
return "TreeNode{" +
"no=" + no +
'}';
}
/**
* 前序遍历
* 根-左-右
*/
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);
}
/**
* 前序遍历查找
* @param no
* @return
*/
public TreeNode preOrderSearch(int no) {
if (this.no == no) {
return this;
}
TreeNode 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;
}
/**
* 中序遍历查找
* @param no
* @return
*/
public TreeNode midOrderSearch(int no) {
TreeNode resNode = null;
if (this.left != null) {
resNode = this.left.midOrderSearch(no);
}
if (resNode != null) {
return resNode;
}
if (this.no == no) {
return this;
}
if (this.right != null) {
resNode = this.right.midOrderSearch(no);
}
return resNode;
}
/**
* 后序遍历查找
* @param no
* @return
*/
public TreeNode postOrderSearch(int no) {
TreeNode 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;
}
if (this.no == no) {
return this;
}
return resNode;
}
}
BinaryTree实体类
package com.github.tree;
/**
* @author lizhangyu
* @version 1.0
* @description
* @date 2021/3/11 14:36
*/
public class BinaryTree {
private TreeNode root;
public void setRoot(TreeNode 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("二叉树为空,无法遍历");
}
}
/**
* 前序遍历查找
* @param no
* @return
*/
public TreeNode preOrderSearch(int no) {
if (this.root != null) {
return this.root.preOrderSearch(no);
}else {
return null;
}
}
/**
* 中序遍历查找
* @param no
* @return
*/
public TreeNode midOrderSearch(int no) {
if (this.root != null) {
return this.root.midOrderSearch(no);
}else {
return null;
}
}
/**
* 后序遍历查找
* @param no
* @return
*/
public TreeNode postOrderSearch(int no) {
if (this.root != null) {
return this.root.postOrderSearch(no);
}else {
return null;
}
}
}
BinaryTreeDemo实体类
package com.github.tree;
/**
* @author lizhangyu
* @version 1.0
* @description
* @date 2021/3/11 14:52
*/
public class BinaryTreeDemo {
public static void main(String[] args) {
//1. 创建节点
TreeNode root = new TreeNode(1);
TreeNode node1 = new TreeNode(2);
TreeNode node2 = new TreeNode(3);
TreeNode node3 = new TreeNode(4);
TreeNode node4 = new TreeNode(5);
//2. 构建二叉树
root.setLeft(node1);
root.setRight(node2);
node1.setRight(node3);
node2.setLeft(node4);
BinaryTree binaryTree = new BinaryTree();
binaryTree.setRoot(root);
System.out.println(root.getRight().getLeft());
//3. 树的遍历
//3.1 树的前序遍历
System.out.println("树的前序遍历:");
binaryTree.preOrder();
//3.2 树的中序遍历
System.out.println("树的中序遍历:");
binaryTree.midOrder();
//3.3 树的后序遍历
System.out.println("树的后序遍历:");
binaryTree.postOrder();
//4. 树中指定节点值的查找
//4.1 树的前序遍历查找
TreeNode res = binaryTree.preOrderSearch(1);
if (res != null) {
System.out.println("树的前序遍历找到指定的值" + res.getNo());
}else {
System.out.println("树的前序遍历没有找到指定的值");
}
//4.2 树的中序遍历查找
res = binaryTree.midOrderSearch(4);
if (res != null) {
System.out.println("树的中序遍历找到指定的值" + res.getNo());
}else {
System.out.println("树的中序遍历没有找到指定的值");
}
//4.3 树的后序遍历查找
res = binaryTree.postOrderSearch(7);
if (res != null) {
System.out.println("树的后序遍历找到指定的值" + res.getNo());
}else {
System.out.println("树的后序遍历没有找到指定的值");
}
}
}
运行结果如下:
树的前序遍历:
TreeNode{no=1}
TreeNode{no=2}
TreeNode{no=4}
TreeNode{no=3}
TreeNode{no=5}
树的中序遍历:
TreeNode{no=2}
TreeNode{no=4}
TreeNode{no=1}
TreeNode{no=5}
TreeNode{no=3}
树的后序遍历:
TreeNode{no=4}
TreeNode{no=2}
TreeNode{no=5}
TreeNode{no=3}
TreeNode{no=1}
树的前序遍历找到指定的值1
树的中序遍历找到指定的值4
树的后序遍历没有找到指定的值