二叉树的前序、中序、后序遍历、树的深度、左右子树互换

一、前序排列

void PreorderTraversal(Tree T)//前序遍历
{
    if( T )
    {
        cout<<T->index <<"  "<<T->data<<endl;//
        PreorderTraversal(T->pLChild);//
        PreorderTraversal(T->pRChild);//
    }
}

二、中序遍历

void InorderTraversal(Tree T)//中序遍历
{
    if( T )
    {
        InorderTraversal(T->pLChild);//
        cout<<T->index <<"  "<<T->data<<endl;//
        InorderTraversal(T->pRChild);//
    }
}

三、后序遍历

void PostorderTraversal(Tree T)//后序遍历
{
    if( T )
    {
        PostorderTraversal(T->pLChild);//
        PostorderTraversal(T->pRChild);//
        cout<<T->index <<"  "<<T->data<<endl;//
    }
}

 四、求树的深度

int GetHeight(Tree T)
{
   int LH,RH;
   if(!T)
   {
       return 0;
   }
   else
   {
       LH = GetHeight(T->Left);
       RH = GetHeight(T->Right);
       return LH > RH? ++LH : ++RH;
   }
}

 

 
posted @ 2019-11-13 21:35  慕尘墨染  阅读(581)  评论(0)    收藏  举报