fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <memory>
  4. using namespace std;
  5.  
  6. struct node {
  7. char ch;
  8. node *link[26];
  9. node(char c=0) : link(), ch(c) {}
  10. ~node() { for (int i=0;i<26; i++) delete link[i]; }
  11. };
  12.  
  13. node head;
  14.  
  15. void addString(node *n, string s) {
  16. if (!s.length()) return;
  17.  
  18. size_t c = tolower(s[0]);
  19. if (c<'a' || c>'z') return;
  20. if (!n -> link[c-'a']) {
  21. n -> link[c-'a'] = new node(c);
  22. }
  23. addString(n -> link[c-'a'], s.substr(1));
  24. }
  25.  
  26. void disp(node *n, int x=0) {
  27. cout <<setw(x*2)<<" "<< n->ch<<endl;
  28. for (int i=0; i<26; i++)
  29. if (n->link[i])
  30. disp(n->link[i],x+1);
  31. }
  32.  
  33. int main(){
  34. addString(&head, "red");
  35. disp(&head);
  36. return 0;
  37. }
  38.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
 
  r
    e
      d