Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

一棵二叉排序树 #29

Open
jyzwf opened this issue Nov 10, 2017 · 0 comments
Open

一棵二叉排序树 #29

jyzwf opened this issue Nov 10, 2017 · 0 comments

Comments

@jyzwf
Copy link
Owner

jyzwf commented Nov 10, 2017

var ary = [50, 30, 60, 15, 20, 70]

var createSearchSortTree = (ary) => {
    let l = ary.length,
        Rootobj = {},
        i = 0;

    var SearchSortTree = (ary, obj) => {
        if (!obj.data) {
            obj.data = ary[i];
            if (++i < l) {
                SearchSortTree(ary, Rootobj)
            }
        } else if (obj.data < ary[i]) {
            obj.rchild ? obj.rchild : obj.rchild = {}
            SearchSortTree(ary, obj.rchild)
        } else if (obj.data > ary[i]) {
            obj.lchild ? obj.lchild : obj.lchild = {}
            SearchSortTree(ary, obj.lchild)
        }
    }

    SearchSortTree(ary, Rootobj)

    return Rootobj

}


var result = createSearchSortTree(ary)



var searchSortTreeArray = obj => {
    var ary = []
    var sortProcess = (obj) => {
        if (obj) {
            sortProcess(obj.lchild)
            ary.push(obj.data)
            sortProcess(obj.rchild)
        }
    }

    sortProcess(obj)

    return ary
}




console.log(searchSortTreeArray(result))     // [ 15, 20, 30, 50, 60, 70 ]

image

@jyzwf jyzwf changed the title 一棵二叉查找树 一棵二叉排序树 Nov 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant