A pre-order traversal visits the node prior to its descendants. An application of pre-order traversal could be to print a structured document.
Let’s take a look at its implementation:
preOrderTraverse(callback) { this.preOrderTraverseNode(this.root, callback); }
The preOrderTraverseNode method implementation is declared as follows:
preOrderTraverseNode(node, callback) { if (node != null) { callback(node.key); // {1} this.preOrderTraverseNode(node.left, callback); // {2} this.preOrderTraverseNode(node.right, callback); // {3} } }
The difference between the in-order and pre-order traversals is that the pre-order one visits the root node first ({1}), then the left node ({2}), and finally the right node ({3}), while the in-order ...