Ternary Tree Vs Hash Table

扶醉桌前 提交于 2019-11-30 03:40:59

Here is what I gather from the Dr. Dobbs Article reachable from the Princeton link you gave:

  1. Ternary Search Trees are up to 10% faster than hash tables on some search problems. They are sometimes slower - depending vastly on the machine used.
  2. TRTs are a custom data structure tuned by two of the finest minds of Computer Science - Jon Bentley and Robert Sedgewick both wrote good textbooks, and have done their share of practical programming. Hash tables are considered run-of-the-mill.
  3. The constants involved are significant, as Hao Wooi Lin says.
  4. Overall, this depends on the problem you are solving. The faster development time and almost ubiquitous support for hash tables in many programming languages are often more important than a ten-percent improvement in run time.

The only way to answer this question is empirically. The answer depends on the details of your implementation, what kinds of searches you do, what hardware you have, and what compiler you're using. You can copy the C code from Princeton. If you want to try a functional language, Standard ML has hash tables (look at SML/NJ), and here is some ML for ternary search trees:

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

log(n) grows slowly engough so it can often require a huge amount of data before it's slower than an O(1) algorithm when taking the constant factor into account.

This is pretty intriguing to me as well. But from the wiki I read, it claimed that ternary Tree is faster at most search problems. This is not surprising, because even though hash table has O(1) lookup, you still need time doing the hashing. So it's not really O(1) but more like O(k) where k do not depend on N (number of items in the data structure). This may gives the impression that Hash Table is faster. However, if you're dealing with a large structures, the k quickly adds up and there will come a point where searching speed of Hash Tables becomes slower than Ternary Tree.

You may have a look at tstdb: http://code.google.com/p/tstdb/

This kv-store is based on Ternary Search Tree, and it is compatible with Memcached. More over, tstdb supports prefix searching and range query which are facilitated by Ternary Search Tree.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!