二叉树遍历、查找
2025-05-25 本文已影响0人
8090的大叔
概念
二叉树(Binary tree)二叉树是一种每个节点最多有两个子节点(左子树和右子树)的树形数据结构,且子树有严格左右之分的有序树。
相关概念
- 结点的度:节点拥有的子树数(二叉树中度为0、1或2)。
- 叶节点:度为0的终端节点。
- 树的深度/高度:节点最大层次数(根节点为第1层)。
- 满二叉树:如果每一个层的结点数都达到最大值,则这个二叉树就是满二叉树。二叉树的深度为K,且结点总数是(2^k) -1 。
- 完全二叉树:深度为k,有n个节点的二叉树当且仅当其每一个节点都与深度为k的满二叉树中编号从1到n的节点一一对应时,称为完全二叉树 。
应用与重要性
- 实际抽象:许多实际问题(如文件系统、决策模型)可抽象为二叉树。
- 算法基础:二叉搜索树、堆、哈夫曼编码等高效算法均基于二叉树结构。
- 存储简化:相比普通树,二叉树的存储和操作算法更简单。
代码示例
- 前序遍历、中序遍历、后序遍历
- 前序查找、中序查找、后序查找
/**
* 二叉树(手动创建)
*/
public class BinaryTreeDemo {
public static void main(String[] args) {
BinaryTree bt = new BinaryTree();
Node root = new Node(1,"苹果");
Node node1 = new Node(2,"香蕉");
Node node2 = new Node(3, "梨");
Node node3 = new Node(4,"榴莲");
Node node4 = new Node(5,"菠萝");
root.setLeft(node1);
root.setRight(node2);
node2.setRight(node3);
node2.setLeft(node4);
bt.setRoot(root);
//前序遍历
bt.perOrder();
System.out.println("-------");
//中序遍历
bt.inOrder();
System.out.println("-------");
//后序遍历
bt.postOrder();
System.out.println("-------");
//前序查找
bt.preOrderSearch(6);
System.out.println("-------");
//中序查找
bt.inOrderSearch(4);
System.out.println("-------");
//后序查找
bt.postOrderSearch(6);
System.out.println("-------");
}
}
// 二叉树
class BinaryTree {
private Node root;
public void setRoot(Node root) {
this.root = root;
}
//前序遍历
public void perOrder() {
if (root != null) {
this.root.preOrder();
}else{
System.out.println("Null Node");
}
}
//中序遍历
public void inOrder() {
if (root != null) {
this.root.inOrder();
}else{
System.out.println("Null Node");
}
}
//后序遍历
public void postOrder() {
if (root != null) {
this.root.postOrder();
}else{
System.out.println("Null Node");
}
}
//前序查找
public void preOrderSearch(int search) {
if (root != null) {
Node tmp = this.root.preOrderSearch(search);
if (tmp != null) {
System.out.println(tmp);
}else{
System.out.println("No Search Node");
}
}else{
System.out.println("Null Node");
}
}
//中序查找
public void inOrderSearch(int search) {
if (root != null) {
Node tmp = this.root.inOrderSearch(search);
if (tmp != null) {
System.out.println(tmp);
}else{
System.out.println("No Search Node");
}
}else{
System.out.println("Null Node");
}
}
//后序查找
public void postOrderSearch(int search) {
if (root != null) {
Node tmp = this.root.postOrderSearch(search);
if (tmp != null) {
System.out.println(tmp);
}else{
System.out.println("No Search Node");
}
}else{
System.out.println("Null Node");
}
}
}
//自定义节点
class Node {
private int no;
private String data;
private Node left;
private Node right;
public Node(int no, String data) {
this.no = no;
this.data = data;
}
/**
* 前序遍历:先输出当前节点(初始为root节点),
* 如果左子节点不为空,则递归前序遍历
* 如果右子节点不为空,则递归前序遍历
*/
public void preOrder() {
System.out.println(this);
if (left != null) {
left.preOrder();
}
if (right != null) {
right.preOrder();
}
}
/**
* 中序遍历:如果当前节点的左子节点不为空,则递归中序遍历
* 输出当前节点
* 如果当前节点的右子节点不为空,则递归中序遍历
*/
public void inOrder() {
if (left != null) {
left.inOrder();
}
System.out.println(this);
if (right != null) {
right.inOrder();
}
}
/**
* 后序遍历:如果当前节点的左子节点不为空,则递归后序遍历
* 如果当前节点的右子节点不为空,则递归后序遍历
* 输出当前节点
*/
public void postOrder() {
if (left != null) {
left.postOrder();
}
if (right != null) {
right.postOrder();
}
System.out.println(this);
}
/** 二叉树查找 */
/**
* 前序查找:
* 判断当前节点 是否等于 查询节点
* 当前节点左子节点进行递归前序查找
* 当前节点右子节点进行递归前序查找
*/
public Node preOrderSearch(int search) {
if(search == this.no){
return this;
}
Node tmp = null; //临时变量,存储找到的节点
//左子节点递归前序查找
if(this.left != null){
tmp = this.left.preOrderSearch(search);
}
//左子节点查找完成后,判断是否找到查询节点
if(tmp != null){
return tmp;
}
//右子节点递归前序查找
if(this.right != null){
tmp = this.right.preOrderSearch(search);
}
//不存是否找到,返回临时节点
return tmp;
}
/**
* 中序查找:
* 当前节点左子节点进行递归中序查找
* 判断当前节点 是否等于 查询节点
* 当前节点右子节点进行递归中序查找
*/
public Node inOrderSearch(int search){
Node tmp = null; //临时变量,存储找到的节点
//左子节点递归前序查找
if(this.left != null){
tmp = this.left.preOrderSearch(search);
}
//左子节点查找完成后,判断是否找到查询节点
if(tmp != null){
return tmp;
}
//判断当前节点是否等于查找节点
if(search == this.no){
return this;
}
//右子节点递归前序查找
if(this.right != null){
tmp = this.right.preOrderSearch(search);
}
return tmp;
}
/**
* 后序查找:
* 当前节点左子节点进行递归后序查找
* 当前节点右子节点进行递归后序查找
* 判断当前节点 是否等于 查询节点
*/
public Node postOrderSearch(int search){
Node tmp = null; //临时变量,存储找到的节点
//左子节点递归前序查找
if(this.left != null){
tmp = this.left.preOrderSearch(search);
}
//左子节点查找完成后,判断是否找到查询节点
if(tmp != null){
return tmp;
}
//右子节点递归前序查找
if(this.right != null){
tmp = this.right.preOrderSearch(search);
}
//判断当前节点是否等于查找节点
if(search == this.no){
return this;
}
return tmp;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
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 "【编号:" + this.no + " ,数据:" + this.data +"】" ;
}
}