Java實現簡單樹結構
更新時間:2017年01月14日 12:59:42 作者:Java開發(fā)-擱淺
這篇文章主要為大家詳細介紹了Java實現簡單樹結構的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
簡單的實現了一個樹的結構,很不完善!后續(xù)參考一些其他代碼的實現。
試圖實現葉子存在可變的節(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)); } } } }
測試的主函數:
package com.app; public class app { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub /*簡單實現一個樹的結構,后續(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"); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
idea配置springboot熱部署終極解決辦法(解決熱部署失效問題)
這篇文章主要介紹了idea配置springboot熱部署終極解決辦法(解決熱部署失效問題),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2020-07-07WIN7系統(tǒng)JavaEE(java)環(huán)境配置教程(一)
這篇文章主要介紹了WIN7系統(tǒng)JavaEE(java+tomcat7+Eclipse)環(huán)境配置教程,本文重點在于java配置,感興趣的小伙伴們可以參考一下2016-06-06IntelliJ IDEA2019實現Web項目創(chuàng)建示例
這篇文章主要介紹了IntelliJ IDEA2019實現Web項目創(chuàng)建示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-04-04SpringBoot使用swagger生成api接口文檔的方法詳解
在之前的文章中,使用mybatis-plus生成了對應的包,在此基礎上,我們針對項目的api接口,添加swagger配置和注解,生成swagger接口文檔,需要的可以了解一下2022-10-10Java多線程Queue、BlockingQueue和使用BlockingQueue實現生產消費者模型方法解析
這篇文章主要介紹了Java多線程Queue、BlockingQueue和使用BlockingQueue實現生產消費者模型方法解析,涉及queue,BlockingQueue等有關內容,具有一定參考價值,需要的朋友可以參考。2017-11-11