Assignment 5 
 
 
Asterix and the Magic Potion: 
 
 
In the village of Gaul, the druid Getafix used to create the Magic Potion that used to give them 
temporary superhuman strength. But he soon got tired of it and decided to go for a pilgrimage, 
stating that he wouldn’t return. 
Now the Gauls were left with a big problem as none of them knew how to prepare the potion. 
Getafix being his moody self, decided to share the secret ingredients, but only with the worthiest 
of them. 
He provided each of them with a parchment which had a string of length N and asked them to 
“Find the list of all substrings (may be overlapping) of that string that appeared at least 
two times.” These would serve as the key to finding the ingredients of the Magic Potion. 
To make sure only the worthiest find the answer, he made sure that the writing on the paper 
would disappear if the answer was not found in O(N3
). 
The villagers think of you as Asterix and look upto you to solve their crisis. 
 
The problem requires you to build a Suffix Tree and then use it to find the answer. 
 
In a Suffix Tree, the paths from the root to the leaves contains all possible suffixes of a word. 
As an example the Suffix Tree for BOOK is shown below. 
 
 
 
 
Node Structure Of Suffix Tree: 
 
 
Each node of the tree should contain at least: 
● Node number (root is 0) 
● An array of children 
● An array of start and end positions of the substring in the string 
 
 
For example let us take the word BOOK as input 
 
B  O  O  K 
      0       1      2       3 
 
The tree will be as shown above and the node information will be as follows. 
 
 
Node 0:  
Child[0]  is Node 1 
Child[1] is Node 2 
Child[2] is  Node 3 
Start[0] is  0 and End[0] is 3  
Start[1] is  3 and End[1] is 3  
Start[2] is  1 and End[2] is 1  
 
 
Node 3: 
Child[0]  is Node 4 
Child[1] is Node 5 
Start[0] is  3 and End[0] is 3  
Start[1] is  2 and End[1] is 3  
 
 
 
Input: 
 
A string of length N 
 
 
 
 
Functions and Outputs: 
 
 
● print_suffix_intermediate_tree(i) 
Show all the paths in the Intermediate Suffix Tree, from the root to the leaves (printing 
tabs between the edges along the same path) for index i < N 
For i=3, the output should be: 
BOOK 
K 
O K 
O OK 
 
 
● print_bfs_intermediate_tree(i) 
Show BFS (Level Ordering) of the Intermediate Suffix Trees for index i < N 
For i=3, the output should be: 
0 BOOK 1 
0 K 2 
0 O 3 
3 K 4 
3 OK 5 
 
 
● print_suffix_final_tree(N) 
All the Suffixes of the Final Suffix Tree 
For the given input, the output should be: 
BOOK 
OOK 
OK 
K 
 
 
● print_final_substrings(N) 
All the substrings as mentioned in the problem 
For the input ABABAB, the output should be: 
A 
B 
AB 
BA 
ABA 
BAB 
ABAB 

Asterix and the Maagic Potion - Suffix tree problem