最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JAVA二叉樹的基本操作

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

記錄二叉樹的基本操作DEMO

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

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

/**
 * @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é)點

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("[先序遍歷]當前遍歷到的元素:" + 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("[中序遍歷]當前遍歷到的元素:" + 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ù)遍歷]當前遍歷到的元素:" + this.data + "\t查找的元素:" + data + "\t" + (this.data.compareTo(data) == 0 ? "查找到元素:" + data : ""));
        if (this.data.compareTo(data) == 0) {
            return data;
        }
        return target;
    }

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

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

相關文章

  • Java反射機制原理、Class獲取方式以及應用場景詳解

    Java反射機制原理、Class獲取方式以及應用場景詳解

    反射機制是JAVA的核心知識點之一,大多數(shù)框架的實現(xiàn)原理就是利用了反射機制,掌握反射機制會使你學習框架更加輕松高效,這篇文章主要給大家介紹了關于Java反射機制原理、Class獲取方式以及應用場景的相關資料,需要的朋友可以參考下
    2022-04-04
  • java 判斷兩個時間段是否重疊的案例

    java 判斷兩個時間段是否重疊的案例

    這篇文章主要介紹了java 判斷兩個時間段是否重疊的案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • Spring Boot實戰(zhàn)之數(shù)據(jù)庫操作的示例代碼

    Spring Boot實戰(zhàn)之數(shù)據(jù)庫操作的示例代碼

    本篇文章主要介紹了Spring Boot實戰(zhàn)之數(shù)據(jù)庫操作的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • 解決微服務中關于用戶token處理到的坑

    解決微服務中關于用戶token處理到的坑

    這篇文章主要介紹了解決微服務中關于用戶token處理到的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • ant使用指南詳細入門教程

    ant使用指南詳細入門教程

    這篇文章主要介紹了ant使用指南詳細入門教程,本文詳細的講解了安裝、驗證安裝、使用方法、使用實例、ant命令等內容,需要的朋友可以參考下
    2015-06-06
  • Java編程一道多線程問題實例代碼

    Java編程一道多線程問題實例代碼

    這篇文章主要介紹了Java編程一道多線程問題實例代碼,分享了相關代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-02-02
  • Spring boot配置多數(shù)據(jù)源代碼實例

    Spring boot配置多數(shù)據(jù)源代碼實例

    這篇文章主要介紹了Spring boot配置多數(shù)據(jù)源代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07
  • Java內省之Introspector解讀

    Java內省之Introspector解讀

    這篇文章主要介紹了Java內省之Introspector解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • 詳解spring applicationContext.xml 配置文件

    詳解spring applicationContext.xml 配置文件

    本篇文章主要介紹了詳解spring applicationContext.xml 配置文件 ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • java集成kafka實例代碼

    java集成kafka實例代碼

    文章介紹了如何在Java項目中集成Apache Kafka以實現(xiàn)消息的生產(chǎn)和消費,通過添加Maven依賴、配置生產(chǎn)者和消費者、使用SpringBoot簡化集成以及控制消費者的啟動和停止,可以實現(xiàn)高效的消息處理
    2024-12-12

最新評論

思南县| 红安县| 班玛县| 斗六市| 云阳县| 咸阳市| 定日县| 宜良县| 长武县| 长沙市| 松潘县| 西充县| 泌阳县| 营山县| 溧阳市| 宜丰县| 大厂| 祁东县| 庆阳市| 五指山市| 天长市| 云和县| 鄯善县| 瓦房店市| 囊谦县| 奉节县| 开鲁县| 方山县| 巴塘县| 霍城县| 泰来县| 天气| 建阳市| 大洼县| 郴州市| 太和县| 平远县| 民勤县| 诏安县| 平昌县| 抚远县|