Advertisement
whiplk

Varredura em árvore binária

Oct 16th, 2014
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1. @Override
  2.     public void preOrder() throws EmptyTreeException {
  3.         if ( isEmpty () )
  4.             throw new EmptyTreeException( this.getClass().getName() );
  5.        
  6.         preOrder ( getRoot () );
  7.     }
  8.    
  9.     private void preOrder ( BinaryTreeNode<type> node ) {
  10.         node.visit ();
  11.  
  12.         if ( node.hasLeftSon () )
  13.             preOrder ( node.getLeftSon () );
  14.        
  15.         if ( node.hasRightSon () )
  16.             preOrder ( node.getRightSon () );
  17.     }
  18.  
  19.     @Override
  20.     public void inOrder() throws EmptyTreeException {
  21.         if ( isEmpty () )
  22.             throw new EmptyTreeException( this.getClass().getName() );
  23.        
  24.         inOrder ( getRoot () );
  25.     }
  26.    
  27.     private void inOrder ( BinaryTreeNode<type> node ) {
  28.         if ( node.hasLeftSon () )
  29.             inOrder ( node.getLeftSon () );
  30.        
  31.         node.visit ();
  32.        
  33.         if ( node.hasRightSon () )
  34.             inOrder ( node.getRightSon () );
  35.     }
  36.  
  37.     @Override
  38.     public void postOrder() throws EmptyTreeException {
  39.         if ( isEmpty () )
  40.             throw new EmptyTreeException( this.getClass().getName() );
  41.        
  42.         postOrder ( getRoot () );
  43.     }
  44.    
  45.     private void postOrder ( BinaryTreeNode<type> node ) {
  46.         if ( node.hasLeftSon() )
  47.             postOrder ( node.getLeftSon () );
  48.        
  49.         if ( node.hasRightSon() )
  50.             postOrder ( node.getRightSon () );
  51.        
  52.         node.visit ();
  53.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement