BST

二叉搜索树

源码

https://github.com/ddzy/ts-utility-plugins/tree/master/src/ddzy/utility/algorithm/binary-search-tree

说明

二叉搜索树插件, 源码地址:

https://github.com/ddzy/ts-utility-plugins/tree/master/src/ddzy/utility/algorithm/binary-search-tree

用法

index.ts
const bst = new utilityAlgorithm.BST({
nodes: [2, 5, 3, 8, 7],
});
bst.handleInsert(1).handleInsert(-5).handleInsert(-2);
bst.handleRemove(8);
bst.handleRemove(23).handleInsert(23);
console.log(bst.handleGetDepth());
console.log(bst.handleGetDepth(4));
console.log(bst.handleGetDepth(23));
console.log(bst.handleGetDepth(9));
console.log(bst.handleGetDepth(7));
console.log(bst.handleGetDepth(2));
console.log(bst.handleGetDepth(-1));
console.log(bst.handleHasValue(23));
console.log(bst.handleHasValue(6));
console.log(bst.handleHasValue(7));
console.log(bst.handleHasValue(2));
bst.handleFrontOrderTraversal((node) => {
console.log(node);
});
bst.handleMiddleOrderTraversal((node) => {
console.log(node);
});
bst.handlBackOrderTraversal((node) => {
console.log(node);
});
console.log(bst.handleGetHeight(2));
console.log(bst.handleGetHeight(9));
console.log(bst.handleGetHeight(23));
console.log(bst.handleGetHeight(7));
console.log(bst.handleGetLeaves());
console.log(bst.handleGetRoot());
console.log(bst.handleGetMinValue());
console.log(bst.handleGetMaxValue());

API

Name

Value

Description

handleInsert

(value: number) => BinarySearchTree

添加新节点

handleRemove

(value: number) => BinarySearchTree

移除指定节点

handleGetDepth

(value?: number) => number

获取指定节点的深度, 不传value则返回树的深度

handleGetHeight

(value?: number) => number

获取指定节点的高度, 不传value则返回树的高度

handleGetRoot

() => TreeNode | null

获取root节点

handleGetLeaves

() => TreeNode[]

获取所有叶子节点

handleGetMaxValue

() => number | null

获取最大值

handleGetMinValue

() => number | null

获取最小值

handleHasValue

(value: number) => boolean

查找二叉树中是否存在指定的值

handleFrontOrderTraversal

(callback: (node: TreeNode) => void) => BinarySearchTree

前序遍历

handleMiddleOrderTraversal

(callback: (node: TreeNode) => void) => BinarySearchTree

中序遍历

handleBackOrderTraversal

(callback: (node: TreeNode) => void) => BinarySearchTree

后序遍历