binary-tree-typed
v2.2.8
Published
Binary Tree. Javascript & Typescript Data Structure.
Maintainers
Keywords
Readme
What
Brief
This is a standalone Binary Tree data structure from the data-structure-typed collection. If you wish to access more data structures or advanced features, you can transition to directly installing the complete data-structure-typed package
How
install
npm
npm i binary-tree-typed --saveyarn
yarn add binary-tree-typedsnippet
basic BinaryTree creation and insertion
// Create a BinaryTree with entries
const entries: [number, string][] = [
[6, 'six'],
[1, 'one'],
[2, 'two'],
[7, 'seven'],
[5, 'five'],
[3, 'three'],
[4, 'four'],
[9, 'nine'],
[8, 'eight']
];
const tree = new BinaryTree(entries);
// Verify size
console.log(tree.size); // 9;
// Add new element
tree.set(10, 'ten');
console.log(tree.size); // 10;BinaryTree get and has operations
const tree = new BinaryTree(
[
[5, 'five'],
[3, 'three'],
[7, 'seven'],
[1, 'one'],
[4, 'four'],
[6, 'six'],
[8, 'eight']
],
{ isMapMode: false }
);
// Check if key exists
console.log(tree.has(5)); // true;
console.log(tree.has(10)); // false;
// Get value by key
console.log(tree.get(3)); // 'three';
console.log(tree.get(7)); // 'seven';
console.log(tree.get(100)); // undefined;
// Get node structure
const node = tree.getNode(5);
console.log(node?.key); // 5;
console.log(node?.value); // 'five';BinaryTree level-order traversal
const tree = new BinaryTree([
[1, 'one'],
[2, 'two'],
[3, 'three'],
[4, 'four'],
[5, 'five'],
[6, 'six'],
[7, 'seven']
]);
// Binary tree maintains level-order insertion
// Complete binary tree structure
console.log(tree.size); // 7;
// Verify all keys are present
console.log(tree.has(1)); // true;
console.log(tree.has(4)); // true;
console.log(tree.has(7)); // true;
// Iterate through tree
const keys: number[] = [];
for (const [key] of tree) {
keys.push(key);
}
console.log(keys.length); // 7;determine loan approval using a decision tree
// Decision tree structure
const loanDecisionTree = new BinaryTree<string>(
['stableIncome', 'goodCredit', 'Rejected', 'Approved', 'Rejected'],
{ isDuplicate: true }
);
function determineLoanApproval(
node?: BinaryTreeNode<string> | null,
conditions?: { [key: string]: boolean }
): string {
if (!node) throw new Error('Invalid node');
// If it's a leaf node, return the decision result
if (!node.left && !node.right) return node.key;
// Check if a valid condition exists for the current node's key
return conditions?.[node.key]
? determineLoanApproval(node.left, conditions)
: determineLoanApproval(node.right, conditions);
}
// Test case 1: Stable income and good credit score
console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: true })); // 'Approved';
// Test case 2: Stable income but poor credit score
console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: false })); // 'Rejected';
// Test case 3: No stable income
console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: true })); // 'Rejected';
// Test case 4: No stable income and poor credit score
console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: false })); // 'Rejected';evaluate the arithmetic expression represented by the binary tree
const expressionTree = new BinaryTree<number | string>(['+', 3, '*', null, null, 5, '-', null, null, 2, 8]);
function evaluate(node?: BinaryTreeNode<number | string> | null): number {
if (!node) return 0;
if (typeof node.key === 'number') return node.key;
const leftValue = evaluate(node.left); // Evaluate the left subtree
const rightValue = evaluate(node.right); // Evaluate the right subtree
// Perform the operation based on the current node's operator
switch (node.key) {
case '+':
return leftValue + rightValue;
case '-':
return leftValue - rightValue;
case '*':
return leftValue * rightValue;
case '/':
return rightValue !== 0 ? leftValue / rightValue : 0; // Handle division by zero
default:
throw new Error(`Unsupported operator: ${node.key}`);
}
}
console.log(evaluate(expressionTree.root)); // -27;API docs & Examples
Examples Repository
