1:深度優先
1.1:前序遍歷
- Visit the root.
- Traverse the left subtree.
- Traverse the right subtree.
preorder(node) if node == null then return visit(node) preorder(node.left) preorder(node.right) |
iterativePreorder(node) parentStack = empty stack parentStack.push(null) top = node while ( top != null ) visit( top ) if ( top.right != null ) parentStack.push(top.right) if ( top.left != null ) parentStack.push(top.left) top = parentStack.top(); parentStack.pop(); |
1.2:中序遍歷
- Traverse the left subtree.
- Visit root.
- Traverse the right subtree.
1.3:後序遍歷
1.4:java代碼實現三種遍歷
/ \
5 12
/ \
4 7
- public class Preorder{
- /*******************************遞歸法****************************/
- //遞歸前序遍歷 print:10,5,4,7,12
- public static void preorder(TNode node){
- if(node!=null){
- System.out.println(node.getValue()) ;
- preorder(node.getLeft()) ;
- preorder(node.getRight()) ;
- }
- }
- //遞歸中序遍歷 print: 4,5,7,10,12
- public static void inorder(TNode node){
- if(node!=null){
- inorder(node.getLeft()) ;
- System.out.println(node.getValue()) ;
- inorder(node.getRight()) ;
- }
- }
- //遞歸後序遍歷 print: 4,7,5,12,10
- public static void postorder(TNode node){
- if(node!=null){
- postorder(node.getLeft()) ;
- postorder(node.getRight()) ;
- System.out.println(node.getValue()) ;
- }
- }
- /*******************************非遞歸*****************************/
- //非遞歸前序遍歷 print:10,5,4,7,12
- public static void iterativePreorder(TNode node){
- MyStack parentStack = new MyStack() ;
- parentStack.push(node) ;
- while(!parentStack.isEmpty()){
- TNode temp = parentStack.getFirst().getValue() ;
- System.out.println(temp.getValue()) ;
- parentStack.pop() ;
- if(temp.getRight()!=null){ parentStack.push(temp.getRight()) ;}
- if(temp.getLeft()!=null){ parentStack.push(temp.getLeft()) ;}
- }
- }
- //非遞歸中序遍歷 print: 4,5,7,10,12
- public static void iterativeInorder(TNode node){
- MyStack mystack = new MyStack() ;
- while(!mystack.isEmpty()||node!=null){
- if(node!=null){
- mystack.push(node) ;
- node = node.getLeft() ;
- }else{
- node = mystack.pop().getValue() ;
- System.out.println(node.getValue()) ;
- node = node.getRight() ;
- }
- }
- }
- //非遞歸後序遍歷 print: 4,7,5,12,10
- public static void iterativePostorder(TNode node){
- MyStack mystack = new MyStack() ;
- TNode temp = null ;
- TNode peeknode = null ;
- while(!mystack.isEmpty()||node!=null){
- if(node!=null){
- mystack.push(node) ;
- node = node.getLeft() ;
- }else{
- peeknode = mystack.getFirst().getValue() ;
- if(peeknode.getRight()!=null&&temp!=peeknode.getRight()){
- node = peeknode.getRight() ;
- }else{
- mystack.pop() ;
- System.out.println(peeknode.getValue()) ;
- temp = peeknode ;
- }
- }
- }
- }
- public static void main(String args[]){
- MyTree mytree = new MyTree() ;
- mytree.insert(10) ;
- mytree.insert(5);
- mytree.insert(12);
- mytree.insert(4);
- mytree.insert(7);
- preorder(mytree.getRoot()) ;
- inorder(mytree.getRoot()) ;
- postorder(mytree.getRoot()) ;
- iterativePreorder(mytree.getRoot()) ;
- iterativeInorder(mytree.getRoot()) ;
- iterativePostorder(mytree.getRoot()) ;
- }
- }
- /*****************二叉樹的實現****************/
- class TNode{
- private int value ;
- private TNode left ;
- private TNode right ;
- public TNode(int value){ this.value = value ;}
- public void setLeft(int value){ this.left = new TNode(value) ;}
- public void setRight(int value){ this.right = new TNode(value) ;}
- public TNode getLeft(){ return this.left ;}
- public TNode getRight(){ return this.right ;}
- public int getValue(){ return this.value ;}
- }
- class MyTree{
- private TNode root ;
- public void setRoot(int value){ this.root = new TNode(value) ;}
- public TNode getRoot(){ return this.root ;}
- public void insert(int value){
- TNode x = this.root ;
- TNode y = null ;
- while(x!=null){
- y = x ;
- if(value<x.getValue()){ x = x.getLeft() ;}
- else{ x = x.getRight() ;}
- }
- if(y == null){ setRoot(value) ;}
- else if(value<y.getValue())
- { y.setLeft(value) ;}
- else{ y.setRight(value) ;}
- }
- }
- /*****************棧的實現****************/
- class StackNode{
- public TNode item ;
- public StackNode next ;
- public StackNode(){ ;}
- public StackNode(TNode item){ this.item = item ;}
- public TNode getValue(){ return item ;}
- }
- class MyStack{
- int N ; //size of myStack
- StackNode first ; // the top of stack
- //size of mystack
- public int size(){ return N ;}
- //empty or not
- public boolean isEmpty(){ return N==0 ;}
- public StackNode getFirst(){ return this.first ;}
- public void push(TNode item){
- if(isEmpty()){
- first = new StackNode() ;
- first.item = item;
- N++;
- }else{
- StackNode oldfirst = first;
- first = new StackNode() ;
- first.item = item;
- first.next = oldfirst;
- N++;
- }
- }
- public StackNode pop(){
- StackNode top = first ;
- first = first.next; // delete first node
- N--;
- return top ;
- }
- }