亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JAVA二叉樹的基本操作

 更新時(shí)間:2021年08月26日 08:52:35   作者:JackHui007  
這篇文章主要介紹了JAVA二叉樹的基本操作DEMO,想要詳情了解的小伙伴請(qǐng)接著看下文吧

記錄二叉樹的基本操作DEMO

1、創(chuàng)建一個(gè)二叉樹類

這里約束了泛型只能為實(shí)現(xiàn)了Comparable這個(gè)接口的類型。

/**
 * @author JackHui
 * @version BinaryTree.java, 2020年03月05日 12:45
 */
public class BinaryTree<T extends Comparable> {

    //樹根
    BinaryTreeNode root;

    public boolean deleteData(T data) {
        if (root.data.equals(data)) {
            root = null;
            return true;
        }
        return root.deleteNode(data);
    }

    public T frontSearch(T data) {
        return (T) root.frontSearch(data);
    }

    public T midSearch(T data) {
        return (T) root.midSearch(data);
    }

    public T rearSearch(T data) {
        return (T) root.rearSearch(data);
    }

    public void frontEach() {
        this.root.frontEach();
    }

    public void midEach() {
        this.root.midEach();
    }

    public void rearEach() {
        this.root.rearEach();
    }


    public BinaryTreeNode getRoot() {
        return root;
    }

    public void setRoot(BinaryTreeNode root) {
        this.root = root;
    }
}

2、然后創(chuàng)建二叉樹的節(jié)點(diǎn)

package binarytree;

/**
 * @author JackHui
 * @version BinaryTreeNode.java, 2020年03月06日 10:24
 */
public class BinaryTreeNode<T extends Comparable> {
    T data;
    BinaryTreeNode lChild;
    BinaryTreeNode rChild;

    public BinaryTreeNode(T data) {
        this.data = data;
    }

    //先序遍歷
    public void frontEach() {
        System.out.print(this.data + "\t");
        if (lChild != null) {
            lChild.frontEach();
        }
        if (rChild != null) {
            rChild.frontEach();
        }
    }

    //中序遍歷
    public void midEach() {
        if (lChild != null) {
            lChild.frontEach();
        }
        System.out.print(this.data + "\t");
        if (rChild != null) {
            rChild.frontEach();
        }
    }

    //后序遍歷
    public void rearEach() {
        if (lChild != null) {
            lChild.frontEach();
        }
        if (rChild != null) {
            rChild.frontEach();
        }
        System.out.print(this.data + "\t");
    }

    //先序查找
    public T frontSearch(T data) {
        T target = null;
        System.out.println("[先序遍歷]當(dāng)前遍歷到的元素:" + this.data + "\t查找的元素:" + data + "\t" + (this.data.compareTo(data) == 0 ? "查找到元素:" + data : ""));
        if (this.data.compareTo(data) == 0) {
            return data;
        } else {
            if (lChild != null && (target = (T) lChild.frontSearch(data)) != null) {
                return target;
            }
            if (rChild != null && (target = (T) rChild.frontSearch(data)) != null) {
                return target;
            }
        }
        return target;
    }

    //中序查找
    public T midSearch(T data) {
        T target = null;
        if (lChild != null && (target = (T) lChild.midSearch(data)) != null) {
            return target;
        }
        System.out.println("[中序遍歷]當(dāng)前遍歷到的元素:" + this.data + "\t查找的元素:" + data + "\t" + (this.data.compareTo(data) == 0 ? "查找到元素:" + data : ""));
        if (this.data.compareTo(data) == 0) {
            return data;
        } else {
            if (rChild != null && (target = (T) rChild.midSearch(data)) != null) {
                return target;
            }
        }
        return target;
    }

    //后序查找
    public T rearSearch(T data) {
        T target = null;
        if (lChild != null && (target = (T) lChild.rearSearch(data)) != null) {
            return target;
        }
        if (rChild != null && (target = (T) rChild.rearSearch(data)) != null) {
            return target;
        }
        System.out.println("[后續(xù)遍歷]當(dāng)前遍歷到的元素:" + this.data + "\t查找的元素:" + data + "\t" + (this.data.compareTo(data) == 0 ? "查找到元素:" + data : ""));
        if (this.data.compareTo(data) == 0) {
            return data;
        }
        return target;
    }

    //根據(jù)值刪除節(jié)點(diǎn)
    public boolean deleteNode(T data) {
        System.out.println("[節(jié)點(diǎn)刪除]當(dāng)前遍歷到的父節(jié)點(diǎn):" + this.data + "\t" + "匹配的節(jié)點(diǎn)數(shù)據(jù):" + data);
        //判斷左子樹是否匹配
        if (this.lChild != null && (this.lChild.data.compareTo(data) == 0)) {
            System.out.println("[節(jié)點(diǎn)刪除]當(dāng)前遍歷到的父節(jié)點(diǎn):" + this.data + "\t" + "匹配的節(jié)點(diǎn)數(shù)據(jù):" + data + "\t節(jié)點(diǎn)刪除成功!");
            this.lChild = null;
            return true;
        } else if (this.rChild != null && (this.rChild.data.compareTo(data) == 0)) {
            System.out.println("[節(jié)點(diǎn)刪除]當(dāng)前遍歷到的父節(jié)點(diǎn):" + this.data + "\t" + "匹配的節(jié)點(diǎn)數(shù)據(jù):" + data + "\t節(jié)點(diǎn)刪除成功!");
            this.rChild = null;
            return true;
        }
        if (this.lChild != null && this.lChild.deleteNode(data)) {
            return true;
        }
        if (this.rChild != null && this.rChild.deleteNode(data)) {
            return true;
        }
        return false;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public BinaryTreeNode getlChild() {
        return lChild;
    }

    public void setlChild(BinaryTreeNode lChild) {
        this.lChild = lChild;
    }

    public BinaryTreeNode getrChild() {
        return rChild;
    }

    public void setrChild(BinaryTreeNode rChild) {
        this.rChild = rChild;
    }
}

到此這篇關(guān)于JAVA二叉樹的基本操作DEMO的文章就介紹到這了,更多相關(guān)JAVA二叉樹內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot 如何編寫配置文件

    SpringBoot 如何編寫配置文件

    這篇文章主要介紹了SpringBoot 編寫配置文件的兩種方法,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下
    2020-11-11
  • Spring中的@EnableWebSecurity注解詳解

    Spring中的@EnableWebSecurity注解詳解

    這篇文章主要介紹了Spring中的@EnableWebSecurity注解詳解,EnableWebSecurity注解是個(gè)組合注解,它的注解中,又使用了@EnableGlobalAuthentication注解,需要的朋友可以參考下
    2023-12-12
  • java前后端加密解密crypto-js的實(shí)現(xiàn)

    java前后端加密解密crypto-js的實(shí)現(xiàn)

    這篇文章主要介紹了java前后端加密解密crypto-js的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • Java設(shè)計(jì)模式之工廠模式實(shí)現(xiàn)方法詳解

    Java設(shè)計(jì)模式之工廠模式實(shí)現(xiàn)方法詳解

    這篇文章主要介紹了Java設(shè)計(jì)模式之工廠模式實(shí)現(xiàn)方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了工廠模式的分類、原理、實(shí)現(xiàn)方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-12-12
  • springboot中JSONObject遍歷并替換部分json值

    springboot中JSONObject遍歷并替換部分json值

    這篇文章主要介紹了springboot中JSONObject遍歷并替換部分json值,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • SpringBoot通過@MatrixVariable進(jìn)行傳參詳解

    SpringBoot通過@MatrixVariable進(jìn)行傳參詳解

    這篇文章主要介紹了SpringBoot使用@MatrixVariable傳參,文章圍繞@MatrixVariable展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-06-06
  • Java多線程中的Exchanger應(yīng)用簡(jiǎn)析

    Java多線程中的Exchanger應(yīng)用簡(jiǎn)析

    這篇文章主要介紹了Java多線程中的Exchanger應(yīng)用簡(jiǎn)析,Exchanger提供了一個(gè)同步點(diǎn)exchange方法,兩個(gè)線程調(diào)用exchange方法時(shí),無論調(diào)用時(shí)間先后,兩個(gè)線程會(huì)互相等到線程到達(dá)exchange方法調(diào)用點(diǎn),此時(shí)兩個(gè)線程可以交換數(shù)據(jù),將本線程產(chǎn)出數(shù)據(jù)傳遞給對(duì)方,需要的朋友可以參考下
    2023-12-12
  • Java文件管理操作的知識(shí)點(diǎn)整理

    Java文件管理操作的知識(shí)點(diǎn)整理

    這篇文章主要為大家詳細(xì)介紹了Java中文件管理操作的一些知識(shí)點(diǎn)和實(shí)現(xiàn)方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下
    2022-09-09
  • SpringBoot JPA使用配置過程詳解

    SpringBoot JPA使用配置過程詳解

    這篇文章主要介紹了SpringBoot JPA使用配置過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Java面向?qū)ο笾饔糜蛟敿?xì)解讀

    Java面向?qū)ο笾饔糜蛟敿?xì)解讀

    這篇文章主要介紹了Java面向?qū)ο笾饔糜蛟敿?xì)解讀,在java編程中,主要的變量就是屬性和局部變量,局部變量一般是指在成員方法中定義的變量,局部變量也就是除了屬性之外的其他變量,作用域?yàn)槎x他的代碼塊中,需要的朋友可以參考下
    2024-01-01

最新評(píng)論