IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> 【学习笔记】java 二叉树的前中后序遍历和查找、层次遍历 图示 -> 正文阅读

[数据结构与算法]【学习笔记】java 二叉树的前中后序遍历和查找、层次遍历 图示

构造二叉树

所构造的二叉树如图所示:

在这里插入图片描述

前中序概念

定义是根据输出父节点的顺序而定的:
前序:先输出父节点,接着输出左节点,最后输出右节点。
中序:先输出左节点,接着输出父节点,最后输出右节点。
后序:先输出左节点,接着输出右节点,最后输出父节点

前序遍历-查找-图示

在这里插入图片描述
输出:
在这里插入图片描述

代码:

public void listTreeByPre(HeroNode node) {
  if (null == node) {
       return;
   }
   System.out.print(node.getName() + "->");
   if (null != node.leftNode) {
       listTreeByPre(node.leftNode);
   }
   if (null != node.rightNode) {
       listTreeByPre(node.rightNode);
   }
}

中序遍历-查找 图示

在这里插入图片描述
输出:
在这里插入图片描述

代码:

public void listTreeByInfix(HeroNode node) {
    if (null == node) {
        return;
    }
    if (null != node.leftNode) {
        listTreeByInfix(node.leftNode);
    }
    System.out.print(node.getName() + "->");
    if (null != node.rightNode) {
        listTreeByInfix(node.rightNode);
    }
}

后序遍历-查找 图示

在这里插入图片描述
输出:
在这里插入图片描述

代码;

public void listTreeByPost(HeroNode node) {
    if (null == node) {
        return;
    }
    if (null != node.leftNode) {
        listTreeByPost(node.leftNode);
    }
    if (null != node.rightNode) {
        listTreeByPost(node.rightNode);
    }
    System.out.print(node.getName() + "->");
}

层次遍历

安树层输出
在这里插入图片描述
输出:
在这里插入图片描述

代码:

public void listTreeByLevel(HeroNode node) {
    if (null != node) {
        LinkedList<HeroNode> list = new LinkedList<HeroNode>();
        list.add(node);
        HeroNode currentNode;
        while (!list.isEmpty()) {
            currentNode = list.poll();
            System.out.print(currentNode.getName() + "->");
            if (null != currentNode.leftNode) {
                list.add(currentNode.leftNode);
            }
            if (null != currentNode.rightNode) {
                list.add(currentNode.rightNode);
            }
        }
    }
}

全部代码:

BinaryTree.java

package com.dove.tree;

import java.util.LinkedList;

/**
 * 二叉树
 * date: 2021年7月15日10:52:14
 */

public class BinaryTree {
    public HeroNode creatTree() {
        HeroNode heroNode1 = new HeroNode(1, "A");
        HeroNode heroNode2 = new HeroNode(2, "B");
        HeroNode heroNode3 = new HeroNode(3, "C");
        HeroNode heroNode4 = new HeroNode(4, "D");
        HeroNode heroNode5 = new HeroNode(5, "E");
        HeroNode heroNode6 = new HeroNode(6, "F");
        HeroNode heroNode7 = new HeroNode(7, "G");
        HeroNode heroNode8 = new HeroNode(8, "H");
        HeroNode heroNode9 = new HeroNode(9, "I");
        HeroNode heroNode10 = new HeroNode(10, "J");
        HeroNode heroNode11 = new HeroNode(11, "K");

        heroNode1.leftNode = heroNode2;
        heroNode1.rightNode = heroNode3;

        heroNode2.leftNode = heroNode4;
        heroNode2.rightNode = heroNode5;

        heroNode3.leftNode = heroNode6;
        heroNode3.rightNode = heroNode7;

        heroNode4.leftNode = heroNode8;

        heroNode5.rightNode = heroNode9;

        heroNode6.leftNode = heroNode10;

        heroNode7.rightNode = heroNode11;

        HeroNode rootNode = heroNode1;
        return rootNode;
    }

    /**
     * 前序遍历
     *
     * @param node
     */
    public void listTreeByPre(HeroNode node) {
        if (null == node) {
            return;
        }
        System.out.print(node.getName() + "->");
        if (null != node.leftNode) {
            listTreeByPre(node.leftNode);
        }
        if (null != node.rightNode) {
            listTreeByPre(node.rightNode);
        }
    }

    /**
     * 遍历树,中序
     *
     * @param node
     */
    public void listTreeByInfix(HeroNode node) {
        if (null == node) {
            return;
        }
        if (null != node.leftNode) {
            listTreeByInfix(node.leftNode);
        }
        System.out.print(node.getName() + "->");
        if (null != node.rightNode) {
            listTreeByInfix(node.rightNode);
        }
    }

    /**
     * 后序编列
     *
     * @param node
     */
    public void listTreeByPost(HeroNode node) {
        if (null == node) {
            return;
        }
        if (null != node.leftNode) {
            listTreeByPost(node.leftNode);
        }
        if (null != node.rightNode) {
            listTreeByPost(node.rightNode);
        }
        System.out.print(node.getName() + "->");
    }

    /**
     * 层次遍历
     */
    public void listTreeByLevel(HeroNode node) {
        if (null != node) {
            LinkedList<HeroNode> list = new LinkedList<HeroNode>();
            list.add(node);
            HeroNode currentNode;
            while (!list.isEmpty()) {

                currentNode = list.poll();

                System.out.print(currentNode.getName() + "->");

                if (null != currentNode.leftNode) {
                    list.add(currentNode.leftNode);
                }
                if (null != currentNode.rightNode) {
                    list.add(currentNode.rightNode);
                }
            }
        }
    }

    /**
     * 前序查找 根据ID
     *
     * @param node
     * @param aimID
     * @return
     */
    public HeroNode findByIDByPre(HeroNode node, int aimID) {
        if (null == node) {
            return null;
        }
        HeroNode resNode = null;
        if (node.getId() == aimID) {
            return node;
        }
        if (null != node.leftNode) {
            resNode = findByIDByPre(node.leftNode, aimID);
        }

        if (null != resNode) {
            return resNode;
        }

        if (null != node.rightNode) {
            resNode = findByIDByPre(node.rightNode, aimID);
        }
        return resNode;
    }

    /**
     * 中序查找,根据ID
     *
     * @param node
     * @param aimID
     * @return
     */
    public HeroNode findByIDByInfix(HeroNode node, int aimID) {
        if (null == node) {
            return null;
        }
        HeroNode resNode = null;

        if (null != node.leftNode) {
            resNode = findByIDByInfix(node.leftNode, aimID);
        }

        if (null != resNode) {
            return resNode;
        }

        if (node.getId() == aimID) {
            return node;
        }

        if (null != node.rightNode) {
            resNode = findByIDByInfix(node.rightNode, aimID);
        }
        return resNode;
    }

    /**
     * 后序查找,根据ID
     *
     * @param node
     * @param aimID
     * @return
     */
    public HeroNode findByIDByPost(HeroNode node, int aimID) {
        if (null == node) {
            return null;
        }
        HeroNode resNode = null;

        if (null != node.leftNode) {
            resNode = findByIDByPost(node.leftNode, aimID);
        }

        if (null != resNode) {
            return resNode;
        }

        if (null != node.rightNode) {
            resNode = findByIDByPost(node.rightNode, aimID);
        }

        if (null != resNode) {
            return resNode;
        }

        if (node.getId() == aimID) {
            return node;
        }

        return resNode;
    }
}

class HeroNode {
    private int id;
    private String name;
    public HeroNode leftNode;
    public HeroNode rightNode;

    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 HeroNode() {

    }

    public HeroNode(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "[id:" + this.id + "---name:" + this.name + "]";
    }

}

BinaryTreeTest.java

package com.dove.tree;

public class BinaryTreeTest {
    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();
        HeroNode rootNode = binaryTree.creatTree();
        System.out.println("前序遍历:");
        binaryTree.listTreeByPre(rootNode);
        System.out.println();

        System.out.println("中序遍历:");
        binaryTree.listTreeByInfix(rootNode);
        System.out.println();

        System.out.println("后序遍历:");
        binaryTree.listTreeByPost(rootNode);
        System.out.println();

        System.out.println("层次遍历:");
        binaryTree.listTreeByLevel(rootNode);
        System.out.println();

        System.out.println("前序查找");
        HeroNode resNode = binaryTree.findByIDByPre(rootNode, 4);
        if (null != resNode){
            System.out.println(resNode.toString());
        }else {
            System.out.println("resNode is null");
        }

        System.out.println("中序查找");
        resNode = binaryTree.findByIDByInfix(rootNode, 6);
        if (null != resNode){
            System.out.println(resNode.toString());
        }else {
            System.out.println("resNode is null");
        }

        System.out.println("后序查找");
        resNode = binaryTree.findByIDByPost(rootNode, 11);
        if (null != resNode){
            System.out.println(resNode.toString());
        }else {
            System.out.println("resNode is null");
        }
    }
}

输出:

在这里插入图片描述

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-07-16 22:03:18  更:2021-07-16 22:03:35 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/7 18:33:44-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码