回答这个问题的唯一方法是凭经验。答案取决于您的实现细节、您执行的搜索类型、您拥有的硬件以及您使用的编译器。您可以从普林斯顿复制 C 代码。如果你想尝试一种函数式语言,标准 ML 有哈希表(查看SML/NJ),这里有一些用于三元搜索树的 ML:
type key = Key.ord_key
type item = Key.ord_key list
datatype set = NODE of { key : key, lt : set, eq : set, gt : set }
| LEAF
val empty = LEAF
fun member (_, LEAF) = false
| member (h::t, NODE {key, lt, eq, gt}) =
(case Key.compare (h, key)
of EQUAL => member(t, eq)
| LESS => member(h::t, lt)
| GREATER => member(h::t, gt))
| member ([], NODE {key, lt, eq, gt}) =
(case Key.compare (Key.sentinel, key)
of EQUAL => true
| LESS => member([], lt)
| GREATER => member([], gt))
exception AlreadyPresent
fun insert(h::t, LEAF) =
NODE { key = h, eq = insert(t, LEAF), lt = LEAF, gt = LEAF }
| insert([], LEAF) =
NODE { key = Key.sentinel, eq = LEAF, lt = LEAF, gt = LEAF }
| insert(h::t, NODE {key, lt, eq, gt}) =
(case Key.compare (h, key)
of EQUAL => NODE {key = key, lt = lt, gt = gt, eq = insert(t, eq)}
| LESS => NODE {key = key, lt = insert(h::t, lt), gt = gt, eq = eq}
| GREATER => NODE {key = key, lt = lt, gt = insert(h::t, gt), eq = eq})
| insert([], NODE {key, lt, eq, gt}) =
(case Key.compare (Key.sentinel, key)
of EQUAL => raise AlreadyPresent
| LESS => NODE {key = key, lt = insert([], lt), gt = gt, eq = eq}
| GREATER => NODE {key = key, lt = lt, gt = insert([], gt), eq = eq})
fun add(l, n) = insert(l, n) handle AlreadyPresent => n