title: Should I put `addNode()` in the `Node` class or `Tree`?
body: <p>I'm trying to implement a binary search tree (BST) with javascript. I came up with 2 versions.</p>
  <p>Here is the first version</p>
  <pre><code>class Node {
    constructor(value) {
      this.value = value
      this.left = null
      this.right = null
    }
  }
  
  class Tree {
    constructor(value) {
      if (value != null) {
        this.root = new Node(value)
      } else {
        return null;
      }
    }
    addNode(parent, n) {
      if (n.value === parent.value) {
        return;
      } else if (n.value &lt; parent.value) {
        if (parent.left == null) {
          parent.left = n;
        } else {
          this.addNode(parent.left, n)
        }
      } else { // n.value &gt; parent.value
        if (parent.right == null) {
          parent.right = n;
        } else {
          this.addNode(parent.right, n)
        }
      }
    }
    addValue(val) {
      let n = new Node(val);
      if (this.root == null) {
        this.root = n;
      } else {
        this.addNode(this.root, n);
      }
    }
  }
  </code></pre>
  <p>I defined a simple class <code>Node</code> and the <code>Tree</code> class.</p>
  <p>The <code>Node</code> class abstracts any node that ranges from the root to a leaf.</p>
  <p>The <code>Tree</code> class handles all the business, especially the <code>addNode</code> method. In contrast, my 2nd version put the <code>addNode</code> method in <code>Node</code> class. Here is the code</p>
  <pre><code>class Node {
    constructor(value) {
      this.value = value
      this.left = null
      this.right = null
    }
    addNode(n) {
      if (n.value == this.value) {
        return;
      } else if (n.value &lt; this.value) {
        if (this.left == null) {
          this.left = n;
        } else {
          this.left.addNode(n)
        }
      } else {
        if (this.right == null) {
          this.right = n;
        } else {
          this.right.addNode(n)
        }
      }
    }
  }
  
  class Tree {
    constructor(value) {
      if (value != null) {
        this.root = new Node(value)
      } else {
        return null;
      }
    }
    addValue(val) {
      let n = new Node(val);
      if (this.root == null) {
        this.root = n;
      } else {
        this.root.addNode(n);
      }
    }
  }
  </code></pre>
  <p>Both versions works as expected. I know I should add more check, in case something like <code>tree.addValue();</code> happens.</p>
  <p>I'd just like to know which version should I go with, and why. Are there some kind of principles or consideration to make such decision?</p>