One issue that, it seems to me, has not been addressed is that trees are much better suited for persistent data structures. That is, immutable structures. A standard hash table (i.e., one that uses one array of linked lists) cannot be modified without changing the entire table. One of the situations in which this is relevant is that two parallel functions have a copy of the hash table, and one of them modifies the table (if the table is mutable, this change will also be visible to the other). Another situation would be something like this:
def bar(table): # some intern stuck this line of code in table["hello"] = "world" return table["the answer"] def foo(x, y, table): z = bar(table) if "hello" in table: raise Exception("failed catastrophically!") return x + y + z important_result = foo(1, 2, { "the answer": 5, "this table": "doesn't contain hello", "so it should": "be ok" }) # catastrophic failure occurs
With a modified table, we cannot guarantee that the table obtained by the function call will remain that table during its execution, as other function calls can change it.
Thus, volatility is sometimes not a pleasant thing. Now, the way around this would be for the table to be immutable, and updates will return the new table without changing the old one. But with a hash table, this will often be an expensive O (n) operation, since the entire base array will need to be copied. On the other hand, with a balanced tree, a new tree can be generated using only O (log n) nodes that must be created (the rest of the tree is identical).
This means that an efficient tree can be very convenient when constant maps are required.
limp_chimp Nov 15 '13 at 22:22 2013-11-15 22:22
source share