Last active
August 29, 2015 14:21
-
-
Save gmfc/b655a00631ebb42e549a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package percursos; | |
| public class Percursos { | |
| public void preOrdemRec(Node root) { | |
| if (root != null) { | |
| visit(root); | |
| preOrdemRec(root.getNoE()); | |
| preOrdemRec(root.getNoD()); | |
| } | |
| } | |
| public void posOrdem(Node root) { | |
| if (root != null) { | |
| posOrdem(root.getNoE()); | |
| posOrdem(root.getNoD()); | |
| visit(root); | |
| } | |
| } | |
| public void iterativePreOrdem(Node root) { | |
| Node no = root; | |
| Stack travStack = new NodeStack(); | |
| if (no != null) { | |
| travStack.push(no); | |
| while (!travStack.isEmpty()) { | |
| no = (Node) travStack.pop(); | |
| visit(no); | |
| if (no.getNoD() != null) | |
| travStack.push(no.getNoD()); | |
| if (no.getNoE() != null) | |
| travStack.push(no.getNoE()); | |
| } | |
| } | |
| } | |
| public void iterativePostorder(Node root) { | |
| Node p = root, q = root; | |
| Stack travStack = new NodeStack(); | |
| while (p != null) { | |
| for (; p.getNoE() != null; p = p.getNoE()) | |
| travStack.push(p); | |
| while (p != null && (p.getNoD() == null || p.getNoD() == q)) { | |
| visit(p); | |
| q = p; | |
| if (travStack.isEmpty()) | |
| return; | |
| p = (Node) travStack.pop(); | |
| } | |
| travStack.push(p); | |
| p = p.getNoD(); | |
| } | |
| } | |
| private void visit(Node no) { | |
| System.out.print(no.getInfo() + " "); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment