二叉排序樹(建樹,先序,中序,后序遍歷)


2018-3-12

在牛客網上提交了一下,賊坑,每一行最后一個元素后有空格,並且輸入可能有相同的元素。

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

struct Node
{
    int num;
    Node *lson,*rson;
} node[105];

int size,n;

Node *create(int num)
{
    node[size].num=num;
    node[size].lson=node[size].rson=NULL;
    return &node[size++];
}

Node *insert(Node *rt,int num)
{
    if(rt==NULL)
    {
        rt=create(num);
        return rt;
    }
    if(rt->num==num)
        return rt;
    else if(rt->num>num)
        rt->lson=insert(rt->lson,num);
    else 
        rt->rson=insert(rt->rson,num);
    return rt;
}

void preOrder(Node *rt)
{
    if(rt==NULL)
        return;
    printf("%d ",rt->num);
    preOrder(rt->lson);
    preOrder(rt->rson);
}

void inOrder(Node *rt)
{
    if(rt==NULL)
        return;
    inOrder(rt->lson);
    printf("%d ",rt->num);
    inOrder(rt->rson);
}

void postOrder(Node *rt)
{
    if(rt==NULL)
        return;
    postOrder(rt->lson);
    postOrder(rt->rson);
    printf("%d ",rt->num);
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        size=0;
        Node *rt=NULL;
        for(int i=0;i<n;i++)
        {
            int tmp;
            scanf("%d",&tmp);
            rt=insert(rt,tmp);
        }
        preOrder(rt);
        printf("\n");
        inOrder(rt);
        printf("\n");
        postOrder(rt);
        printf("\n");
    }
    return 0;
}

 

 


 

思路簡單,但是實現時有的地方需要注意,

1) insert(Node *rt,int num)的傳參,指針做形參是地址傳遞,可以達到修改形參所指地址內容的目的,但這個形參的值即地址值不會發生變化,最開始Insert是這樣寫的

void insert(Node *rt,int num)
{
    if(rt==NULL)
    {
        //cout<<"**"<<endl;
        rt=create(num);
        return rt;
    }
    if(num<rt->num)
        insert(rt->lson,num);
    else if(num>rt->num)
        insert(rt->rson,num);
}

最開始不覺得有什么不對,但是發現insert完過后,rt==NULL;分析應該是,指針做參數可以改變rt指向的單元的內容,但如同一般變量的傳參,函數體內對形參的操作不會改變傳入的實參的值

 

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

struct Node
{
    Node *lson,*rson;
    int num;
} node[105];
int cntn;

Node *create(int num)
{
    node[cntn].lson=node[cntn].rson=NULL;
    node[cntn].num=num;
    return &node[cntn++];
}

int cnt,n;
void preOrder(Node *rt)
{
    if(rt==NULL)
        return;
        //cout<<"*"<<endl;
    printf("%d",rt->num);
    if(++cnt==n)
        printf("\n");
    else 
        printf(" ");
    preOrder(rt->lson);
    preOrder(rt->rson);
}


void inOrder(Node *rt)
{
    if(rt==NULL)
        return;
        //cout<<"*"<<endl;
    inOrder(rt->lson);
    printf("%d",rt->num);
    if(++cnt==n)
        printf("\n");
    else 
        printf(" ");
    
    inOrder(rt->rson);
}

void postOrder(Node *rt)
{
    if(rt==NULL)
        return;
        //cout<<"*"<<endl;
    postOrder(rt->lson);
    postOrder(rt->rson);
    printf("%d",rt->num);
    if(++cnt==n)
        printf("\n");
    else 
        printf(" ");
}

Node *insert(Node *rt,int num)
{
    if(rt==NULL)
    {
        rt=create(num);
        return rt;
    }
    if(num<rt->num)
        rt->lson=insert(rt->lson,num);
    else if(num>rt->num)
        rt->rson=insert(rt->rson,num);
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        cntn=0;
        Node *rt=NULL;
        for(int i=0; i<n; i++)
        {
            int num;
            scanf("%d",&num);
            rt=insert(rt,num);
        }
        cnt=0;
        preOrder(rt);
        cnt=0;
        inOrder(rt);
        cnt=0;
        postOrder(rt);
    }
    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM