Advertisement
Guest User

Untitled

a guest
Jun 14th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. // Pre-order traversal med brug af Stack. Dette er en depth-first gennemgang af træet, hvor man starter med <root>,
  2. // efterfulgt af <left sub-tree> og til sidst <right sub-tree>. DLR
  3. public ArrayList<T> printPreOrder() {
  4. ArrayList<T> returnList = new ArrayList<>();
  5. Node<T> rootNode = this.root;
  6. Stack<Node<T>> depthStack = new Stack<>();
  7.  
  8. depthStack.push(rootNode);
  9. while (!depthStack.isEmpty()) {
  10. returnList.add(rootNode.getValue());
  11. rootNode = depthStack.pop();
  12.  
  13. if (rootNode.hasChildRight()) {
  14. depthStack.push(rootNode.getChildRight());
  15. }
  16.  
  17. if (rootNode.hasChildLeft()) {
  18. depthStack.push(rootNode.getChildLeft());
  19. }
  20. }
  21. return returnList;
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement