VOOZH about

URL: https://www.geeksforgeeks.org/dsa/suffix-tree-application-4-build-linear-time-suffix-array/

⇱ Suffix Tree Application 4 - Build Linear Time Suffix Array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Suffix Tree Application 4 - Build Linear Time Suffix Array

Last Updated : 23 Jul, 2025

Given a string, build it's Suffix Array 
We have already discussed following two ways of building suffix array: 
 


Please go through these to have the basic understanding. 
Here we will see how to build suffix array in linear time using suffix tree.
As a prerequisite, we must know how to build a suffix tree in one or the other way. 
Here we will build suffix tree using Ukkonen’s Algorithm, discussed already as below: 
Ukkonen’s Suffix Tree Construction – Part 1 
Ukkonen’s Suffix Tree Construction – Part 2 
Ukkonen’s Suffix Tree Construction – Part 3 
Ukkonen’s Suffix Tree Construction – Part 4 
Ukkonen’s Suffix Tree Construction – Part 5 
Ukkonen’s Suffix Tree Construction – Part 6
Lets consider string abcabxabcd. 
It's suffix array would be: 

0 6 3 1 7 4 2 8 9 5


Lets look at following figure: 
 

👁 Suffix Tree Application


If we do a DFS traversal, visiting edges in lexicographic order (we have been doing the same traversal in other Suffix Tree Application articles as well) and print suffix indices on leaves, we will get following: 
 

10 0 6 3 1 7 4 2 8 9 5


"$" is lexicographically lesser than [a-zA-Z]. 
The suffix index 10 corresponds to edge with "$" label. 
Except this 1st suffix index, the sequence of all other numbers gives the suffix array of the string.
So if we have a suffix tree of the string, then to get it's suffix array, we just need to do a lexicographic order DFS traversal and store all the suffix indices in resultant suffix array, except the very 1st suffix index.
 

Output:

Suffix Array for String banana is: 5 3 1 0 4 2 
Suffix Array for String GEEKSFORGEEKS is: 9 1 10 2 5 8 0 11 3 6 7 12 4 
Suffix Array for String AAAAAAAAAA is: 9 8 7 6 5 4 3 2 1 0 
Suffix Array for String ABCDEFG is: 0 1 2 3 4 5 6 
Suffix Array for String ABABABA is: 6 4 2 0 5 3 1 
Suffix Array for String abcabxabcd is: 0 6 3 1 7 4 2 8 9 5
Suffix Array for String CCAAACCCGATTA is: 12 2 3 4 9 1 0 5 6 7 8 11 10

Ukkonen’s Suffix Tree Construction takes O(N) time and space to build suffix tree for a string of length N and after that, traversal of tree take O(N) to build suffix array. 
So overall, it's linear in time and space. 
Can you see why traversal is O(N) ?? Because a suffix tree of string of length N will have at most N-1 internal nodes and N leaves. Traversal of these nodes can be done in O(N).
We have published following more articles on suffix tree applications: 
 


 

Comment