Java数据结构与算法——二叉树前中后序遍历 & 查找 & 删除

简介: Java数据结构与算法——二叉树前中后序遍历 & 查找 & 删除

1.关于树结构


为什么需要树这种数据结构


数组存储方式的分析优点:通过下标方式访问元素,速度快。对于有序数组,还可使用二分查找提高检索速度。缺点:如果要检索具体某个值,或者插入值(按一定顺序)会整体移动,效率较低。


链式存储方式的分析优点:在一定程度上对数组存储方式有优化(比如:插入一个数值节点,只需要将插入节点,链接到链表中即可,删除效率也很好)缺点:在进行检索时,效率仍然较低,比如(检索某个值,需要从头节点开始遍历)


树存储方式的分析能提高数据存储,读取的效率,  比如利用二叉排序树(Binary Sort Tree),既可以保证数据的检索速度,同时也可以保证数据的插入,删除,修改的速度。


树的常用术语(结合示意图理解):

节点
根节点
父节点
子节点
叶子节点 (没有子节点的节点)
节点的权(节点值)
路径(root节点找到该节点的路线)

子树
树的高度(最大层数)
森林 :多颗子树构成森林

2.二叉树


树有很多种,每个节点最多只能有两个子节点的一种形式称为二叉树。  二叉树的子节点分为左节点和右节点。


如果该二叉树的所有叶子节点都在最后一层,并且结点总数= 2^n -1 , n 为层数,则我们称为满二叉树。


如果该二叉树的所有叶子节点都在最后一层或者倒数第二层,而且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续,我们称为完全二叉树。


3.代码案例


代码中,我们主要实现对二叉树的前序、中序、后序遍历,以及前中后序查找某个结点,以及删除某个结点。

代码以构造下面这棵树为例。

package com.szh.tree;
import java.util.Objects;
/**
 *
 */
//先创建HeroNode结点
class HeroNode {
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;
    private static int preNum;
    private static int infixNum;
    private static int postNum;
    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 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);
    }
    //前序遍历查找某个结点
    public HeroNode preOrderSearch(int no) {
        System.out.println("进入前序遍历查找,第 " + (++preNum) + " 次....");
        //比较当前结点是不是我们要查找的
        if (this.no == no) {
            return this;
        }
        //1.则判断当前结点的左子节点是否为空,如果不为空,则递归前序查找
        //2.如果左递归前序查找,找到结点,则返回
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.preOrderSearch(no);
        }
        if (resNode != null) { //说明我们在左子树中找到了
            return resNode;
        }
        //1.左递归前序查找,找到结点,则返回,否继续判断,
        //2.当前的结点的右子节点是否为空,如果不空,则继续向右递归前序查找
        if (this.right != null) {
            resNode = this.right.preOrderSearch(no);
        }
        return resNode;
    }
    //中序遍历查找某个结点
    public HeroNode infixOrderSearch(int no) {
        //判断当前结点的左子节点是否为空,如果不为空,则递归中序查找
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.infixOrderSearch(no);
        }
        if (resNode != null) { //说明在左子树中找到了
            return resNode;
        }
        System.out.println("进入中序遍历查找,第 " + (++infixNum) + " 次....");
        //比较当前结点是不是我们要查找的
        if (this.no == no) {
            return this;
        }
        //否则继续进行右递归的中序查找
        if (this.right != null) {
            resNode = this.right.infixOrderSearch(no);
        }
        return resNode;
    }
    //后序遍历查找某个结点
    public HeroNode postOrderSearch(int no) {
        //判断当前结点的左子节点是否为空,如果不为空,则递归后序查找
        HeroNode 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;
        }
        System.out.println("进入后序遍历查找,第 " + (++postNum) + " 次....");
        //如果左右子树都没有找到,就比较当前结点是不是
        if (this.no == no) {
            return this;
        }
        return resNode;
    }
    //递归删除某个结点 (我们自定义如下规则)
    //1.如果删除的节点是叶子节点,则删除该节点
    //2.如果删除的节点是非叶子节点,则删除该子树
    public void delNode(int no) {
        //因为我们的二叉树是单向的,所以我们是判断当前结点的子结点是否需要删除结点,而不能去判断当前这个结点是不是需要删除结点.
        //如果当前结点的左子结点不为空,并且左子结点 就是要删除结点,就将this.left = null; 并且就返回(结束递归删除)
        if (this.left != null && this.left.no == no) {
            this.left = null;
            return;
        }
        //如果当前结点的右子结点不为空,并且右子结点 就是要删除结点,就将this.right = null; 并且就返回(结束递归删除)
        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);
        }
    }
}
//定义二叉树
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 infixOrder() {
        if (this.root != null) {
            this.root.infixOrder();
        } else {
            System.out.println("二叉树为空,无法遍历");
        }
    }
    //后序遍历
    public void postOrder() {
        if (this.root != null) {
            this.root.postOrder();
        } else {
            System.out.println("二叉树为空,无法遍历");
        }
    }
    //前序遍历查找某个结点
    public HeroNode preOrderSearch(int no) {
        if (root != null) {
            return root.preOrderSearch(no);
        }
        return null;
    }
    //中序遍历查找某个结点
    public HeroNode infixOrderSearch(int no) {
        if (root != null) {
            return root.infixOrderSearch(no);
        }
        return null;
    }
    //后序遍历查找某个结点
    public HeroNode postOrderSearch(int no) {
        if (root != null) {
            return root.postOrderSearch(no);
        }
        return null;
    }
    //递归删除某个结点
    public void delNode(int no) {
        if (root != null) {
            //如果该树只有一个root结点, 这里立即判断root是不是就是要删除结点
            if (root.getNo() == no) {
                root = null;
            } else {
                root.delNode(no);
            }
        } else {
            System.out.println("空树,不能删除~~~~");
        }
    }
}
public class BinaryTreeDemo {
    public static void main(String[] args) {
        //先需要创建一颗二叉树
        BinaryTree binaryTree = new BinaryTree();
        //创建需要的结点
        HeroNode root = new HeroNode(1, "宋江");
        HeroNode node2 = new HeroNode(2, "吴用");
        HeroNode node3 = new HeroNode(3, "卢俊义");
        HeroNode node4 = new HeroNode(4, "林冲");
        HeroNode node5 = new HeroNode(5, "关胜");
        //说明,我们先手动创建该二叉树,后面我们使用递归的方式创建二叉树
        root.setLeft(node2);
        root.setRight(node3);
        node3.setRight(node4);
        node3.setLeft(node5);
        binaryTree.setRoot(root);
        //测试前中后序遍历功能
    System.out.println("前序遍历"); // 1,2,3,5,4
    binaryTree.preOrder();
        System.out.println("--------------------------------");
    System.out.println("中序遍历");
        binaryTree.infixOrder(); // 2,1,5,3,4
        System.out.println("--------------------------------");
        System.out.println("后序遍历");
        binaryTree.postOrder(); // 2,5,4,3,1
        System.out.println("=================================================");
        //测试前中后序查找某个结点功能
        System.out.println("前序遍历查找某个结点:");
        HeroNode preNode = binaryTree.preOrderSearch(4);
        if (Objects.nonNull(preNode)) {
            System.out.printf("该结点已找到,信息为 no = %d, name = %s\n", preNode.getNo(), preNode.getName());
        } else {
            System.out.println("查无此人....");
        }
        System.out.println("--------------------------------");
        System.out.println("中序遍历查找某个结点:");
        HeroNode infixNode = binaryTree.infixOrderSearch(4);
        if (Objects.nonNull(infixNode)) {
            System.out.printf("该结点已找到,信息为 no = %d, name = %s\n", infixNode.getNo(), infixNode.getName());
        } else {
            System.out.println("查无此人....");
        }
        System.out.println("--------------------------------");
        System.out.println("后序遍历查找某个结点:");
        HeroNode postNode = binaryTree.postOrderSearch(4);
        if (Objects.nonNull(postNode)) {
            System.out.printf("该结点已找到,信息为 no = %d, name = %s\n", postNode.getNo(), postNode.getName());
        } else {
            System.out.println("查无此人....");
        }
        System.out.println("=================================================");
        //测试删除某个结点功能
        System.out.println("删除之前,前序遍历:");
        binaryTree.preOrder();
        binaryTree.delNode(5);
        System.out.println("删除之后,前序遍历:");
        binaryTree.preOrder();
    }
}

相关文章
|
2天前
|
存储 算法 Java
【Java高阶数据结构】并查集-最小生成树(下)
【Java高阶数据结构】并查集-最小生成树
11 3
|
22小时前
|
缓存 算法 Java
数据结构~缓存淘汰算法--LRU算法(Java的俩种实现方式,万字解析
数据结构~缓存淘汰算法--LRU算法(Java的俩种实现方式,万字解析
|
1天前
|
存储 Java 测试技术
滚雪球学Java(31):玩转多维数组:高效访问和遍历
【5月更文挑战第6天】?本文收录于「滚雪球学Java」专栏,专业攻坚指数级提升,希望能够助你一臂之力,帮你早日登顶实现财富自由?;同时,欢迎大家关注&&收藏&&订阅!持续更新中,up!up!up!!
6 0
滚雪球学Java(31):玩转多维数组:高效访问和遍历
|
2天前
|
存储 Java
ZigZagging on a Tree二叉树蛇形层次遍历(Java语言)
ZigZagging on a Tree二叉树蛇形层次遍历(Java语言)
7 1
|
2天前
|
Java
Tree Traversals Again(Java语言)(先序和中序创建二叉树)(遍历树)
Tree Traversals Again(Java语言)(先序和中序创建二叉树)(遍历树)
11 4
|
2天前
|
存储 算法 Java
Java 数据结构
5月更文挑战第9天
|
2天前
|
存储 算法 搜索推荐
【Java高阶数据结构】图补充-拓扑排序
【Java高阶数据结构】图补充-拓扑排序
7 1
|
2天前
|
算法 Java
【Java高阶数据结构】图的最短路径问题(下)
【Java高阶数据结构】图的最短路径问题
6 1
|
2天前
|
算法 Java
【Java高阶数据结构】图的最短路径问题(上)
【Java高阶数据结构】图的最短路径问题
6 1
http://www.vxiaotou.com