Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void preorderT(Node root){
- if (root==null) return;
- root.visit();
- preorderT(root.left);
- preorderT(root.right);
- }
- void postorderT(Node root){
- if (root==null) return;
- preorderT(root.left);
- preorderT(root.right);
- root.visit();
- }
- void inorderT(Node root){
- if (root==null) return;
- preorderT(root.left);
- root.visit();
- preorderT(root.right);
- }
- /* Non-Recursive Implementation of Preorder Traversal */
- public class Stack{
- public void push(Node node) {...}
- public Node pop() {...}
- public boolean isEmpty() {...}
- }
- void preorderTNR(Node root){
- Stack stack=new Stack();
- stack.push(root);
- //Note that I had forgotten the () after isEmpty in the video
- while(!stack.isEmpty()){
- Node n=stack.pop();
- n.visit();
- if (n.right!=null)stack.push(n.right);
- if (n.left!=null)stack.push(n.left);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement