チェーンシート-二叉木

4611 ワード

チェーンシート-二叉木

//========================================//
//    :                     //
//Writtrn By HEWEI                        //
//2011 05 31                              //
//========================================//

#include<iostream>
using namespace std;

#define  MAX 20	
//-----------------------------------------//
//----------      -------------------//
//-----------------------------------------//
struct tree
{
	struct tree *left;  //    
	int Data;           //  
	struct tree *right; //    
};

typedef struct tree treenode;
typedef treenode *b_tree;

//-----------------------------------------//
//-----------        --------------//
//-----------------------------------------//
b_tree Insert_Tree(b_tree root,int data)
{
	 b_tree New;
	 //b_tree Pointer;//    
	 b_tree currentnode;  //    
	 //b_tree parentnode;  //   

	 //allocate memory to New
	 New = new treenode[sizeof(treenode)];
	//       
	 New->left = NULL;
	 New->Data = data;
	 New->right = NULL;

    //      ,    
	//     
    if(root == NULL)
	{
     root = New;
	}
	else
	{
		//     root    ,       
        currentnode = root ;//    
		while(currentnode != NULL)
		{
			//     
			if (data <= currentnode->Data)
			{
                //        
				if(currentnode->left != NULL)//    
				{
					currentnode = currentnode->left;  //         
				}
				else                          //     
				{//       
					//parentnode = currentnode;
				    //currentnode = currentnode->left;
                                      break;
				}
			}
			else//   
			{
				//     
					//        
					if(currentnode->right != NULL)//    
					{
						//parentnode = currentnode;
						currentnode = currentnode->right;  //         
					}
					else                          //     
					{
						//parentnode = currentnode;
						//parentnode->right = New;    //       
						//currentnode = currentnode->right;
                                                 break;
					}
			}
		}
		//if(data <= parentnode->Data) //   
                //     parentnode->left = New;
		//else
		//	parentnode->right = New;
		if(data <= currentnode ->Data) //   
                     currentnode ->left = New;
		else
			currentnode ->right = New;
	}
	return root;
}

//------------------------------------------//
//-------------      -----------------//
//------------------------------------------//
b_tree Create_Tree(int len,int *pArray)
{
	int i;
   //      
	b_tree root = NULL;

	//      
    for(i=0; i< len; i++)
	{
		root = Insert_Tree(root,pArray[i]);
	}
	return root;
}

//-------------------------------------------//
//-----------    ------------------------//
//-------------------------------------------//
void Store_Data(int nMaxIndex,int *pArray)
{
   int i ;
   int temp;
   for(i = 0; i <nMaxIndex; i++)
   {
	   cout << "Please Input the Data => ";
	   cin >> temp;
       pArray[i] = temp;
   }
}
//-------------------------------------------//
//------------         ------------//
//-------------------------------------------//
void Print_Tree(b_tree point)
{
if(point != NULL)
{
	cout <<point->Data;
	Print_Tree(point->left);//     
	Print_Tree(point->right);//     
}
}


//--------------------------------------------//
//---------------   -----------------------//
//--------------------------------------------//
int main(void)
{
	b_tree root = NULL; //     
	int i,index,capacity;
	int value;
	int nodelist[MAX];

	//        
	cout <<"Please Input the capacity : ";
	cin >> capacity;
	cout << endl;
	Store_Data(capacity,nodelist);
	//     
    root = Create_Tree(capacity,nodelist);
	//     
  Print_Tree(root);
	return 0;
}