UNPKG

aux-dbf

Version:

DBF for price PSM

64 lines (58 loc) 1.46 kB
import { NodeIndex } from "./NodeIndex"; export class BTree { private root: NodeIndex; constructor() { this.root = null; } public search(key: number ) : NodeIndex { return this._search(key, this.root); } public insert(key: number, adress: number, leaf?: NodeIndex): void { if(!leaf) { if(this.root) { this.insert(key, adress, this.root) } else { this.root = new NodeIndex(); this.root.value = key; this.root.Adress = adress; this.root.left = null; this.root.right = null; } return; } if (key < leaf.value) { if (leaf.left != null) { this.insert(key, adress, leaf.left); } else { leaf.left = new NodeIndex(); leaf.left.value = key; leaf.left.Adress = adress; leaf.left.left = null; leaf.left.right = null; } } else if (key >= leaf.value) { if (leaf.right != null) { this.insert(key, adress, leaf.right); } else { leaf.right = new NodeIndex(); leaf.right.value = key; leaf.right.Adress = adress; leaf.right.right = null; leaf.right.left = null; } } } private _search(key: number, leaf: NodeIndex): NodeIndex { if (leaf == null) return null; if (key == leaf.value) return leaf; if (key < leaf.value) return this._search(key, leaf.left); else return this._search(key, leaf.right); } }