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

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

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

來看一個具體的習(xí)題實踐:

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

代碼

 import java.util.Scanner; 
   
  public class BinaryTree { 
    public static String[] str; 
    public static int count; 
   
    /** 
     * 靜態(tài)內(nèi)部類,定義二叉樹節(jié)點 
     */ 
    static class TreeNode { 
      public String data; 
      TreeNode lchild; 
      TreeNode rchild; 
   
      public TreeNode(String x) { 
        this.data = x; 
      } 
    } 
   
    /** 
     * 根據(jù)前序序列遞歸構(gòu)建二叉樹 
     * 
     * @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(); 
      } 
    } 
  }

二叉樹的深度

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

class Node{
 String name;
 Node left;
 Node right;
 public Node(String name) {
 this.name = name;
 }
 @Override
 public String toString() {
 return name;
 }
}
//定義二叉樹
class BinaryTree{
 Node root;
 
 public BinaryTree(){
 root = null;
 }
 //為了方便起見,我就直接寫個初始化的二叉樹,詳細(xì)的可以見以前的日志
 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;
 }
 //求二叉樹的深度
 int length(Node root){
 int depth1;
 int depth2;
 if(root == null) return 0;
 //左子樹的深度
 depth1 = length(root.right);
 //右子樹的深度
 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)文章

  • Java?Bean?作用域及它的幾種類型介紹

    Java?Bean?作用域及它的幾種類型介紹

    這篇文章主要介紹了Java?Bean作用域及它的幾種類型介紹,Spring框架作為一個管理Bean的IoC容器,那么Bean自然是Spring中的重要資源了,那Bean的作用域又是什么,接下來我們一起進入文章詳細(xì)學(xué)習(xí)吧
    2022-09-09
  • Java數(shù)據(jù)結(jié)構(gòu)之最小堆和最大堆的原理及實現(xiàn)詳解

    Java數(shù)據(jù)結(jié)構(gòu)之最小堆和最大堆的原理及實現(xiàn)詳解

    在計算機科學(xué)中,堆(heap)?的實現(xiàn)是一種基于樹的特殊的數(shù)據(jù)結(jié)構(gòu),它可以在數(shù)組上構(gòu)建出樹的結(jié)構(gòu)體,并滿足堆的屬性。本文就來和大家詳細(xì)聊聊Java數(shù)據(jù)結(jié)構(gòu)中的堆,感興趣的可以了解一下
    2022-09-09
  • Java集合中的fail-fast(快速失敗)機制詳解

    Java集合中的fail-fast(快速失敗)機制詳解

    這篇文章主要給大家介紹了關(guān)于Java集合中fail-fast(快速失敗)機制的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • Java多線程鎖機制相關(guān)原理實例解析

    Java多線程鎖機制相關(guān)原理實例解析

    這篇文章主要介紹了Java多線程鎖機制相關(guān)原理實例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-08-08
  • Spring一步到位精通攔截器

    Spring一步到位精通攔截器

    攔截器(Interceptor)是一種動態(tài)攔截方法調(diào)用的機制,在SpringMVC中動態(tài)攔截控制器方法的執(zhí)行。本文將詳細(xì)講講SpringMVC中攔截器的概念及入門案例,感興趣的可以嘗試一下
    2023-01-01
  • Mysql中的聚簇索引cluster index解析

    Mysql中的聚簇索引cluster index解析

    這篇文章主要介紹了Mysql中的聚簇索引cluster index解析,聚簇索引是一種數(shù)據(jù)庫索引的類型,它將數(shù)據(jù)行物理上存儲在磁盤上按照索引的順序進行排序,聚簇索引可以提高查詢性能,因為它可以減少磁盤I/O操作,需要的朋友可以參考下
    2023-10-10
  • 劍指Offer之Java算法習(xí)題精講二叉樹與N叉樹

    劍指Offer之Java算法習(xí)題精講二叉樹與N叉樹

    跟著思路走,之后從簡單題入手,反復(fù)去看,做過之后可能會忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會發(fā)現(xiàn)質(zhì)的變化
    2022-03-03
  • Java找不到或無法加載主類及編碼錯誤問題的解決方案

    Java找不到或無法加載主類及編碼錯誤問題的解決方案

    今天小編就為大家分享一篇關(guān)于Java找不到或無法加載主類及編碼錯誤問題的解決方案,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • Spring Boot 如何使用Liquibase 進行數(shù)據(jù)庫遷移(操作方法)

    Spring Boot 如何使用Liquibase 進行數(shù)據(jù)庫遷移(操作方法)

    在Spring Boot應(yīng)用程序中使用Liquibase進行數(shù)據(jù)庫遷移是一種強大的方式來管理數(shù)據(jù)庫模式的變化,本文重點講解如何在Spring Boot應(yīng)用程序中使用Liquibase進行數(shù)據(jù)庫遷移,從而更好地管理數(shù)據(jù)庫模式的變化,感興趣的朋友跟隨小編一起看看吧
    2023-09-09
  • spring解決循環(huán)依賴的方案示例

    spring解決循環(huán)依賴的方案示例

    這篇文章主要介紹spring如何解決循環(huán)依賴,文中有相關(guān)的代碼示例給大家參考,對我們的學(xué)習(xí)或工作有一定的幫助,感興趣的同學(xué)可以借鑒閱讀
    2023-05-05

最新評論