Java實現(xiàn)簡單樹結(jié)構(gòu)
簡單的實現(xiàn)了一個樹的結(jié)構(gòu),很不完善!后續(xù)參考一些其他代碼的實現(xiàn)。
試圖實現(xiàn)葉子存在可變的節(jié)點,能夠用來解析xml文件。
葉子的代碼:
package com.app;
import java.util.ArrayList;
import java.util.List;
public class treeNode<T> {
public T t;
private treeNode<T> parent;
public List<treeNode<T>> nodelist;
public treeNode(T stype){
t = stype;
parent = null;
nodelist = new ArrayList<treeNode<T>>();
}
public treeNode<T> getParent() {
return parent;
}
}
樹的代碼:
package com.app;
public class tree<T> {
public treeNode<T> root;
public tree(){}
public void addNode(treeNode<T> node, T newNode){
//增加根節(jié)點
if(null == node){
if(null == root){
root = new treeNode(newNode);
}
}else{
treeNode<T> temp = new treeNode(newNode);
node.nodelist.add(temp);
}
}
/* 查找newNode這個節(jié)點 */
public treeNode<T> search(treeNode<T> input, T newNode){
treeNode<T> temp = null;
if(input.t.equals(newNode)){
return input;
}
for(int i = 0; i < input.nodelist.size(); i++){
temp = search(input.nodelist.get(i), newNode);
if(null != temp){
break;
}
}
return temp;
}
public treeNode<T> getNode(T newNode){
return search(root, newNode);
}
public void showNode(treeNode<T> node){
if(null != node){
//循環(huán)遍歷node的節(jié)點
System.out.println(node.t.toString());
for(int i = 0; i < node.nodelist.size(); i++){
showNode(node.nodelist.get(i));
}
}
}
}
測試的主函數(shù):
package com.app;
public class app {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
/*簡單實現(xiàn)一個樹的結(jié)構(gòu),后續(xù)完善解析xml */
/*寫得滿爛的,后續(xù)查閱一些其他代碼 2012-3-12 */
//測試
/*
* string
* hello
* sinny
* fredric
* world
* Hi
* York
* */
tree<String> tree = new tree();
tree.addNode(null, "string");
tree.addNode(tree.getNode("string"), "hello");
tree.addNode(tree.getNode("string"), "world");
tree.addNode(tree.getNode("hello"), "sinny");
tree.addNode(tree.getNode("hello"), "fredric");
tree.addNode(tree.getNode("world"), "Hi");
tree.addNode(tree.getNode("world"), "York");
tree.showNode(tree.root);
System.out.println("end of the test");
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解Java構(gòu)建樹結(jié)構(gòu)的公共方法
- Java實現(xiàn)遞歸查詢樹結(jié)構(gòu)的示例代碼
- java樹結(jié)構(gòu)stream工具類的示例代碼詳解
- java合成模式之神奇的樹結(jié)構(gòu)
- JAVA使用hutool工具實現(xiàn)查詢樹結(jié)構(gòu)數(shù)據(jù)(省市區(qū))
- JAVA如何轉(zhuǎn)換樹結(jié)構(gòu)數(shù)據(jù)代碼實例
- JAVA后臺轉(zhuǎn)換成樹結(jié)構(gòu)數(shù)據(jù)返回給前端的實現(xiàn)方法
- java后端操作樹結(jié)構(gòu)的案例代碼
相關(guān)文章
idea配置springboot熱部署終極解決辦法(解決熱部署失效問題)
這篇文章主要介紹了idea配置springboot熱部署終極解決辦法(解決熱部署失效問題),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2020-07-07
WIN7系統(tǒng)JavaEE(java)環(huán)境配置教程(一)
這篇文章主要介紹了WIN7系統(tǒng)JavaEE(java+tomcat7+Eclipse)環(huán)境配置教程,本文重點在于java配置,感興趣的小伙伴們可以參考一下2016-06-06
Mybatis逆向工程實現(xiàn)連接MySQL數(shù)據(jù)庫
本文主要介紹了Mybatis逆向工程實現(xiàn)連接MySQL數(shù)據(jù)庫,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
IntelliJ IDEA2019實現(xiàn)Web項目創(chuàng)建示例
這篇文章主要介紹了IntelliJ IDEA2019實現(xiàn)Web項目創(chuàng)建示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
SpringBoot使用swagger生成api接口文檔的方法詳解
在之前的文章中,使用mybatis-plus生成了對應(yīng)的包,在此基礎(chǔ)上,我們針對項目的api接口,添加swagger配置和注解,生成swagger接口文檔,需要的可以了解一下2022-10-10
Java多線程Queue、BlockingQueue和使用BlockingQueue實現(xiàn)生產(chǎn)消費者模型方法解析
這篇文章主要介紹了Java多線程Queue、BlockingQueue和使用BlockingQueue實現(xiàn)生產(chǎn)消費者模型方法解析,涉及queue,BlockingQueue等有關(guān)內(nèi)容,具有一定參考價值,需要的朋友可以參考。2017-11-11

