PAT 1043 二叉查找树

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

const int SIZE = 5000;

int g_preOrder[SIZE];
int g_preOrderImg[SIZE];
int g_postOrder[SIZE];
int g_index = 0;

struct BSNode
{
    BSNode *left;
    BSNode *right;
    int key;
};

//在一颗二叉查找树中添加一个节点。isImg为true,则表示生成的
//是二叉查找树的镜像;
void insertNode(BSNode* &p, int key,bool isImg)
{
    if(p == NULL)
    {
        p = new BSNode();
        p->left = NULL;
        p->right = NULL;
        p->key = key;
        return;
    }
    else
    {
        if(isImg)
        {
            if(p->key <= key)
                insertNode(p->left, key, isImg);
            else
                insertNode(p->right, key, isImg);
        }
        else
        {
            if(p->key > key)
                insertNode(p->left, key, isImg);
            else
                insertNode(p->right, key, isImg);
        }
    }
    
}


//创建一颗二叉查找树或其镜像。
void createBST(BSNode* &root, int arr[], int n,bool isImg)
{
    for(int i=0; i<n; i++)
    {
        insertNode(root, arr[i], isImg);
    }
}

void preOrder(BSNode* root,bool isImg)
{
    if(root == NULL)
        return;
    if(isImg)
        g_preOrderImg[g_index++] = root->key;
    else
        g_preOrder[g_index++] = root->key;
    if(root->left)
        preOrder(root->left,isImg);
    if(root->right)
        preOrder(root->right,isImg);
}

//二叉查找树或其镜像的输出结果。
void postOrder(BSNode* root)
{
    if(root == NULL)
        return;
    if(root->left)
        postOrder(root->left);
    if(root->right)
        postOrder(root->right);
    g_postOrder[g_index++] = root->key;
}

//检查两个数组的内容是否相同。
bool checkIsSame(int a[],int b[], int len)
{
    int i = 0;
    for(i=0; i<len; i++)
        if(a[i] != b[i])
            return false;
    return true;
}

//输出后序遍历的结果。
void printRes(int arr[], int len)
{
    bool isFirst = true;
    int i = 0;
    for(i=0; i<len; i++)
    {
        if(isFirst)
        {
            cout<<arr[i];
            isFirst = false;
        }
        else
        {    
            cout<<" "<<arr[i];
        }
        if(i == len-1)//最后一个元素要输出换行符
            cout<<endl;
    }
}

//释放一颗二叉树的节点
void freeNodes(BSNode *root)
{
    if(NULL == root->left && NULL == root->right)
        delete root;
    else if(root->left)
        freeNodes(root->left);
    else if(root->right)
        freeNodes(root->right);
}


int main()
{
    int i,j,N,arr[1003];
    while(cin>>N)
    {
        BSNode *root = NULL;
        BSNode *rootImg = NULL;
        for(i=0; i<N; i++)
        {
            cin>>arr[i];
        }
        createBST(root, arr, N, false);
        createBST(rootImg, arr, N, true);
        g_index = 0;
        preOrder(root,false);//得到二叉查找树的先序遍历结果
        if(checkIsSame(arr, g_preOrder, N))
        {
            cout<<"YES"<<endl;
            g_index = 0;
            postOrder(root);
            printRes(g_postOrder, N);
        }    
        else 
        {
            g_index = 0;
            preOrder(rootImg, true);//得到二叉查找树镜像的先序遍历结果
            if(checkIsSame(arr, g_preOrderImg, N))
            {
                cout<<"YES"<<endl;
                g_index = 0;
                postOrder(rootImg);
                printRes(g_postOrder,N);
            }
            else
                cout<<"NO"<<endl;            
        }
        
        freeNodes(root);
        freeNodes(rootImg);
    }
    
    return 0;
}
posted @ 2012-11-26 22:06  Frank@609  Views(876)  Comments(0Edit  收藏  举报