java樹形結構,java樹形結構遞歸實現

本文目錄一覽:

如何用Java實現樹形結構啊?

package tree;

import java.util.LinkedList;

import java.util.List;

/**

* 功能:把一個數組的值存入二叉樹中,然後進行3種方式的遍歷

*

* 參考資料0:數據結構(C語言版)嚴蔚敏

*

* 參考資料1:

*

* 參考資料2:

*

* @author ocaicai@yeah.net @date: 2011-5-17

*

*/

public class BinTreeTraverse2 {

private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

private static ListNode nodeList = null;

/**

* 內部類:節點

*

* @author ocaicai@yeah.net @date: 2011-5-17

*

*/

private static class Node {

Node leftChild;

Node rightChild;

int data;

Node(int newData) {

leftChild = null;

rightChild = null;

data = newData;

}

}

public void createBinTree() {

nodeList = new LinkedListNode();

// 將一個數組的值依次轉換為Node節點

for (int nodeIndex = 0; nodeIndex array.length; nodeIndex++) {

nodeList.add(new Node(array[nodeIndex]));

}

// 對前lastParentIndex-1個父節點按照父節點與孩子節點的數字關係建立二叉樹

for (int parentIndex = 0; parentIndex array.length / 2 – 1; parentIndex++) {

// 左孩子

nodeList.get(parentIndex).leftChild = nodeList

.get(parentIndex * 2 + 1);

// 右孩子

nodeList.get(parentIndex).rightChild = nodeList

.get(parentIndex * 2 + 2);

}

// 最後一個父節點:因為最後一個父節點可能沒有右孩子,所以單獨拿出來處理

int lastParentIndex = array.length / 2 – 1;

// 左孩子

nodeList.get(lastParentIndex).leftChild = nodeList

.get(lastParentIndex * 2 + 1);

// 右孩子,如果數組的長度為奇數才建立右孩子

if (array.length % 2 == 1) {

nodeList.get(lastParentIndex).rightChild = nodeList

.get(lastParentIndex * 2 + 2);

}

}

/**

* 先序遍歷

*

* 這三種不同的遍歷結構都是一樣的,只是先後順序不一樣而已

*

* @param node

* 遍歷的節點

*/

public static void preOrderTraverse(Node node) {

if (node == null)

return;

System.out.print(node.data + ” “);

preOrderTraverse(node.leftChild);

preOrderTraverse(node.rightChild);

}

/**

* 中序遍歷

*

* 這三種不同的遍歷結構都是一樣的,只是先後順序不一樣而已

*

* @param node

* 遍歷的節點

*/

public static void inOrderTraverse(Node node) {

if (node == null)

return;

inOrderTraverse(node.leftChild);

System.out.print(node.data + ” “);

inOrderTraverse(node.rightChild);

}

/**

* 後序遍歷

*

* 這三種不同的遍歷結構都是一樣的,只是先後順序不一樣而已

*

* @param node

* 遍歷的節點

*/

public static void postOrderTraverse(Node node) {

if (node == null)

return;

postOrderTraverse(node.leftChild);

postOrderTraverse(node.rightChild);

System.out.print(node.data + ” “);

}

public static void main(String[] args) {

BinTreeTraverse2 binTree = new BinTreeTraverse2();

binTree.createBinTree();

// nodeList中第0個索引處的值即為根節點

Node root = nodeList.get(0);

System.out.println(“先序遍歷:”);

preOrderTraverse(root);

System.out.println();

System.out.println(“中序遍歷:”);

inOrderTraverse(root);

System.out.println();

System.out.println(“後序遍歷:”);

postOrderTraverse(root);

}

}

如何用Java實現樹形結構?

[java] view plain copy

package com.tree.test;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class Test {

public static void main(String[] args){

showTree();

}

public static void showTree(){

Connection conn=null;

ResultSet  rs = null;

Statement stmt=null;

try {

Class.forName(“com.mysql.jdbc.Driver”);

conn=DriverManager.getConnection(“jdbc:mysql://localhost/tree?user=rootpassword=root”);

/*stmt=conn.createStatement();

rs=stmt.executeQuery(“select * from country where pid=0”);

while(rs.next()){

System.out.println(rs.getString(“actile”));*/

tree(conn,0,0);

// }

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally {

try {

if(rs != null) {

rs.close();

rs = null;

}

if(stmt != null) {

stmt.close();

stmt = null;

}

if(conn != null) {

conn.close();

conn = null;

}

} catch (SQLException e) {

e.printStackTrace();

}

}

}

public static void tree(Connection conn,int id,int level){

Statement stmt = null;

ResultSet rs = null;

try {

stmt = conn.createStatement();

String sql = “select * from country where pid = ” + id;

rs = stmt.executeQuery(sql);

while(rs.next()) {

StringBuffer strPre = new StringBuffer(“”);

for(int i=0; ilevel; i++) {

strPre.append(”    “);

}

System.out.println(strPre + rs.getString(“actile”));

if(rs.getInt(“is_leaf”) != 0)

tree(conn, rs.getInt(“id”), level + 1);

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

try {

if(rs != null) {

rs.close();

rs = null;

}

if(stmt != null) {

stmt.close();

stmt = null;

}

} catch (SQLException e) {

e.printStackTrace();

}

}

}

數據庫

[sql] view plain copy

create database tree;

use tree;

create table country

(

id int primary key auto_increment,

pid int,

actile varchar(40),

is_leaf int

);

insert into country values(1,0, ‘中國’,1);

insert into country values(2,1,’北京’,0);

insert into country values(3,0,’美國’,1);

insert into country values(4,3,’紐約’,0);

insert into country values(5,1,’浙江’,1);

insert into country values(6,5,’杭州’,1);

insert into country values(7,6,’濱江’,0);

javaweb裏面樹形結構(tree)

這個是java中的forEach循環,和

for(int i =0 ;i  10 ;i++){…}

還是有點區別的。有問題可以繼續 問。

原創文章,作者:UKOX,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/147907.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
UKOX的頭像UKOX
上一篇 2024-11-02 13:14
下一篇 2024-11-02 13:14

相關推薦

  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • java client.getacsresponse 編譯報錯解決方法

    java client.getacsresponse 編譯報錯是Java編程過程中常見的錯誤,常見的原因是代碼的語法錯誤、類庫依賴問題和編譯環境的配置問題。下面將從多個方面進行分析…

    編程 2025-04-29
  • Java騰訊雲音視頻對接

    本文旨在從多個方面詳細闡述Java騰訊雲音視頻對接,提供完整的代碼示例。 一、騰訊雲音視頻介紹 騰訊雲音視頻服務(Cloud Tencent Real-Time Communica…

    編程 2025-04-29
  • Java Bean加載過程

    Java Bean加載過程涉及到類加載器、反射機制和Java虛擬機的執行過程。在本文中,將從這三個方面詳細闡述Java Bean加載的過程。 一、類加載器 類加載器是Java虛擬機…

    編程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介紹

    本文將詳細介紹Java Milvus SearchParam withoutFields的相關知識和用法。 一、什麼是Java Milvus SearchParam without…

    編程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java語言中的一個版本,於2014年3月18日發佈。本文將從多個方面對Java 8中某一周的周一進行詳細的闡述。 一、數組處理 Java 8新特性之一是Stream…

    編程 2025-04-29
  • Java判斷字符串是否存在多個

    本文將從以下幾個方面詳細闡述如何使用Java判斷一個字符串中是否存在多個指定字符: 一、字符串遍歷 字符串是Java編程中非常重要的一種數據類型。要判斷字符串中是否存在多個指定字符…

    編程 2025-04-29
  • VSCode為什麼無法運行Java

    解答:VSCode無法運行Java是因為默認情況下,VSCode並沒有集成Java運行環境,需要手動添加Java運行環境或安裝相關插件才能實現Java代碼的編寫、調試和運行。 一、…

    編程 2025-04-29
  • Java任務下發回滾系統的設計與實現

    本文將介紹一個Java任務下發回滾系統的設計與實現。該系統可以用於執行複雜的任務,包括可回滾的任務,及時恢復任務失敗前的狀態。系統使用Java語言進行開發,可以支持多種類型的任務。…

    編程 2025-04-29
  • Java 8 Group By 會影響排序嗎?

    是的,Java 8中的Group By會對排序產生影響。本文將從多個方面探討Group By對排序的影響。 一、Group By的概述 Group By是SQL中的一種常見操作,它…

    編程 2025-04-29

發表回復

登錄後才能評論