haleyhousellc / arboriculture

A simple tree library written in TypeScript.
https://haleyhousellc.github.io/arboriculture
ISC License
2 stars 0 forks source link

Consider refactoring the tree API to use key/value #9

Closed haleyga closed 6 years ago

haleyga commented 7 years ago

Currently, the #find operation takes an entire object, even if the comparer only uses a piece of the object. Consider refactoring this to use just a key to find a node.

For example, say we have the following type as the data type for a tree:

interface ITest {
    member1: number;
    member2: string;
}

function testComparer(a: ITest, b: ITest): number {
    return a.member1 - b.member1;
}

Perhaps the only member used in the comparer is #member1. This would be considered the key, while the entire object would be the value. To search for the node containing a specific object, the current API requires a full object passed to #find:

tree.find({ member1: 3, member2: 'this can be anything during find, since the comparer only considers #member1 during lookup'});

Consider refactoring #find such that it only requires the 'key' during lookup:

tree.find(3);

This would likely require a second templated argument throughout the codebase, and would also be a major breaking change. Rather than IBinaryTree<T>, the base signature might be something like IBinaryTree<K,V>. Given the example above, an invocation might look like new BinarySearchTree<number, ITest> rather than the current new BinarySearchTree<ITest>.

haleyga commented 7 years ago

This would also likely require refactoring the signature of and use of an IComparer<T>.