我必须创建一个通用树。它可能有两个以上的孩子,一个孩子可能作为下一个级别的父级而另一个孩子可能不会。(不是二叉树) 我试过使用下面的代码。问题是,它们是通过假设所有孩子都可以作为父母来创造的。
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
class Tree<T>
{
private T data;
private Tree<T> parent;
private List<Tree<T>> children;
public Tree(T data)
{
this.data = data;
children = new ArrayList<Tree<T>>();
}
/**
* Adds a new child node to the parent node returns the added Child
*/
public Tree<T> addChild(T childData)
{
Tree<T> childNode = new Tree<T>(childData);
childNode.parent = this;
this.children.add(childNode);
return childNode;
}
/**
* Returns the parent
*/
public Tree<T> getRoot()
{
if (this == null)
{
return this;
}
Tree<T> parentTmp = this.parent;
Tree<T> root = this;
//iteration
while (parentTmp != null)
{
parentTmp = root.parent;
if (parentTmp != null)
{
root = parentTmp;
}
}
return root;
}
@Override
public String toString()
{
StringBuilder output = new StringBuilder("[");
helpToString(this, output, 0);
output.append("]");
return output.toString();
}
private void helpToString(Tree<T> tree, StringBuilder output, int level)
{
if (tree == null)
return; // Tree is empty, so leave.
output.append(getSpaces(level) + tree.data);
List<Tree<T>> children2 = tree.children;
++level; //increment the level
Iterator<Tree<T>> iterator = children2.iterator();
while (children2 != null && iterator.hasNext())
{
Tree<T> next = iterator.next();
if (next != null)
{
helpToString(next, output, level); //recursion
}
}
}
private String getSpaces(int level)
{
StringBuilder sb = new StringBuilder("\n");
for (int i = 0; i < level; i++)
{
sb.append("--");
}
return sb.toString();
}
}
public class TreeTest
{
public static void main(String[] args)
{
Tree<String> root = new Tree<String>("A");
root.addChild("B");
Tree<String> childC = root.addChild("C");
root.addChild("D");
Tree<String> childC1 = childC.addChild("C1");
System.out.println("root = " + childC.getRoot()); // toString() method is invoked
// toString() method is invoked
}
}
输出:
root = [
A
--B
--C
----C1
--D]
这里A是有3个孩子的根b,c,d,C是C1的父。对于具有2个属性,描述和值的场景B,它必须是哈希映射,并且C具有1个属性描述,因此它必须是arraylist。
A[Description]
--B[Description,Value]
--C[Description]
----C1[Description,Value]
--D[Description,Value]]
所以我必须创建一个必须具有arraylist和hashmap的通用树。
Root[Description: root]
-SubPart1[Description: SubPart1]
---- Description: SubPart1.1 and Value: 1.1
---- Description: SubPart1.2 and Value: 1.2
- Description: Root 1.1 and Value: 1.1
- Description: Root 1.2 and Value: 1.2
- Description: Root 1.3 and Value: 1.3
-SubPart2[Description: SubPart2]
---- Description: SubPart2.1 and Value:2.1
---- Description: SubPart2.2 and Value: 2.2
---- Description: SubPart2.3 and Value: 2.3
- Description: Root 1.4 and Value: 1.4
-SubPart3[Description: SubPart3]
---- Description: SubPart3.1 and Value:3.1
答案 0 :(得分:1)
以下是您要找的内容的示例
树类
public class Tree
{
private String description = null;
private ArrayList<Tree> treeList = new ArrayList<Tree>();
private HashMap<Integer,Tree> childrenTreeList = new HashMap<Integer,Tree>();
private int value = 0;
public Tree(String description, int value)
{
this.description = description;
this.value = value;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
public int getValue()
{
return value;
}
public void setValue(int value)
{
this.value = value;
}
public void addChildTree(Tree childTree, int value)
{
this.childrenTreeList.put(value, childTree);
}
public void addTree(Tree tree)
{
this.treeList.add(tree);
}
public ArrayList<Tree> getTreeList()
{
return treeList;
}
public HashMap<Integer, Tree> getChildrenTreeList()
{
return childrenTreeList;
}
}
主要测试
public class TreeRunner
{
public static void main(String[] args)
{
Tree root = new Tree("Root", 1);
// Child B
Tree treeB = new Tree("SubPart1", 1);
treeB.addChildTree(new Tree("SubPart1", 1),1);
treeB.addChildTree(new Tree("SubPart1", 2),2);
root.addTree(treeB);
root.addChildTree(new Tree("Root", 1),1);
root.addChildTree(new Tree("Root", 2),2);
root.addChildTree(new Tree("Root", 3),3);
root.addChildTree(new Tree("Root", 4),4);
// Child D
Tree treeD = new Tree("SubPart2", 2);
treeD.addChildTree(new Tree("SubPart2", 1),1);
treeD.addChildTree(new Tree("SubPart2", 2),2);
treeD.addChildTree(new Tree("SubPart2", 3),3);
root.addTree(treeD);
// Child D
Tree treeE = new Tree("SubPart3", 3);
treeE.addChildTree(new Tree("SubPart3", 1),1);
root.addTree(treeE);
System.out.println(String.format("Root[Description: %s]",root.getDescription()));
for (Map.Entry<Integer, Tree> treeEntry : root.getChildrenTreeList().entrySet())
{
System.out.println(String.format("-------- Description: %s %d.%d and Value %d.%d",root.getDescription(),root.getValue(),treeEntry.getKey(),root.getValue(),treeEntry.getValue().getValue()));
}
for (Tree treeObj : root.getTreeList())
{
System.out.println(String.format("%s[Description: %s]",treeObj.getDescription(),treeObj.getDescription()));
HashMap<Integer, Tree> treeMap = treeObj.getChildrenTreeList();
for (Map.Entry<Integer, Tree> treeEntry : treeMap.entrySet())
{
System.out.println(String.format("-------- Description: %s %d.%d and Value %d.%d",treeObj.getDescription(),treeObj.getValue(),treeEntry.getKey(),treeObj.getValue(),treeEntry.getValue().getValue()));
}
}
}
}
输出
Root[Description: Root]
-------- Description: Root 1.1 and Value 1.1
-------- Description: Root 1.2 and Value 1.2
-------- Description: Root 1.3 and Value 1.3
-------- Description: Root 1.4 and Value 1.4
SubPart1[Description: SubPart1]
-------- Description: SubPart1 1.1 and Value 1.1
-------- Description: SubPart1 1.2 and Value 1.2
SubPart2[Description: SubPart2]
-------- Description: SubPart2 2.1 and Value 2.1
-------- Description: SubPart2 2.2 and Value 2.2
-------- Description: SubPart2 2.3 and Value 2.3
SubPart3[Description: SubPart3]
-------- Description: SubPart3 3.1 and Value 3.1
您必须实施进一步的订购才能获得非常具体的订单正如您在示例中所看到的那样您在SubPart1之后打印根儿童