gpt4 book ai didi

java - 创建具有两个以上子级的通用树,每个子级可能具有独特的属性

转载 作者:行者123 更新时间:2023-12-01 12:58:46 25 4
gpt4 key购买 nike

我必须创建一个通用树。它可能有 2 个以上的子节点,其中一个子节点可以充当下一级的父节点,而另一个则不能。(不是二叉树)我尝试过使用下面的代码。问题是,它们是通过假设所有 child 都可以充当 parent 而创建的。

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 的父节点。对于我的场景 B 有 2 个属性,描述和值,它必须是 HashMap ,而 C 有 1 个属性描述,所以它必须是数组列表。

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

最佳答案

这是您正在寻找的示例

树类

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 之后打印根子项

关于java - 创建具有两个以上子级的通用树,每个子级可能具有独特的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23673180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com