daily pastebin goal
56%
SHARE
TWEET

Untitled

a guest Jun 14th, 2018 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top