Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Balancing a ternary search tree

How does one go about 'balancing' a ternary search tree? Most tst implementations don't address balancing, but suggest inserting in an optimal order (which I can't control.)

like image 365
uroc Avatar asked Dec 01 '10 03:12

uroc


People also ask

Which technique can be used to perform depth first search on a ternary tree?

Breadth-first search (BFS) and Depth First Search (DFS) is an algorithm for traversing or searching tree or graph data structures. Depth First Search (DFS) uses Stack data structure. DFS uses backtracking technique.


1 Answers

One simple optimization is to make it a red-black tree, which can avoid some worst-case scenarios. TSTs are really just binary trees where the value of a given node is another TST. So, the "middle" child of a node is not really part of the tree that is being balanced at each level, as it cannot move to a different parent anyway.

This ensures that each tier of the trie is traversed in log(R) time, although you could probably do even better by taking into account the size of the subtries at each node. That looks to be a lot more complicated though!

like image 80
Jim Howes Avatar answered Sep 19 '22 21:09

Jim Howes