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

Java中二叉樹(shù)數(shù)據(jù)結(jié)構(gòu)的實(shí)現(xiàn)示例

 更新時(shí)間:2015年08月06日 11:57:20   作者:zinss26914  
這篇文章主要介紹了Java中二叉樹(shù)數(shù)據(jù)結(jié)構(gòu)的實(shí)現(xiàn)示例,包括前中后序遍歷和求二叉樹(shù)深度的方法,需要的朋友可以參考下

來(lái)看一個(gè)具體的習(xí)題實(shí)踐:

題目
根據(jù)二叉樹(shù)前序遍歷序列例如:7,-7,8,#,#,-3,6,#,9,#,#,#,-5,#,#,構(gòu)建二叉樹(shù),并且用前序、中序、后序進(jìn)行遍歷

代碼

 import java.util.Scanner; 
   
  public class BinaryTree { 
    public static String[] str; 
    public static int count; 
   
    /** 
     * 靜態(tài)內(nèi)部類(lèi),定義二叉樹(shù)節(jié)點(diǎn) 
     */ 
    static class TreeNode { 
      public String data; 
      TreeNode lchild; 
      TreeNode rchild; 
   
      public TreeNode(String x) { 
        this.data = x; 
      } 
    } 
   
    /** 
     * 根據(jù)前序序列遞歸構(gòu)建二叉樹(shù) 
     * 
     * @return 
     */ 
    public static TreeNode createBtree() { 
      TreeNode root = null; 
   
      if (count >= str.length || str[count++].equals("#")) { 
        root = null; 
      } else { 
        root = new TreeNode(str[count - 1]); 
        root.lchild = createBtree(); 
        root.rchild = createBtree(); 
      } 
   
      return root; 
    } 
   
    /** 
     * 前序遍歷 
     * 
     * @param root 
     */ 
    public static void preTraverse(TreeNode root) { 
      if (root != null) { 
        System.out.print(root.data + " "); 
        preTraverse(root.lchild); 
        preTraverse(root.rchild); 
      } 
    } 
   
    /** 
     * 中序遍歷 
     * 
     * @param root 
     */ 
    public static void inTraverse(TreeNode root) { 
      if (root != null) { 
        inTraverse(root.lchild); 
        System.out.print(root.data + " "); 
        inTraverse(root.rchild); 
      } 
    } 
   
    /** 
     * 后序遍歷 
     * 
     * @param root 
     */ 
    public static void postTraverse(TreeNode root) { 
      if (root != null) { 
        postTraverse(root.lchild); 
        postTraverse(root.rchild); 
        System.out.print(root.data + " "); 
      } 
    } 
   
    public static void main(String args[]) { 
      Scanner cin = new Scanner(System.in); 
   
      while (cin.hasNext()) { 
        String s = cin.nextLine(); 
        str = s.split(","); 
   
        count = 0; 
   
        TreeNode root = createBtree(); 
   
        // 前序遍歷 
        preTraverse(root); 
        System.out.println(); 
   
        // 中序遍歷 
        inTraverse(root); 
        System.out.println(); 
   
        // 后序遍歷 
        postTraverse(root); 
        System.out.println(); 
      } 
    } 
  }

二叉樹(shù)的深度

下面是是實(shí)現(xiàn)二叉樹(shù)的遞歸算法的實(shí)現(xiàn),其思想就是,若為空,則其深度為0,否則,其深度等于左子樹(shù)和右子樹(shù)的深度的最大值加1:

class Node{
 String name;
 Node left;
 Node right;
 public Node(String name) {
 this.name = name;
 }
 @Override
 public String toString() {
 return name;
 }
}
//定義二叉樹(shù)
class BinaryTree{
 Node root;
 
 public BinaryTree(){
 root = null;
 }
 //為了方便起見(jiàn),我就直接寫(xiě)個(gè)初始化的二叉樹(shù),詳細(xì)的可以見(jiàn)以前的日志
 public void initTree(){
 
 Node node1 = new Node("a");
 Node node2 = new Node("b");
 Node node3 = new Node("c");
 Node node4 = new Node("d");
 Node node5 = new Node("e");
 root = node1;
 node1.left = node2;
 node2.right = node3;
 node1.right = node4;
 node3.left = node5;
 }
 //求二叉樹(shù)的深度
 int length(Node root){
 int depth1;
 int depth2;
 if(root == null) return 0;
 //左子樹(shù)的深度
 depth1 = length(root.right);
 //右子樹(shù)的深度
 depth2 = length(root.left);
 if(depth1>depth2)
  return depth1+1;
 else
  return depth2+1;
 }
}
public class TestMatch{

 public static void main(String[] args) {
 BinaryTree tree = new BinaryTree();
 tree.initTree();
 System.out.println(tree.length(tree.root));
 }
}

相關(guān)文章

  • Spring Boot Actuator未授權(quán)訪(fǎng)問(wèn)漏洞的問(wèn)題解決

    Spring Boot Actuator未授權(quán)訪(fǎng)問(wèn)漏洞的問(wèn)題解決

    Spring Boot Actuator 端點(diǎn)的未授權(quán)訪(fǎng)問(wèn)漏洞是一個(gè)安全性問(wèn)題,可能會(huì)導(dǎo)致未經(jīng)授權(quán)的用戶(hù)訪(fǎng)問(wèn)敏感的應(yīng)用程序信息,本文就來(lái)介紹一下解決方法,感興趣的可以了解一下
    2023-09-09
  • java中double強(qiáng)制轉(zhuǎn)換int引發(fā)的OOM問(wèn)題記錄

    java中double強(qiáng)制轉(zhuǎn)換int引發(fā)的OOM問(wèn)題記錄

    這篇文章主要介紹了java中double強(qiáng)制轉(zhuǎn)換int引發(fā)的OOM問(wèn)題記錄,本文給大家分享問(wèn)題排查過(guò)程,感興趣的朋友跟隨小編一起看看吧
    2024-10-10
  • 關(guān)于Java的Character類(lèi)詳解

    關(guān)于Java的Character類(lèi)詳解

    這篇文章主要介紹了關(guān)于Java的Character類(lèi)詳解,Java中的Character類(lèi)是一個(gè)包裝類(lèi),用于封裝一個(gè)基本數(shù)據(jù)類(lèi)型char的值,它提供了一些靜態(tài)方法來(lái)操作字符,需要的朋友可以參考下
    2023-05-05
  • java substring(a)與substring(a,b)的使用說(shuō)明

    java substring(a)與substring(a,b)的使用說(shuō)明

    這篇文章主要介紹了java substring(a)與substring(a,b)的使用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-10-10
  • Java 爬蟲(chóng)工具Jsoup詳解

    Java 爬蟲(chóng)工具Jsoup詳解

    這篇文章主要介紹了 Java 爬蟲(chóng)工具Jsoup詳解的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • Java程序中的延遲加載功能使用

    Java程序中的延遲加載功能使用

    這篇文章主要介紹了Java程序中的延遲加載功能使用,一定程度上有助于提升性能和降低內(nèi)存使用率,需要的朋友可以參考下
    2015-07-07
  • Java裝飾者模式的深入了解

    Java裝飾者模式的深入了解

    這篇文章主要為大家介紹了Java裝飾者模式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-01-01
  • 5個(gè)JAVA入門(mén)必看的經(jīng)典實(shí)例

    5個(gè)JAVA入門(mén)必看的經(jīng)典實(shí)例

    這篇文章主要為大家詳細(xì)介紹了5個(gè)JAVA入門(mén)必看的經(jīng)典實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • Springboot分模塊項(xiàng)目搭建的實(shí)現(xiàn)

    Springboot分模塊項(xiàng)目搭建的實(shí)現(xiàn)

    在軟件開(kāi)發(fā)中,利用Spring?Boot進(jìn)行分模塊項(xiàng)目搭建能夠提高代碼的模塊化和復(fù)用性,本文主要介紹了Springboot分模塊項(xiàng)目搭建的實(shí)現(xiàn),感興趣的可以了解一下
    2024-10-10
  • Fluent Mybatis實(shí)現(xiàn)環(huán)境隔離和租戶(hù)隔離

    Fluent Mybatis實(shí)現(xiàn)環(huán)境隔離和租戶(hù)隔離

    我們?cè)趯?shí)際的業(yè)務(wù)開(kāi)發(fā)中,經(jīng)常會(huì)碰到環(huán)境邏輯隔離和租戶(hù)數(shù)據(jù)邏輯隔離的問(wèn)題。本文就詳細(xì)的來(lái)介紹一下,感興趣的小伙伴們可以參考一下
    2021-08-08

最新評(píng)論

海南省| 兰溪市| 如皋市| 洛隆县| 额济纳旗| 台东县| 上高县| 确山县| 丹巴县| 包头市| 无棣县| 浪卡子县| 鱼台县| 蓬溪县| 永仁县| 湘乡市| 农安县| 红桥区| 锦州市| 天镇县| 邢台市| 东丽区| 嘉禾县| 济南市| 秦皇岛市| 昌江| 大宁县| 泾川县| 轮台县| 阳山县| 阜宁县| 宣威市| 玉山县| 康乐县| 湘乡市| 曲阳县| 宾阳县| 句容市| 松江区| 澄江县| 准格尔旗|