Pure JavaScript implements the traversal of a DOM tree

Source: Internet
Author: User
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) {
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.