Advertisement
UniQuet0p1

Untitled

Oct 31st, 2021
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. @Override
  2. public String toString() {
  3. StringBuffer b = new StringBuffer();
  4. List<Tnode> usedList = new ArrayList<>();
  5. Tnode current = this;
  6.  
  7. do {
  8. if (!usedList.contains(current)) {
  9. b.append(current.getName());
  10. usedList.add(current);
  11. }
  12.  
  13. Tnode child = current.getFirstChild();
  14. Tnode sibling = current.getNextSibling();
  15.  
  16. if (child != null) {
  17. if (child.getNextSibling() != null || child.getFirstChild() != null) {
  18. current = current.getFirstChild();
  19. } else {
  20. current.setFirstChild(null);
  21. current = this;
  22. }
  23. if (!usedList.contains(current)) {
  24. b.append("(");
  25. }
  26. } else if (null != sibling) {
  27. if (sibling.getFirstChild() != null) {
  28. current = current.getNextSibling();
  29. } else {
  30. current.setNextSibling(null);
  31. if (!usedList.contains(sibling)) b.append(",").append(sibling.getName());
  32. current = this;
  33. b.append(")");
  34. }
  35. if (!usedList.contains(current)) {
  36. b.append(",");
  37. }
  38. }
  39. } while (this.getFirstChild() != null);
  40. return b.toString();
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement