fork(4) download
  1. #include<iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. struct SuffixTreeNode{
  6. char c;
  7. SuffixTreeNode* one;
  8. SuffixTreeNode* two;
  9. SuffixTreeNode* three;
  10. SuffixTreeNode* four;
  11. SuffixTreeNode(char c=0) : c(c), one(nullptr), two(nullptr), three(nullptr), four(nullptr) { }
  12. };
  13.  
  14. SuffixTreeNode* Insert(SuffixTreeNode* root,char ch){
  15. if (! root){
  16. root=new SuffixTreeNode(ch);
  17. }
  18. else if(ch=='a'){
  19. root->one=Insert(root->one,ch);
  20. }
  21. else if(ch=='c'){
  22. root->two=Insert(root->two,ch);
  23. }
  24. else if(ch=='g'){
  25. root->three=Insert(root->three,ch);
  26. }
  27. else if(ch=='t') {
  28. root->four=Insert(root->four,ch);
  29. }
  30.  
  31. return root;
  32. }
  33.  
  34. bool Search(SuffixTreeNode* root, int data){
  35. cout << (char)data<<"=="<<root->c<<"?"<<endl;
  36. if(!root) return false;
  37. else if (root->c==data) return true;
  38. else if (data=='a')return Search(root->one,data);
  39. else if (data=='c')return Search(root->two,data);
  40. else if (data=='g')return Search(root->three,data);
  41. else return Search(root->four,data);
  42. }
  43.  
  44.  
  45. void Print(SuffixTreeNode* root, int x=0){
  46. for (int i=0; i<x; i++) cout<<" ";
  47. if(!root) cout <<"X"<<endl;
  48. else {
  49. cout << root->c <<endl;
  50. Print (root->one, x+2);
  51. Print(root->two,x+2);
  52. Print(root->three,x+2);
  53. Print(root->four,x+2);
  54. }
  55. }
  56.  
  57. int main(){
  58. SuffixTreeNode* root=nullptr;
  59. char str;
  60. root=Insert(root,'a');
  61. root=Insert(root,'c');
  62. root=Insert(root,'c');
  63. root=Insert(root,'t');
  64. root=Insert(root,'a');
  65. root=Insert(root,'g');
  66. cout<<"Enter character to be searched\n";
  67. cin>>str;
  68.  
  69. cout << "Searching "<<str<<" in:"<<endl;
  70. Print (root);
  71. if(Search(root,str))
  72. cout<<"Found\n";
  73. else cout<<"Not found\n";
  74. }
  75.  
Success #stdin #stdout 0s 3420KB
stdin
g
stdout
Enter character to be searched
Searching g in:
a
  a
    X
    X
    X
    X
  c
    X
    c
      X
      X
      X
      X
    X
    X
  g
    X
    X
    X
    X
  t
    X
    X
    X
    X
g==a?
g==g?
Found