How the following Depth first traversal code works?

How the following Depth first traversal code works?

  BST.prototype.depthFirstTraversal = function(iteratorFunc, order) {
    if (order === 'pre-order')
     iteratorFunc(this.value);

    if (this.left)
     this.left.depthFirstTraversal(iteratorFunc, order);

    if (order === 'in-order')
     iteratorFunc(this.value);

    if (this.right)
     this.right.depthFirstTraversal(iteratorFunc, order);
     
    if (order === 'post-order')
     iteratorFunc(this.value);
  };

Here’s how depth-first works

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.