一、什么是二叉树
一个节点有左节点和右节点,且最多有两个节点,个人画图如下
满二叉树:树的节点个数为2^n-1(n为树的层)
完全二叉树:最后一层左边叶子节点并且是连续的,倒数第二层从左到也是连续的。
二、二叉树的前中后序遍历
2.1 前序遍历
/**
* 前序遍历
* 先遍历根节点,然后遍历左节点,最后右节点
*/
public void preOrder() {
System.out.println(this);
if (this.left != null) {
this.left.preOrder();
}
if (this.right != null) {
this.right.preOrder();
}
}
2.2 中序遍历
/**
* 中序遍历
*/
public void infixOrder() {
if (this.left != null) {
this.left.infixOrder();
}
System.out.println(this);
if (this.right != null) {
this.right.infixOrder();
}
}
2.3 后序遍历
/**
* 后序遍历
*/
public void postOrder() {
if (this.left != null) {
this.left.postOrder();
}
if (this.right != null) {
this.right.postOrder();
}
System.out.println(this);
}
2.4 先序遍历查找
/**
* 先序遍历查找
* @param id 你要查找的id
*/
public Node preOrderSearch(int id) {
System.out.println("遍历查找的次数"); // 用于输出遍历了多少次
Node resNode = null; // 辅助变量,用于记录遍历的结果
// 如果当前节点为需要查找的id,则赋值resNode
if (this.id == id) {
return this;
}
// 左子节点遍历
if (this.left != null) {
resNode = this.left.preOrderSearch(id);
}
// 如果辅助变量不为空,则表示已经找到了此节点,则返回
if (resNode != null) {
return resNode;
}
// 右子节点遍历
if (this.right != null) {
resNode = this.right.preOrderSearch(id);
}
return resNode;
}
2.5 中序遍历查找
/**
* 中序遍历查找
* @param id 你要查找的id
* @return
*/
public Node infixOrderSearch(int id) {
Node resNode = null;
if (this.left != null) {
resNode = this.left.infixOrderSearch(id);
}
System.out.println("遍历查找的次数"); // 用于输出遍历了多少次
if (this.id == id) {
resNode = this;
}
if (resNode != null) {
return resNode;
}
if (this.right != null) {
resNode = this.right.infixOrderSearch(id);
}
return resNode;
}
2.6 后序遍历查找
/**
* 后序遍历查找
* @param id 你要查找的id
* @return
*/
public Node postOrderSearch(int id) {
Node resNode = null;
if (this.left != null) {
resNode = this.left.postOrderSearch(id);
}
if (this.right != null) {
resNode = this.right.postOrderSearch(id);
}
if (this.id == id) {
resNode = this;
}
return resNode;
}
三、整体代码
Node.java
package com.lzh.tree;
import com.lzh.hash.Student;
/**
* @author liuzhihao
* @create 2022-08-09 0:12
* 节点
*/
public class Node {
private int id;
private String name;
private Node left;
private Node right;
public Node(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 "Node{" +
"id=" + id +
", 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 id 你要查找的id
*/
public Node preOrderSearch(int id) {
System.out.println("遍历查找的次数"); // 用于输出遍历了多少次
Node resNode = null; // 辅助变量,用于记录遍历的结果
// 如果当前节点为需要查找的id,则赋值resNode
if (this.id == id) {
return this;
}
// 左子节点遍历
if (this.left != null) {
resNode = this.left.preOrderSearch(id);
}
// 如果辅助变量不为空,则表示已经找到了此节点,则返回
if (resNode != null) {
return resNode;
}
// 右子节点遍历
if (this.right != null) {
resNode = this.right.preOrderSearch(id);
}
return resNode;
}
/**
* 中序遍历查找
* @param id 你要查找的id
* @return
*/
public Node infixOrderSearch(int id) {
Node resNode = null;
if (this.left != null) {
resNode = this.left.infixOrderSearch(id);
}
System.out.println("遍历查找的次数"); // 用于输出遍历了多少次
if (this.id == id) {
resNode = this;
}
if (resNode != null) {
return resNode;
}
if (this.right != null) {
resNode = this.right.infixOrderSearch(id);
}
return resNode;
}
/**
* 后序遍历查找
* @param id 你要查找的id
* @return
*/
public Node postOrderSearch(int id) {
Node resNode = null;
if (this.left != null) {
resNode = this.left.postOrderSearch(id);
}
if (this.right != null) {
resNode = this.right.postOrderSearch(id);
}
if (this.id == id) {
resNode = this;
}
return resNode;
}
}
BinaryTree.java
package com.lzh.tree;
/**
* @author liuzhihao
* @create 2022-08-09 0:16
*/
public class BinaryTree {
private Node root;
public void setRoot(Node root) {
this.root = root;
}
/**
* 先序遍历
*/
public void preOrder(){
if(this.root != null) {
this.root.preOrder();
}
}
/**
* 中序遍历
*/
public void infixOrder() {
if (this.root != null) {
this.root.infixOrder();
}
}
/**
* 后序遍历
*/
public void postOrder() {
if (this.root != null) {
this.root.postOrder();
}
}
/**
* 前序遍历查找
*/
public Node preOrderSearch(int id) {
if (this.root != null) {
return this.root.preOrderSearch(id);
}
return null;
}
/**
* 中序遍历查找
* @param id
* @return
*/
public Node infixOrderSearch(int id) {
if (this.root != null) {
return this.root.infixOrderSearch(id);
}
return null;
}
/**
* 后序遍历查找
* @param id
* @return
*/
public Node postOrderSearch(int id) {
if (this.root != null) {
this.root.postOrderSearch(id);
}
return null;
}
}
BinaryTreeDemo.java
package com.lzh.tree;
/**
* @author liuzhihao
* @create 2022-08-09 0:12
*/
public class BinaryTreeDemo {
public static void main(String[] args) {
Node root = new Node(1, "1111");
Node node2 = new Node(2, "222");
Node node3 = new Node(3, "333");
Node node4 = new Node(4, "444");
Node node5 = new Node(5, "555");
Node node6 = new Node(6, "6666");
Node node7 = new Node(7, "777");
root.setLeft(node2);
root.setRight(node3);
node2.setLeft(node4);
node2.setRight(node5);
node3.setLeft(node6);
node3.setRight(node7);
BinaryTree binaryTree = new BinaryTree();
binaryTree.setRoot(root);
System.out.println("======先序遍历======");
binaryTree.preOrder();
System.out.println("======中序遍历======");
binaryTree.infixOrder();
System.out.println("======后序遍历======");
binaryTree.postOrder();
System.out.println("======前序遍历查找======");
Node resNode = binaryTree.preOrderSearch(5);
if (resNode != null) {
System.out.println("找到了节点:" + resNode);
} else {
System.out.println("未找到此节点");
}
System.out.println("======中序遍历查找======");
Node resNode1 = binaryTree.infixOrderSearch(5);
if (resNode1 != null) {
System.out.println("找到了节点:" + resNode1);
} else {
System.out.println("未找到此节点");
}
System.out.println("======后序遍历查找======");
Node resNode2 = binaryTree.infixOrderSearch(5);
if (resNode2 != null) {
System.out.println("找到了节点:" + resNode2);
} else {
System.out.println("未找到此节点");
}
}
}