traversal of a two-fork DOM Tree
[JavaScript]View Plain copy function tree () {var Node = function (key) {this.key = key; This.left = null; This.right = null; Root =null; }
Pre-sequence traversal
<code style= "font-family: ' Source code Pro ', Consolas, Menlo, Monaco, ' Courier New ', monospace; Font-size:1em; Color:inherit; padding:0px; White-space:inherit; Background:none; " > First access the root node, then traverse the left subtree, and finally traverse the right subtree </code>
[JavaScript] View Plain copy Tree.prototype.preordertraverse = function (callback) { preorder (root, callback); } var preOrder = function (node,callback) { if (node !== null) { callback (node.key); preorder (node.left, callback); Preorder (node.right, callback); } }
<code style= "font-family: ' Source code Pro ', Consolas, Menlo, Monaco, ' Courier New ', monospace; Font-size:1em; Color:inherit; padding:0px; White-space:inherit; Background:none; " > modified to Dom two fork tree:</code>
[JavaScript] view plain copy var preorder = function (Node,callback) {Callback (node); if (node.firstelementchild) {///First, determine if the child element node exists This.preorder (node.firstelementchild,callback); } if (Node.lastelementchild) {this.preorder (node.lastelementchild,callback); } };
in-sequence traversal
<code style= "font-family: ' Source code Pro ', Consolas, Menlo, Monaco, ' Courier New ', monospace; Font-size:1em; Color:inherit; padding:0px; White-space:inherit; Background:none; " > First traverses the left subtree, then accesses the root node, and finally traverses the right subtree. </code>
[JavaScript]View plain Copy Tree.prototype.inOrderTraverse = function (callback) {inorder (root, callback); var inorder = function (node,callback) {if (node!== null) {