How to display flat data structure into hierarchical data structure (Java)?

前端 未结 5 1749
我在风中等你
我在风中等你 2021-02-02 15:11

I have recently faced this question in a practical test for a job .

Suppose you are given a flat data structure like this :

**Category**         **Name**         


        
5条回答
  •  旧巷少年郎
    2021-02-02 15:19

    To be efficient, I'd create something like this:

    public class Node
    {
      // My name
      public String name;
      // My first child. Null if no children.
      public Node child;
      // The next child after me under the same parent.
      public Node sibling;
    
      // The top node in the tree.
      public static Node adam;
    
      // Add first node to tree
      public Node(String name)
      {
        this.name=name;
        this.child=null;
        this.sibling=null;
        adam=this;
      }
    
      // Add a non-Adam node to the tree.
      public Node(String name, Node parent)
      {
        // Copy my name. Easy part.
        this.name=name;
        // Make me the first child of my parent. The previous first child becomes
        // my sibling. If the previous first child was null, fine, now my sibling is null.
        // Note this means that children always add to the front. If this is undesirable,
        // we could make this section a little more complicated to impose the desired
        // order.
        this.sibling=parent.child;
        parent.child=this;
        // As a new node, I have no children.
        this.child=null;
      }
      // Print the current node and all nodes below it.
      void printFamily(int level)
      {
        // You might want a fancier print function than this, like indenting or
        // whatever, but I'm just trying to illustrate the principle.
        System.out.println(level+" "+name);
        // First process children, then process siblings.
        if (child!=null)
          child.printFamiliy(level+1);
        if (sibling!=null)
          sibling.printFamily(level);
      }
      // Print all nodes starting with adam
      static void printFamily()
      {
        adam.printFamily(1);
      }
      // Find a node with a given name. Must traverse the tree.
      public static Node findByName(String name)
      {
        return adam.findByName(name);
      }
      private Node findByNameFromHere(String name)
      {
        if (this.name.equals(name))
          return this;
        if (child!=null)
        {
          Node found=child.findByNameFromHere(name);
          if (found!=null)
            return found;
        }
        if (sibling!=null)
        {
          Node found=sibling.findByNameFromHere(name);
          if (found!=null)
            return found;
        }
        return null;
      }
      // Now we can add by name
      public Node(String name, String parentName)
      {
        super(name, findByName(parentName));
      }
    }
    

    Usual disclaimer: This code is off the top of my head, completely untested.

    If I was doing this for a real application, I would include error checking and no doubt all sorts of peripheral stuff.

提交回复
热议问题