title: Implementing and visualizing a binary tree in java
body: <p>i had to implement a binary tree pictured on this file</p>
  
  <p>together with the class Diagramm and binary tree for orientation.</p>
  
  <p><img src="https://i.stack.imgur.com/T3ygo.png" alt="enter image description here"></p>
  
  <p><img src="https://i.stack.imgur.com/OmVqv.png" alt="enter image description here"></p>
  
  <p>So following the text and the Pictures i have to implement a constructor, a get- and insert method for this binary tree.</p>
  
  <pre><code>public class BinaryTree {
  
   private Node root = null;
  
  private static class Node {
      private Integer key;
      private String value;
      private Node left = null;
      private Node right = null;
  
      public Node(Integer key, String value) {
          this.key = key;
          this.value = value;
      }
  }
  
  public boolean insert(Integer key, String value) {
      if (root == null) {
          root = new Node(key, value);
          return true;
      } else {
          return insert(root, key, value);
      }
  }
  
  private boolean insert(Node node, Integer key, String value) {
      if (key.equals(node.key)) {
          // duplicate
          return false;
      } else if (key &lt; node.key) {
          if (node.left == null) {
              node.left = new Node(key, value);
              return true;
          } else {
              return insert(node.left, key, value);
          }
      } else if (key &gt; node.key) {
          if (node.right == null) {
              node.right = new Node(key, value);
              return true;
          } else {
              return insert(node.right, key, value);
          }
      }
      return false;
  }
  
  // not tested, crass assumptions, public domain
  public String get(Integer key) {
      return get(root, key); // start search from the root.
  }
  
  public String get(Node node, Integer key) {
      String result = null; // Assume key is not found
  
      if (node.key.equals(key)) { // Key matches? This is the result.
          return node.value;
      } else {
          if (key &lt; node.key &amp;&amp; node.left != null) { // key is lower than
                                                      // current node,
                                                      // and there is a left
                                                      // branch, keep
                                                      // search from there.
              result = get(node.left, key);
          } else if (key &gt; node.key &amp;&amp; node.right != null) { // key is greater
                                                              // than current
                                                              // node,
                                                              // and there is
                                                              // a left
                                                              // branch,
                                                              // keep search
                                                              // from there.
                                                              // The key &gt;
                                                              // node.key is
                                                              // arguably
                                                              // redundant.
              result = get(node.right, key);
          }
      }
  
      return result;
  }
  </code></pre>
  
  <p>How can i implement a correct main function for testing? And on top i have to visualize the binary tree with help of <a href="http://www.graphviz.org/" rel="nofollow noreferrer">graphviz</a> and adding a method in the node class which creates a string for the dot-code. How does it work with Eclipses?</p>