Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewstokeley/c2e6bdedc5ff37d198a489a9bd9f8251 to your computer and use it in GitHub Desktop.
Save matthewstokeley/c2e6bdedc5ff37d198a489a9bd9f8251 to your computer and use it in GitHub Desktop.

WIP

A possible implementation of a traversal

problems:

  • returning an object instead of the index is idiosyncratic.
  • currently accepts a string
  • strict equality doesn't compare deeply nested properties with objects
  • need to use prototypal inheritance to utilize the hasOwnProperty method in order to properly implement an object equality comparator
const balancedBinaryTreeSearch(val: String, tree: Array<Object>): Array<Object> {
    let i = 0
    // @todo implement a comparator
    const strictEqualsConditional = (a,b) => a === b ? true : false
    let result = []
    const loop = () => {
        for (; i < tree.length; i++) {
            const compare() {
                if(tree[i] === val) {
                    return {
                        index: i
                    }
                } elseif(tree[2i+1] === val) {
                    return {
                        index: i,
                        node: "left"
                    }
                } elseif(tree[2i+2] === val) {
                    return {
                        index: i,
                        node: "right"
                    }
                } else {
                    return false
                }
             }
             let res = compare()
             if res result.push(res)
        }
    }
    loop()
    return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment