Java实现tree
2016-03-25 11:29
381 查看
package sktask;
import java.util.ArrayList;
import java.util.List;
class Tree {
private String key;
private String value;
private List<Tree> children;
public Tree(){};
public Tree(String key,String value){this.key=key;this.value=value;}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public List<Tree> getChildren() {
return children;
}
public void setChildren(List<Tree> children) {
this.children = children;
}
}
public class DoubleTree{
public static void insert(Tree parents,Tree data){
List<Tree> children=parents.getChildren();
if(children==null) children=new ArrayList<Tree>();
children.add(data);
parents.setChildren(children);
}
public static void get(Tree parents){
String key=parents.getKey();
String value=parents.getValue();
System.out.println(key+"=========="+value);
List<Tree> children=parents.getChildren();
if(null!=children && children.size()>0)
for(Tree t:children)
get(t);
}
public static void main(String[] args){
Tree root=new Tree();
root.setKey("root");
root.setValue("root");
Tree child1=new Tree("child1","child1");
Tree child2=new Tree("child2","child2");
Tree child3=new Tree("child3","child3");
Tree child4=new Tree("child4","child4");
Tree next1=new Tree("next1","next1");
Tree next2=new Tree("next2","next2");
Tree next3=new Tree("next3","next3");
Tree next4=new Tree("next4","next4");
Tree next5=new Tree("next5","next5");
insert(root, child1);
insert(root, child2);
insert(root, child3);
insert(root, child4);
insert(child1, next1);
insert(child1, next2);
insert(child1, next3);
insert(child2, next2);
insert(child3, next3);
insert(child4, next4);
get(root);
//get(child1);
}
}
import java.util.ArrayList;
import java.util.List;
class Tree {
private String key;
private String value;
private List<Tree> children;
public Tree(){};
public Tree(String key,String value){this.key=key;this.value=value;}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public List<Tree> getChildren() {
return children;
}
public void setChildren(List<Tree> children) {
this.children = children;
}
}
public class DoubleTree{
public static void insert(Tree parents,Tree data){
List<Tree> children=parents.getChildren();
if(children==null) children=new ArrayList<Tree>();
children.add(data);
parents.setChildren(children);
}
public static void get(Tree parents){
String key=parents.getKey();
String value=parents.getValue();
System.out.println(key+"=========="+value);
List<Tree> children=parents.getChildren();
if(null!=children && children.size()>0)
for(Tree t:children)
get(t);
}
public static void main(String[] args){
Tree root=new Tree();
root.setKey("root");
root.setValue("root");
Tree child1=new Tree("child1","child1");
Tree child2=new Tree("child2","child2");
Tree child3=new Tree("child3","child3");
Tree child4=new Tree("child4","child4");
Tree next1=new Tree("next1","next1");
Tree next2=new Tree("next2","next2");
Tree next3=new Tree("next3","next3");
Tree next4=new Tree("next4","next4");
Tree next5=new Tree("next5","next5");
insert(root, child1);
insert(root, child2);
insert(root, child3);
insert(root, child4);
insert(child1, next1);
insert(child1, next2);
insert(child1, next3);
insert(child2, next2);
insert(child3, next3);
insert(child4, next4);
get(root);
//get(child1);
}
}
相关文章推荐
- java生成二维码
- ConcurrentHashMap源码剖析
- Java day10 IO FileReader,Properties 待补充
- (JNI)c代码利用反射调用java方法
- Java基础之多线程与并发篇
- Eclipse启动都会Error when loading the SDK
- 从Java虚拟机来看Java对象的等价性
- Java day10 IO文件复制 FileReader,FileWriter
- java 输入
- java语言程序设计第十版(Introduce to java 10th) 课后习题 chapter7-24
- 求两线段交点(Java)
- Java day10 Math类
- response 返回 带双引号 的字符串解决办法 springmvc
- (java)两个队列实现栈的操作
- [Java] char、byte类型
- Java day09 基本数据类型
- Java生成不重复的随机数
- 理解Java垃圾回收机制
- java arraylist remove null or ""
- Java——菜单组件