#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;
}