Java 创建具有两个以上子级的泛型树,每个子级可能具有唯一的属性

Java 创建具有两个以上子级的泛型树,每个子级可能具有唯一的属性,java,generics,Java,Generics,我必须创建一个通用树。它可能有两个以上的子级,一个子级可以作为下一级的父级,另一个子级可以不作为。(不是二叉树) 我试过使用下面的代码。问题是,它们是通过假设所有孩子都可以充当父母而产生的 import java.util.ArrayList; import java.util.Iterator; import java.util.List; class Tree<T> { private T data; private Tree<T> parent; private

我必须创建一个通用树。它可能有两个以上的子级,一个子级可以作为下一级的父级,另一个子级可以不作为。(不是二叉树) 我试过使用下面的代码。问题是,它们是通过假设所有孩子都可以充当父母而产生的

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

}
这里A是根,有3个子元素b,c,d,c是C1的父元素。对于我的场景B,有2个属性,描述和值,这必须是哈希映射,而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 

这是一个你正在寻找的例子

树类

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;
  }
}

您必须实现进一步的排序,以获得非常具体的顺序,正如您在示例中看到的,您在子Part1之后打印根子级

为什么没有创建节点类??私有树父级;树类中的语句没有意义实际上我是java初学者,我不明白,你能调试一下吗?当然我能帮你调试。但这根本不是一个写得很好的解决方案。没有使用OOPs,您将了解到,根据经验,无论以何种方式,我都会在这方面帮助您。他们并不意味着您必须添加
childC.addChild(“D”)meanc是D的父级,C仍然是Akenneth的子级,在我提到的问题中,树结构。您显示的描述和值位于根a的子部分下,根A的描述和值如何?即-描述:根1.1和值:1.1-描述:根1.2和值:1.2-描述:根1.3和值:1.3参考相关树形图感谢您的回复。结束后,您必须实施订购以获得所需的订单类型。从输出中可以看到,首先打印根子级。
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