How to create dynamic tree data structure in Java

specifically I need to represent the following:

  • The tree at any node can have an arbitrary number of children
  • Each parent node (after the root) is just a String (whose children are also Strings)
  • I need to be able to get parent and list out all the children (some sort of list or array of Strings) given an input string representing a given node
  • Dynamically populating the tree structure based on reference relationship between parent and child. Example given is I have one member1 sponsor another member2, and member2 sponsor member 3 and so and so for. Already have the table records relationship
  • Is there an available structure for this ???

    My data is from DB or a List, I will loop through the information with the name and the relation to determine if the node is a root, parent or a child.

    So during the loop, I found a child, I need a reference to the parent so that I can compare the child relation to the parent before adding the child to its parent.

    The closest code I found .

    public class TreeNode<T> implements Iterable<TreeNode<T>> {
    
        T data;
        TreeNode<T> parent;
        List<TreeNode<T>> children;
    
        public TreeNode(T data) {
            this.data = data;
            this.children = new LinkedList<TreeNode<T>>();
        }
    
        public TreeNode<T> addChild(T child) {
            TreeNode<T> childNode = new TreeNode<T>(child);
            childNode.parent = this;
            this.children.add(childNode);
            return childNode;
        }
    
        // other features ...
    
    }
    Sample usage:
    
    TreeNode<String> root = new TreeNode<String>("root");
    {
        TreeNode<String> node0 = root.addChild("node0");
        TreeNode<String> node1 = root.addChild("node1");
        TreeNode<String> node2 = root.addChild("node2");
        {
            TreeNode<String> node20 = node2.addChild(null);
            TreeNode<String> node21 = node2.addChild("node21");
            {
                TreeNode<String> node210 = node20.addChild("node210");
            }
        }
    }
    

    This is what I have done so far. The parent will get overwritten by the latest entry so hence I am unable to retrieve what I have added previously .

    public static TreeNode<String> getSet1() throws IOException {
    
            BufferedReader reader = new BufferedReader(new FileReader("test.txt"));
            String line;
    
            while ((line = reader.readLine()) != null) {
                String[] items = line.split(":");
    
                String name = items[0];
                String parent = items[1];
                String type = items[2];
    
                if (parent.equalsIgnoreCase("-") && type.equalsIgnoreCase("mainparent")) {
    
                    root = new TreeNode<String>(name);
    
    
                } else if (type.equalsIgnoreCase("ChildParent")  && parent.equalsIgnoreCase(root.toString())) {
    
                    childParent = root.addChild(name);
    
               } else if (type.equalsIgnoreCase("Child") && parent.equalsIgnoreCase(childParent.toString())) {
    
                    child = childParent.addChild(name);
                }
    
            }
    
            return root;
        }
    

    Your diagram indicates a tree of arbitrary depth, but your code only handles grandparent -> parent -> child relationships (with a single grandparent at the root).

    I would ignore the type, as all you need is the name of a person and the name of their parent. If the parent name is a dash, you know you have the root.

    Now for each person, you need to get the parent node already in the tree (assuming parents come before children in the list - if that's not the case, the problem becomes significantly more complex, as you would have to store orphaned persons temporarily and for each new person see if they are the parent of an orphaned person).

    In order to get the parent by name, you should store each person you have already processed in a second data structure, parallel to the tree. The second data structure should make it easy to look someone up by name. Maps, and in particular Hashtables, are ideal for this. This is how it works:

    Map processedPersonsMap=new Hashtable<String, TreeNode<String>>();
    

    For each person, you store them in the map, indexed by their name:

    TreeNode<String> person=...;
    processedPersonsMap.put(person.getData(), person);
    

    When you read in a new person and their parent's name is not a dash, you look up the parent:

    String parentName=items[1];
    TreeNode<String> parent=processedPersonsMap.get(parentName);
    

    In this way, no matter how deep the tree is, you will always find the right parents. However, keep in mind that this requires a valid input file where each child comes after their parent, and which does not contain circular references or missing parents.

    If those conditions are not met, you have to handle them explicitly.

    链接地址: http://www.djcxy.com/p/39878.html

    上一篇: 指数:小哦

    下一篇: 如何在Java中创建动态树数据结构