Parent, child & sibling iterating

Discussion in 'Spigot Plugin Development' started by MrDienns, Jun 3, 2017.

  1. Hi,

    I have a small parent & child system in one of my plugins.
    Each object can have a child. That child can have siblings, but also more childs.

    Here's a beautfifully drawn picture of what I mean (I know, I should be an artist instead):

    http://prntscr.com/ffdg6m
    Note that an object can only have one parent.

    All of these objects have a name. I want to create a list and add all names to this list.
    Every object also has the following methods:
    • isChild() - Boolean
    • getChilds() - List
    • hasChild() - Boolean
    • getParent() - Object
    • hasParent() - Boolean
    All of the above methods work perfectly. I can't come up with some code that does what I want. Any help would be appriciated.

    Thanks :D
     
  2. So you want from us a code for your methods?
     
  3. @MrDienns so basically a Trie? In what order should the list be created? Based on that, I could recommend an algorithm.
     
  4. I'd need a loop that adds all names of all of the objects to a list.

    I'm not sure if you could all it an algorithm though. All I need would be a loop that adds the names of the object to a list :p

    EDIT: Also, if you're asking for an order, I suppose you mean the siblings, right? For now, let's just use alphabetic.

    EDIT 2:
    http://prntscr.com/ffe2yb
    I noticed I had a few invalid errors. This should be the order for the given example, assuming they are alphabetic.
     
  5. Just walk through the Trie and add all nodes to a TreeSet
     
  6. Something like ?

    Person.java
    Code (Java):

    public class Person {

        public final String name;
        public boolean isChild;
        public Parent parent;

        private TreeSet<Person> children = new TreeSet();

        public Person(String name, boolean isChild) {
            this.name = name;
            this.isChild = isChild;
        }

        public boolean isChild() {
            return isChild;
        }

        public TreeSet<Person> getChildren() {
            return children;
        }

        public boolean hasChildren() {
            return !children.isEmpty();
        }

        public boolean hasParent(Person person) {
            return person.getParent() != null;
        }

        public Parent getParent() {
            return parent;
        }
    }
     
    Parent.java
    Code (Java):

    public class Parent extends Person {
        public Parent(String name) {
            super(name, false);
        }
    }
     
    Child.java
    Code (Java):

    public class Child extends Person {

        public Child(String name) {
            super(name, true);
        }

        public Child(Parent parent, String name) {
            super(name, true);
            super.parent = parent;
        }

    }
     
    Usage:
    Code (Java):

        public void usage() {
            List<Person> persons = new ArrayList();

            Parent parent = new Parent("Dad");
            Child bob = new Child("Bob");
            // or
            Child steve = new Child(parent, "Steve");
         
            // add child to the parent
            parent.getChildren().add(bob);
            parent.getChildren().add(steve);

            // add person to the list
            persons.add(parent);

            persons.stream().filter(p -> p.isChild).forEach(person -> {
                //do stuff for all Children
            });

            persons.stream().filter(p -> p.hasChildren()).forEach(person -> {
                //do stuff for all Person's with Children
            });
            //..etc
        }
     
    This is how I would go about it, anyways. Hope this helps
     
    • Winner Winner x 1
  7. There's no use for a TreeSet in the Person class, nor is there use for a Child subclass. A Trie is simply implemented by:
    Code (Text):
    struct TrieNode:
      T value
      TrieNode parent
      List children

      addChild(T value):
        child = new TrieNode(value, this) // value, parent
        children.add(child)
        return child // For adding grandchildren
     
      visit(consumer):
        consumer.consume(parent)
        children.forEach(child -> child.visit(consumer))
    Then simply call root.visit(set::add), where set is a TreeSet with a proper comparator (i.e. comparing the value)
     
    • Useful Useful x 1
  8. Choco

    Moderator

    [​IMG]

    wat... I don't want to be consumed :c
     
    • Like Like x 2
  9. This seems fairly usefull, however, building up the List<?> variable is the entire point of this thread. Looping through them once I have all childs, parents & siblings wouldn't be an issue. Could you come up with some loop that creates a list of all parent names, which the given available methods in my first post? That would greatly help :D
     
  10. Wouldn't my recursive visit method do what you need? (With as consumer, a Collection add)
     
  11. I have seen something like this nicely done in an Enum. It recursively adds new family members in a heriarchy like your diagram

    Code (Text):
    Family.SIBLING("Steve", Family.PARENT("Bob"), Family.PARENT("Josh"));
    Instead of a List you can have a family variable which stores all of this
     
    #11 Synapz, Jun 4, 2017
    Last edited: Jun 4, 2017
  12. A list of all parent names?
    Code (Java):

    getChilds().forEach(child -> {
       println(child.getParent().name);
    });
     
     
  13. I get chills that the list is called childs.

    Children
    Sorry, had to get it out of my system.
     
  14. Yeah I noticed as well. I already changed :p
    [​IMG]
     
    • Like Like x 1