Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Pre-order traversal med brug af Stack. Dette er en depth-first gennemgang af træet, hvor man starter med <root>,
- // efterfulgt af <left sub-tree> og til sidst <right sub-tree>. DLR
- public ArrayList<T> printPreOrder() {
- ArrayList<T> returnList = new ArrayList<>();
- Node<T> rootNode = this.root;
- Stack<Node<T>> depthStack = new Stack<>();
- depthStack.push(rootNode);
- while (!depthStack.isEmpty()) {
- returnList.add(rootNode.getValue());
- rootNode = depthStack.pop();
- if (rootNode.hasChildRight()) {
- depthStack.push(rootNode.getChildRight());
- }
- if (rootNode.hasChildLeft()) {
- depthStack.push(rootNode.getChildLeft());
- }
- }
- return returnList;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement