forked from sambit77/Algoexpert-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuffixTreeConstruction.java
More file actions
102 lines (86 loc) · 3.03 KB
/
SuffixTreeConstruction.java
File metadata and controls
102 lines (86 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import java.util.*;
class A
{
//A node of a suffix tree is a hashmap
static class TreeNode
{
//the map contains maooing from character to another hashmap
HashMap<Character,TreeNode> children = new HashMap<Character,TreeNode>();
}
static class suffixTree
{
//when suffix tree is called i.e its object is created then we instantiate root of suffix tree
static TreeNode root = new TreeNode();
//to mark the end of substring / a branch of suffix tree
static char endSymbol = '*';
public suffixTree(String str)
{
populateSuffuxTree(str);
}
}
//Time Complexity O(n^2) | Space Complexity O(n^2)
//n=input String length
public static void populateSuffuxTree(String str)
{
//generate all suffix of the given string and insert it in tree
for(int i = 0 ; i < str.length() ; i++)
{
insertSubstringStartingAt(i,str);
}
}
public static void insertSubstringStartingAt(int i , String str)
{
//to insert a substring first get the root
TreeNode node = suffixTree.root;
//iterate every charcater in the substring
for(int j = i ; j < str.length() ; j++)
{
//get the character at index j
char letter = str.charAt(j);
//if the charcter is not in the map pointed by the root
if(!node.children.containsKey(letter))
{
//create a new node
TreeNode newNode = new TreeNode();
//put this character to child of root node
//root node has children hashmap ,hence in the hashmap put this child mapping to another hashmap
node.children.put(letter,newNode);
}
//if the node points to a hashmap that contains the letter skip it and increment the node pointer
node = node.children.get(letter);
}
//after inserion of substring node will be pointing to last character of substring
//then put the endsymobol mapping to null
node.children.put(suffixTree.endSymbol,null);
}
//Time Complexity O(m) Space Complexity O(1)
//m = string for which we r performing a check
public static boolean containsSuffix(String str)
{
//get the root
TreeNode node = suffixTree.root;
for(int i = 0 ; i < str.length() ; i++)
{
char letter = str.charAt(i);
//hashmap pointed by root must contain the first letter of suffix else its not a suffix
if(!node.children.containsKey(letter))
{
return false;
}
//kepp on traversing the branch of tree
node = node.children.get(letter);
}
//after reaching here check if it is the ned of branch
//if its is the ned of branch then given string is a valisd suffix
//else it is not a valid suffix rather another substring
return node.children.containsKey(suffixTree.endSymbol) ? true : false;
}
public static void main(String[] args)
{
String st = "babc";
suffixTree tree = new suffixTree(st);
System.out.println("Given String "+st);
System.out.println("is abc is a suffix "+ containsSuffix("abc"));
System.out.println("is bab is a suffix "+ containsSuffix("bab"));
}
}