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)

你可能感兴趣的:(BST (Binary Search Tree))