[ create a new paste ] login | about

Link: http://codepad.org/Fc2LLglX    [ raw code | output | fork ]

C++, pasted on Jun 28:
#include <iostream>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */


using namespace std;
struct Node 
{
    int data;
    Node *L, *R;      
};


void showTree( Node *mover )
{
 
    if( mover != NULL )
    {
        showTree( mover->L );
        printf("%d\n",mover->data);
        showTree( mover->R );    
    }   
}







int main(int argc, char** argv) {
	
	int x[]={5,3,9,1,6,4,8,2,7};
	
	
 Node *root = new Node(), *mover = root;

//        cin >> root->data;
    root->data=x[0];
      
     root->L = NULL;
     root->R = NULL;
     
     

    
     
	for(int i=1;i<=8;i++) { 
     		 
     
          bool end = false;
          mover = root;
          while( !end )
          {
             if ( x[i] > mover->data )
             {
                 if ( mover->R == NULL )
                 {
                        mover->R = new Node();
                        end = true;
                 }
                 mover = mover->R;
             }
             else
             {
                 if ( mover->L == NULL )
                 {
                       mover->L = new Node();
                       end = true;
                 }
                 mover = mover->L; 
             }
          }    
          mover->data = x[i];
           
          
     }
	
	printf("\n中序走訪:\n\n"); 
          showTree( root );
	
	
	
	return 0;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12

中序走訪:

1
2
3
4
5
6
7
8
9


Create a new paste based on this one


Comments: