tags:

views:

278

answers:

4

What is the complexity of creating a lexicographic tree?

+1  A: 

The appropriate data structure for this is probably a sorted list. In that case this becomes a bisection search problem, so O(log n).

cletus
+1 Since you are given a sorted list up front, this is probably correct (i.e. the answer his professor is looking for :-)
Gabe Moothart
A: 

just run with a simple loop and check whether each word start with whatever.

in almost every language there is a build in function for check whether one string start with another.

the complexity is O(log n), while n being the number of the words in the dictionary.

Itay
A simple loop would be O(n).
Greg Hewgill
but you stop as soon as you find
Itay
Which means you'll have to scan, on average, half the list, which makes it O(0.5n), which is O(n).
Nick Johnson
+2  A: 

If you create a prefix tree out of your input, you can perform this query in constant time.

Edit

The query is linear in the length of the search string. I meant that it was constant with regard to the size of the word list.

Gabe Moothart
A: 
giolekva
It's not O(n log n) to find a single word in a dictionary using binary search! It's O(log n).
Nick Johnson