二元查找树转换为它的镜像

题目:输入一棵二元查找树,将该树转换为它的镜像,即在转换后的二元查找树中,左子树的结点都大于右子树的结点。

要求:用递归和循环两种方法完成树的镜像转换。

举例:

        8                     8
      /   \       转换       /   \
     6     10     -->      10    6
    /  \   / \            /  \   / \
   5    7 9   11         11   9 7   5

二叉树定义的结点为:

struct BSTreeNode
{
    int m_nValue;
    BSTreeNode *m_pLeft;
    BSTreeNode *m_pRight;
};

答:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <stack>

using namespace std;

/*      8                     8
      /   \       转换       /   \
     6     10     -->      10    6
     /  \   / \            /  \   / \
   5    7 9   11         11   9 7   5
*/

struct BSTreeNode
{
    int m_nValue;
    BSTreeNode *m_pLeft;
    BSTreeNode *m_pRight;
};

//递归转换
void RecursionConversion(BSTreeNode *&pRoot)
{
    if (NULL == pRoot)
    {
        return;
    }
    BSTreeNode *pTmp = pRoot->m_pLeft;
    pRoot->m_pLeft = pRoot->m_pRight;
    pRoot->m_pRight = pTmp;

    if (NULL != pRoot->m_pLeft)
    {
        RecursionConversion(pRoot->m_pLeft);
    }

    if (NULL != pRoot->m_pRight)
    {
        RecursionConversion(pRoot->m_pRight);
    }
}

//非递归转换
void Conversion(BSTreeNode *&pRoot)
{
    if (NULL == pRoot)
    {
        return;
    }
    stack<BSTreeNode*> s;
    BSTreeNode *pNode = pRoot;
    BSTreeNode *pTmp = NULL;
    s.push(pNode);
    while (!s.empty())
    {
        pNode = s.top();
        s.pop();
        pTmp = pNode->m_pLeft;
        pNode->m_pLeft = pNode->m_pRight;
        pNode->m_pRight = pTmp;

        if (NULL != pNode->m_pLeft)
        {
            s.push(pNode->m_pLeft);
        }
        if (NULL != pNode->m_pRight)
        {
            s.push(pNode->m_pRight);
        }
    }
}

//辅助函数
//创建二元二叉树
void CreateBitree(BSTreeNode *&pNode, fstream &fin)
{
    int dat;
    fin>>dat;
    if(dat==0)
    {
        pNode = NULL;
    }
    else 
    {
        pNode = new BSTreeNode();
        pNode->m_nValue=dat;      
        CreateBitree(pNode->m_pLeft, fin);      
        CreateBitree(pNode->m_pRight, fin);
    }
}

//前序递归遍历
void PreRecurTraversal(BSTreeNode *pRoot)
{
    if (NULL != pRoot)
    {
        cout<<pRoot->m_nValue<<'\t';
        PreRecurTraversal(pRoot->m_pLeft);
        PreRecurTraversal(pRoot->m_pRight);
    }
}

//中序递归遍历
void MidRecurTraversal(BSTreeNode *pRoot)
{
    if (NULL != pRoot)
    {
        MidRecurTraversal(pRoot->m_pLeft);
        cout<<pRoot->m_nValue<<'\t';
        MidRecurTraversal(pRoot->m_pRight);
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    fstream fin("tree.txt");
    BSTreeNode *pRoot = NULL;
    CreateBitree(pRoot, fin);
    cout<<"**********转换前的前序和中序遍历***********"<<endl;
    cout<<"前序: ";
    PreRecurTraversal(pRoot);
    cout<<endl<<"后序: ";
    MidRecurTraversal(pRoot);

    RecursionConversion(pRoot);
    cout<<endl<<"********递归转换后的前序和中序遍历*********"<<endl;
    cout<<"前序: ";
    PreRecurTraversal(pRoot);
    cout<<endl<<"后序: ";
    MidRecurTraversal(pRoot);

    Conversion(pRoot);
    cout<<endl<<"*******非递归转换后的前序和中序遍历********"<<endl;
    cout<<"前序: ";
    PreRecurTraversal(pRoot);
    cout<<endl<<"后序: ";
    MidRecurTraversal(pRoot);

    cout<<endl;
    return 0;
}

运行界面如下:

建造二叉树用到的tree.txt为:

8 6 5 0 0 7 0 0 10 9 0 0 11 0 0
原文地址:https://www.cnblogs.com/venow/p/2651438.html