Recursive traversal method for JavaScript two fork Tree

Source: Internet
Author: User

Binary tree is composed of root node, left subtree, right subtree, Zuozi and friends subtree are a binary tree respectively.

Here is an example of a binary tree:

{    "ruleId":"",    "name":"customer age greater than",    "ID":"Root",    "expression":"age>20",    "Yes":{        "name":"customers older than",        "ID":"root-true",        "expression":"age>25",        "Yes":{            "name":"50,000 of the available loan amount",            "ID":"root-true-true",            "expression":"Money = 50000"            },        "No":{            "name":"30,000 of the available loan amount",            "ID":"Root-true-false",            "expression":"Money = 30000"                }        },    "No":{        "name":"customer age less than",        "ID":"Root-false",        "expression":"age<18",        "Yes":{            "name":"No loans",            "ID":"root-false-true",            "expression":"Money = 0"            },        "No":{            "name":"10,000 of the available loan amount",            "ID":"Root-false-false",            "expression":"Money = 10000"                }    }}

1) First Order traversal

function Preorder (data) {    if(data) {        console.log (data.name);        Preorder (data.yes);        Preorder (data.no)    }}  

2) Middle sequence traversal

function Preorder (data) {    if(data) {        preorder (data.yes);        Console.log (data.name);        Preorder (data.no)    }}

3) Post-traversal

function Preorder (data) {    if(data) {        preorder (data.yes);        Preorder (data.no);        Console.log (data);}    }

All three of these are depth-first traversal, as priority is given to deep access.

Recursive traversal method for JavaScript two fork Tree

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.