BST (Binary Search Tree)

Some data structures and algorithms related to binary search trees. (implemented by JavaScript)

class Node {
    constructor(data) {
        this.data = data
        this.left = null
        this.right = null
    }
}

class Tree {
    constructor(data) {
        if (data) {
            this.root = new Node(data)
        } else {
            this.root = null
        }
    }

    insertNode(data) {
        if (this.root) {
            insertData(this.root, data)
        } else {
            this.root = new Node(data)
        }
    }
}

function insertData(node, data) {
    if (data < node.data) {
        if (node.left) {
            insertData(node.left, data)
        } else {
            node.left = new Node(data)
        }
    } else {
        if (node.right) {
            insertData(node.right, data)
        } else {
            node.right = new Node(data)
        }
    }
}

function preorder(node) {
    if (node) {
        console.log(node.data)
        preorder(node.left)
        preorder(node.right)
    }
}

function inorder(node) {
    if (node) {
        inorder(node.left)
        console.log(node.data)
        inorder(node.right)
    }
}

function getHeight(node) {
    if (node) {
        let leftHeight = getHeight(node.left)
        let rightHeight = getHeight(node.right)
        if (leftHeight > rightHeight) {
            return leftHeight + 1
        } else {
            return rightHeight + 1
        }
    } else {
        return 0
    }
}

function getMax(node) {
    if (node) {
        let leftMax = getMax(node.left)
        let rightMax = getMax(node.right)
        let max = node.data

        if (leftMax > max) {
            max = leftMax
        }
        if (rightMax > max) {
            max = rightMax
        }

        return max
    } else {
        return -1
    }
}

let tree = new Tree()
let arr = [6, 3, 8, 2, 5, 1, 7]
arr.forEach(e => {
    tree.insertNode(e)
})
// preorder(tree.root) //6, 3, 2, 1, 5, 8, 7
// inorder(tree.root)
// console.log(getHeight(tree.root)) // 4
console.log(getMax(tree.root))
  • Construct Binary Tree from Preorder and Inorder
class Node {
    constructor(data) {
        this.data = data
        this.left = null
        this.right = null
    }
}

function reconstruct(preorder, inorder) {
    if (preorder.length > 0) {
        let data = preorder[0]
        let index = inorder.indexOf(data)
        let node = new Node(data)
        node.left = reconstruct(preorder.slice(1, index + 1), inorder.slice(0, index))
        node.right = reconstruct(preorder.slice(index + 1), inorder.slice(index + 1))

        return node
    } else {
        return null
    }
}

function preorder(node) {
    if (node) {
        console.log(node.data)
        preorder(node.left)
        preorder(node.right)
    }
}

function inorder(node) {
    if (node) {
        inorder(node.left)
        console.log(node.data)
        inorder(node.right)
    }
}

let root = reconstruct([1, 2, 4, 7, 3, 5, 6, 8], [4, 7, 2, 1, 5, 3, 8, 6])
// preorder(root)
inorder(root)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,458评论 0 10
  • 希望多年以后,你想起我的时候能面带微笑 有些东西太珍贵,以至于拥有后,就想一直握在手里,害怕失去于是患得患失,最后...
    95d3b723f913阅读 154评论 0 0
  • 高铭玉阅读 163评论 0 0
  • 活着 送孩子去舞蹈班上课,听一个家长讲起她朋友的故事,让我的心里非常不是滋味。 ...
    夏天的暖阳阅读 268评论 0 0
  • I'd like to try,try every way I can. When the sun is neve...
    半下阅读 297评论 0 3