title: Coordinates of every node in a binary tree?
body: <p>I have the following function for calculating coordinates for every node in a binary tree.</p>
  
  <pre><code>//x &amp; y parameters should be untouched
  //root assumed to be 0,0
  function nodeCoordinates(node, x, y)
  {
  
      if (x === undefined &amp;&amp; y === undefined ) {x = 0; y = 0;}
      if (!node) {return;}
  
      console.log("Node: " + node.value + " x: " + x + " y: " + y);
      nodeCoordinates(node.left, --x, --y);
      nodeCoordinates(node.right, x+=2, y--);
  
  }
  </code></pre>
  
  <p>Node and Tree(BST):</p>
  
  <pre><code>//Nodes for BST
  function Node(val) {
      this.value = val;
      this.left = null;
      this.right = null;
  }
  
  //Binary Search Tree
  function BST() {
  
      this.root = null;
  
  }
  </code></pre>
  
  <p>For x, it should decrement if it goes left.  increment if it goes right.</p>
  
  <p>For y, it should decrement as it goes down a level.</p>
  
  <p>Example test code and output:</p>
  
  <pre><code>my_BST.insert(50);
  my_BST.insert(60);
  my_BST.insert(55);
  my_BST.insert(20);
  my_BST.insert(70);
  my_BST.insert(80);
  my_BST.insert(10);
  my_BST.insert(30);
  my_BST.insert(65);
  nodeCoordinates(my_BST.root);
  </code></pre>
  
  <ul>
  <li>Node: 50 x: 0 y: 0</li>
  <li>Node: 20 x: -1 y: -1</li>
  <li>Node: 10 x: -2 y: -2</li>
  <li>Node: 30 x: 0 y: -2</li>
  <li>Node: 60 x: 1 y: -1</li>
  <li>Node: 55 x: 0 y: -2</li>
  <li>Node: 70 x: 2 y: -2</li>
  <li>Node: 65 x: 1 y: -3</li>
  <li>Node: 80 x: 3 y: -3</li>
  </ul>
  
  <p>The output is correct but this was the result of fiddling with how the parameters are passed in with recursion and it feels unintuitive.  Can someone help me clarify what's going on?  Is there a more intuitive way to go about this?</p>