Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- public String toString() {
- StringBuffer b = new StringBuffer();
- List<Tnode> usedList = new ArrayList<>();
- Tnode current = this;
- do {
- if (!usedList.contains(current)) {
- b.append(current.getName());
- usedList.add(current);
- }
- Tnode child = current.getFirstChild();
- Tnode sibling = current.getNextSibling();
- if (child != null) {
- if (child.getNextSibling() != null || child.getFirstChild() != null) {
- current = current.getFirstChild();
- } else {
- current.setFirstChild(null);
- current = this;
- }
- if (!usedList.contains(current)) {
- b.append("(");
- }
- } else if (null != sibling) {
- if (sibling.getFirstChild() != null) {
- current = current.getNextSibling();
- } else {
- current.setNextSibling(null);
- if (!usedList.contains(sibling)) b.append(",").append(sibling.getName());
- current = this;
- b.append(")");
- }
- if (!usedList.contains(current)) {
- b.append(",");
- }
- }
- } while (this.getFirstChild() != null);
- return b.toString();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement