Imagine you write a long word and you want a magic map of every smaller piece hiding inside it. You take the word and chop off the front letter again and again — banana, anana, nana, … — and feed all these tails into a tree where words that start the same way share the same branch. Now if a friend asks "is nan in there?", you just walk: n → a → n, and if you can finish the path, yes it's inside! Counting how many little leaf-tags hang below tells you how many times it appears. The $ at the end is a tiny flag that makes sure every tail gets its own leaf so nothing hides inside another.
All suffixes of the string S\$$.
Why append a unique terminal `? ::: So no suffix is a prefix of another; every suffix then ends at its own leaf, guaranteeing exactly $n+1$ leaves. How many leaves does a suffix tree of $S\$ have? ::: Exactly $n+1$ (one per suffix, including the ‘suffix).Constraintoninternalnodes(besidesroot)?:::Eachhasatleast2children(theyarebranchingpoints).WhyistotalspaceO(n)andnotO(n^2)?:::Edgelabelsarestoredasindexpairs(i,j)intoS,notascopiedsubstrings;withO(n)edgesthat′sO(n)space.TimetotestifpatternPisasubstring?:::O(\lvert P\rvert)—justwalkdownmatchingP′scharacters.HowdoyoucountoccurrencesofPaftermatchingit?:::Countthenumberofleavesinthesubtreebelowthematchpoint.Whatdoesthedeepestinternalnode(bychardepth)giveyou?:::ThelongestrepeatedsubstringofS.Whymustedgesoutofanodestartwithdistinctcharacters?:::Sowalkingmatchesapatterndeterministically—atmostoneedgetofollowpernextcharacter.SubstringofSequalsaprefixofwhat?:::AprefixofsomesuffixofS$ — this is why a tree of suffixes answers substring queries.
Suffix tree ka core idea simple hai: kisi string S ke saare suffixes (peeche se chhote hote tails — banana, anana, nana...) ko ek tree me daal do, jaha same se shuru hone wale tails same branch share karte hain. Yaad rakho: koi bhi substring asal me kisi suffix ka prefix hi hota hai. Isliye agar saare suffixes tree me hain, to "kya P string ke andar hai?" ka jawaab bas root se P ke letters follow karke mil jaata hai — bilkul O(∣P∣) time me, chahe S kitni badi ho.
Hum end me ek special $ lagate hain jo alphabet me nahi hai. Kyun? Taaki koi suffix doosre ka prefix na ban jaaye (jaise a aur ana). $ ke baad har suffix apne alag leaf pe khatam hota hai, to exactly n+1 leaves milte hain — clean structure.
Space ki tension mat lo. Edge labels ko hum literal text ke roop me store nahi karte; sirf do integer (i,j) rakhte hain jo bolte hain "S[i..j]". Isliye poora tree O(n) memory me aata hai, O(n2) nahi. Build karne ke liye Ukkonen's algorithm linear O(n) time leta hai.
Yeh kyun matter karta hai? Ek hi structure se bahut saare problems turant solve hote hain: pattern search, kitni baar P aaya (subtree ke leaves gino), aur longest repeated substring (sabse deep internal node ka path). Interviews aur real bioinformatics (DNA matching) dono me yeh power-tool hai. Aaj practice me log aksar suffix array prefer karte hain kyunki memory kam lagti hai, par concept yahi suffix tree wala hai.