2.4 排序二叉树(包括中序遍历在内的两种遍历结果可以唯一确定一棵二叉树)

一、二叉排序树

1、题目和要求

时间限制:1s,内存限制:32MB,特殊判题:否
在这里插入图片描述

2、总结

返回Node *和没有返回值两种方式相比,第一种方式定义的中间变量少了很多,代码结构更清晰了。尤其是if(root == NULL){node[pos].value = v; root = &node[pos++];}这段代码用的很妙。

3、代码
3.1 前序、中序、后序遍历
void preOrder(Node *node)
{
    cout<<node->value;
    if(node->lchild!=NULL)
    {
        preOrder(node->lchild);
    }
    if(node->rchild!=NULL)
    {
        preOrder(node->rchild);
    }
}

void inOrder(Node *node)
{
    if(node->lchild!=NULL)
    {
        inOrder(node->lchild);
    }
    cout<<node->value;
    if(node->rchild!=NULL)
    {
        inOrder(node->rchild);
    }
}

void postOrder(Node *node)
{
    if(node->lchild!=NULL)
    {
        postOrder(node->lchild);
    }
    if(node->rchild!=NULL)
    {
        postOrder(node->rchild);
    }
    cout<<node->value;
}

3.2 返回值为 void
#include <iostream>
using namespace std;

#define N 100
class Node
{
public:
    int value;
    Node *lchild;
    Node *rchild;

    Node()
    {
        lchild=NULL;
        rchild=NULL;
    }
};
Node node[N];
int pos=0;

void initNode1(Node *root,Node *node)
{
    if(root->lchild==NULL && node->value<root->value)
    {
        root->lchild=node;
    }
    else if(root->rchild==NULL && node->value>root->value)
    {
        root->rchild=node;
    }
    else if(root->lchild!=NULL && node->value<root->value)
    {
        initNode1(root->lchild,node);
    }
    else if(root->rchild!=NULL && node->value>root->value)
    {
        initNode1(root->rchild,node);
    }
}

int main()
{
    Node r,*root;
    int num,i=1,pos=0;
    cin>>num;
    cin>>r.value;

    root=&r;
    while(i<num)
    {
        cin>>node[pos].value;
        initNode1(root,&node[pos++]);
        i++;
    }

    preOrder(root);
    cout<<endl;
    inOrder(root);
    cout<<endl;
    postOrder(root);
    cout<<endl;
    return 0;
}
3.3 返回值为 Node *
#include <iostream>
using namespace std;

#define N 100
class Node
{
public:
    int value;
    Node *lchild;
    Node *rchild;

    Node()
    {
        lchild=NULL;
        rchild=NULL;
    }
};
Node node[N];
int pos=0;

Node *initNode2(Node *root,int v)
{
    if(root==NULL)
    {
        node[pos].value = v;
        root=&node[pos++];
    }
    else if(v<root->value)
    {
        root->lchild=initNode2(root->lchild,v);
    }
    else if(v>root->value)
    {
        root->rchild=initNode2(root->rchild,v);
    }
    return root;
}

int main()
{
    Node *root = NULL;
    //num:记录结点个数 
    //i:记录node[N]当前位置 
    //v:记录输入值
    int num,i = 0,v; 
    
    cin>>num;
    while(i<num)
    {
        cin>>v;
        root=initNode2(root,v);
        i++;
    }

    preOrder(root);
    cout<<endl;
    inOrder(root);
    cout<<endl;
    postOrder(root);
    cout<<endl;
    return 0;
}

二、二叉搜索树

1、题目和要求

时间限制:1s,内存限制:32MB,特殊判题:否
在这里插入图片描述

2、总结

1)strcmp返回值:若str1=str2,则返回零;若str1<str2,则返回负数;若str1>str2,则返回正数。

2)为orderThree[N_DATA][N_NODE]赋值时,每一行的结尾加上'\0'才能正确输出。

3)结点值为int类型,orderThree[N_DATA][N_NODE]char类型,要进行类型转换(+‘0’)。

4)包括中序遍历在内的两种遍历结果可以唯一确定一棵二叉树(必须包括中序遍历)。

3、思路

思路1: 根据输入结点建立二叉排序树;存储三种遍历顺序下的输出;存储需要判断的输入序列;两者进行比较,输出。

思路2: 按总结的第四点,每输入一个需判断的序列,就为其构造二叉排序树,将前序+中序(或中序+后序)遍历的结果拼成一个字符串,最后只需比较一个字符串(strcmp(str1, str2) == 0 ? "YES" : "NO")。

4、代码
#include <iostream>
#include <string.h>
using namespace std;

#define N_NODE 10
#define N_DATA 20
class Node
{
public:
    int value;
    Node *lchild;
    Node *rchild;

    Node()
    {
        lchild=NULL;
        rchild=NULL;
    }
};
/*
node[N_NODE]:记录结点值的数组,root:根节点
orderThree:前序、中序后序遍历结果
orderInput:输入的需要判断的字符串
pos:结点数组的位置,orderPos:当前处于哪种遍历顺序
*/
Node node[N_NODE],*root=NULL;
char orderThree[N_DATA][N_NODE];
char orderInput[N_DATA][N_NODE];
int pos=0,orderPos;

void preOrder(Node *node)
{
    orderThree[0][orderPos++]=node->value+'0';
    if(node->lchild !=NULL)
    {
        preOrder(node->lchild);
    }
    if(node->rchild !=NULL)
    {
        preOrder(node->rchild);
    }
}

void inOrder(Node *node)
{
    if(node->lchild !=NULL)
    {
        inOrder(node->lchild);
    }
    orderThree[1][orderPos++]=node->value+'0';
    if(node->rchild !=NULL)
    {
        inOrder(node->rchild);
    }
}

void postOrder(Node *node)
{
    if(node->lchild !=NULL)
    {
        postOrder(node->lchild);
    }
    if(node->rchild !=NULL)
    {
        postOrder(node->rchild);
    }
    orderThree[2][orderPos++]=node->value+'0';
}

Node *initNode(Node *root,int v)
{
    if(root==NULL)
    {
        node[pos].value=v;
        root = &node[pos++];
    }
    else if(v<root->value)
    {
        root->lchild=initNode(root->lchild,v);
    }
    else if(v>root->value)
    {
        root->rchild=initNode(root->rchild,v);
    }
    return root;
}

int main()
{
    int num,i=0;                //num:记录输入序列个数 i:当前循环次数
    char initData[N_NODE];      //构造搜索二叉树序列
    cin>>num;

    cin>>initData;
    for(int j=0; j<(int)strlen(initData); j++)
    {
        root = initNode(root,initData[j]-'0');
    }
    orderPos=0;
    preOrder(root);
    orderThree[0][orderPos]='\0';

    orderPos=0;
    inOrder(root);
    orderThree[1][orderPos]='\0';

    orderPos=0;
    postOrder(root);
    orderThree[2][orderPos]='\0';

    while(i<num)
    {
        cin>>orderInput[i];
        i++;
    }

    for(i=0; i<num; i++)
    {
        if(!strcmp(orderInput[i],orderThree[0])
                || !strcmp(orderInput[i],orderThree[1])
                || !strcmp(orderInput[i],orderThree[2]))
        {
            cout<<"YES"<<endl;
        }
        else
        {
            cout<<"NO"<<endl;
        }
    }

    return 0;
}

原创文章 35 获赞 17 访问量 7833

猜你喜欢

转载自blog.csdn.net/dear_jing/article/details/105958841
今日推荐