I am currently working on creating a binary search tree (BST) and an extended version called Statistic BST that includes an additional size property. While experimenting, I have explored three different approaches:
- Using recursion
- Implementing polymorphism
- Applying bounded polymorphism
// 1. Recursive.
interface IBSTRec<K, V> {
key: K;
value: V;
left?: IBSTRec<K, V>;
right?: IBSTRec<K, V>;
}
class BSTRec<K, V> implements IBSTRec<K, V> {
constructor(
public key: K,
public value: V,
public left?: BSTRec<K, V>,
public right?: BSTRec<K, V>
) { /* ... */ }
insert(node: BSTRec<K, V>): BSTRec<K, V> {
// Insert logic...
return node;
}
}
// more code here...