UNPKG

utilict

Version:

The JavaScript utility library for performing operations on all data types and data structures.

168 lines (167 loc) 4.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BinarySearchTree = void 0; const queue_1 = require("../queue"); const utils_1 = require("../../utils"); class TreeNode { constructor(value) { this.value = value; this.left = null; this.right = null; } getValue() { return this.value; } getLeft() { return this.left; } setLeft(node) { this.left = node; } getRight() { return this.right; } setRight(node) { this.right = node; } } class BinarySearchTree { constructor(list) { this.root = null; if (list) { this.constructBSTFromList(list); } } /** * Get the root of the Binary Search Tree * @returns The root of the Binary Search Tree. */ getRoot() { return this.root; } constructBSTFromList(list) { for (const item of list) { this.insertNode(this.root, item); } return this.root; } insertNode(node, item) { if (node === null) { node = new TreeNode(item); if (this.root === null) { this.root = node; } return node; } const compareValue = (0, utils_1.compare)(item, node.getValue()); if (compareValue === -1 || compareValue === 0) { node.setLeft(this.insertNode(node.getLeft(), item)); } else if (compareValue === 1) { node.setRight(this.insertNode(node.getRight(), item)); } return node; } /** * Inserts the new node with the given value in the Binary Search Tree. * @param item * @returns The root of the Binary Search Tree. */ insert(item) { this.insertNode(this.root, item); } traverseInorder(node, list) { if (node == null) { return; } this.traverseInorder(node.getLeft(), list); list.push(node.getValue()); this.traverseInorder(node.getRight(), list); } /** * Traverse binary search tree in an inorder manner. * @returns The list of node values in an inorder manner. */ inorder() { const list = []; this.traverseInorder(this.root, list); return list; } traversePreorder(node, list) { if (node == null) { return; } list.push(node.getValue()); this.traversePreorder(node.getLeft(), list); this.traversePreorder(node.getRight(), list); } /** * Traverse binary search tree in a preorder manner. * @returns Returns the list of node values in a preorder manner. */ preorder() { const list = []; this.traversePreorder(this.root, list); return list; } traversePostorder(node, list) { if (node == null) { return; } this.traversePostorder(node.getLeft(), list); this.traversePostorder(node.getRight(), list); list.push(node.getValue()); } /** * Traverse binary search tree in a postorder manner. * @returns Returns the list of node values in a postorder manner. */ postorder() { const list = []; this.traversePostorder(this.root, list); return list; } /** * Traverse tree in a level order (Breadth First Search) manner. * @returns Returns the list of node values in a level order manner. */ levelOrder() { const queue = new queue_1.Queue(); const list = []; queue.enqueue(this.getRoot()); while (!queue.isEmpty()) { const temp = queue.dequeue(); list.push(temp.getValue()); if (temp.getLeft() != null) { queue.enqueue(temp.getLeft()); } if (temp.getRight() != null) { queue.enqueue(temp.getRight()); } } return list; } getHeight(node) { if (node == null) { return 0; } return (1 + Math.max(this.getHeight(node.getLeft()), this.getHeight(node.getRight()))); } /** * Returns the height of the Binary Search Tree. * @returns The height of the Binary Search Tree. */ height() { return this.getHeight(this.root); } /** * The height of the given node in the Binary Search Tree. * @param node * @returns Returns Height of the node in the Binary Search Tree. */ nodeHeight(node) { return this.getHeight(node); } } exports.BinarySearchTree = BinarySearchTree;