Four kinds of binary tree traversal chain store

  1. void InorderTraversal( BinTree BT )
  2. {
  3.     if( BT ) {
  4.         InorderTraversal( BT->Left );
  5.         / * Access to BT is assumed here that the node is the print data * /
  6.         the printf ( "% D" , BT-> the Data);  / * assume integer data * /
  7.         InorderTraversal( BT->Right );
  8.     }
  9. }
  10.  
  11. void PreorderTraversal( BinTree BT )
  12. {
  13.     if( BT ) {
  14.         printf("%d ", BT->Data );
  15.         PreorderTraversal( BT->Left );
  16.         PreorderTraversal( BT->Right );
  17.     }
  18. }
  19.  
  20. void PostorderTraversal( BinTree BT )
  21. {
  22.     if( BT ) {
  23.         PostorderTraversal( BT->Left );
  24.         PostorderTraversal( BT->Right );
  25.         printf("%d ", BT->Data);
  26.     }
  27. }
  28.  
  29. void LevelorderTraversal ( BinTree BT )
  30.     Queue Q; 
  31.     BinTree T;
  32.  
  33.     IF  (! BT)  return / * if empty tree directly return * /
  34.      
  35.     CreatQueue = Q ();  / * Create an empty queue Q * /
  36.     AddQ( Q, BT );
  37.     while ( !IsEmpty(Q) ) {
  38.         T = DeleteQ( Q );
  39.         the printf ( "% D" , T-> the Data);  / * remove the access node queue * /
  40.         if ( T->Left )   AddQ( Q, T->Left );
  41.         if ( T->Right )  AddQ( Q, T->Right );
  42.     }
  43. }

Guess you like

Origin www.cnblogs.com/lzdxh027/p/11323534.html