title: Trouble Implementing Generic Tree
body: <p>So I have created a class for my generic tree and the Node class that goes along with it. The classes are as follows:</p>
  
  <pre><code>public class DirectoryTree&lt;E&gt; implements Serializable {
      protected Node&lt;E&gt; root;
      protected Node&lt;E&gt; child;
  
      public DirectoryTree() {
          root = null;
      }//end constructor
  
      protected DirectoryTree(Node&lt;E&gt; root) {
          this.root = root;
      }
  
      public boolean isLeaf() {
          return root.children == null;
      }
  
      public DirectoryTree&lt;E&gt; getTree() {
          if (root != null) {
              return new DirectoryTree&lt;E&gt;(root);
          }//end if
          else {
              return null;
          }//end else
      }
  
  
      public Node&lt;E&gt; getRoot() {
          return root;
      }
  
      public Node&lt;E&gt; getChild() {
          return child;
      }
  
      public String toString() {
          return null;
  
      }//end toString()
  
  
  }
  
  class Node&lt;E&gt; implements Serializable {
      protected Node&lt;E&gt; parent;
      protected E data;
      protected ArrayList&lt;Node&lt;E&gt;&gt; children;
  
      public Node(E data) {
          this.data = data;
          children = new ArrayList&lt;Node&lt;E&gt;&gt;();
      }
  
      public Node&lt;E&gt; getParent() {
          return parent;
      }
  
      public ArrayList&lt;Node&lt;E&gt;&gt; getChildren() {
          return this.children;
      }
  
      public int getNumberOfChildren() {
          return getChildren().size();
      }
  
      public boolean hasChildren() {
          return getChildren().size() &gt; 0;
      }
  
      public void addChild(Node&lt;E&gt; child) {
          int counter = 0;
          for (int i = 0; i &lt; children.size(); i++) {
              if (child == children.get(i)) {
                  System.out.println("directory exists");
                  counter++;
              }
          }
          if (counter == 0) {
              children.add(child);
          } else {
          }
      }
  
      public E getData() {
          return this.data;
      }
  
      public String toString() {
          return data.toString();
      }
  
  
  }
  </code></pre>
  
  <p>I am wondering whether my 2 classes are set up correctly for a generic tree.
  Also, I am not sure how to even implement this tree into a program.  My program is supposed to be a program that creates directories and files and puts them in the tree.  My root directory must be named /.  So far my main class is (i just started it):</p>
  
  <pre><code>public class Driver {
  
      public static void main(String[] args) {
          Scanner in = new Scanner(System.in);
          System.out.println("Welcome to Fake Unix");
          System.out.print("$");
          File f = new File("//");
          String input = in.nextLine();
          DirectoryTree tree = new DirectoryTree(new Node&lt;File&gt;(f));
          while (!input.equalsIgnoreCase("exit")) {
              String[] command = input.split(" ");
              if (command[0].equals("mkdir") &amp;&amp; command[1].equals("-p") &amp;&amp; command.length == 3) {
                  File f1 = new File(command[2]);
                  tree.root.addChild(new Node&lt;File&gt;(f1));
                  System.out.println("complete");
              }//end if
  
              if (input.equals("tree")) {
                  System.out.println(tree.toString());
              }
              System.out.print("$");
              input = in.nextLine();
          }//end while
  
      }//end main
  }//end class
  </code></pre>
  
  <p>So what I am basically asking is if my methods look correct within my tree and node classes and whether or not i am heading in the right direction implementing them in the main class.  Am i creating the directories correctly or is my syntax wrong?</p>
  
  <p>Thanks.</p>