码迷,mamicode.com
首页 > 其他好文 > 详细

第六章例题二叉树层次遍历

时间:2017-07-28 23:54:10      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:names   结构体   nod   cout   int   push   front   pre   let   

 

1.指针实现

#include <iostream>
#include <vector>
#include <queue>
#include <cstdio>
#include <cstring>

using namespace std;

#define maxn 100

struct Node
{
    bool have_value;
    int value;
                    /*节点结构体*/
    Node *left,*right;
    Node():have_value(false),left(NULL),right(NULL){}

};

                /*全局变量写起来更方便*/
char s[maxn];
Node* root=NULL;
bool faile;
              
                /*用来防止内存泄漏*/
void remove_tree(Node* tree)
{
    if(tree==NULL) return;

    remove_tree(tree->left);
    remove_tree(tree->right);

    delete tree;
}

                /*创建新节点封装成函数*/
Node* newnode() { return new Node();}


                /*添加新节点的函数*/
void addnode(int v,char* s)
{
    int n=strlen(s);

    Node* u=root;

    for(int i=0;i<n;i++)
    {
        if(s[i]==L)
        {
            if(u->left==NULL)
                u->left=newnode();
            u=u->left;
        }
        else if(s[i]==R)
        {
            if(u->right==NULL)
                u->right=newnode();
            u=u->right;
        }
    }

    if(u->have_value) faile=true;

    u->value=v;
    u->have_value=true;
}


                /*读入数据并创建树,成功返回true读到文件结尾则返回false*/
bool read_input()
{
    faile=false;

    remove_tree(root);
    root=newnode();

    for(;;)
    {
        if(scanf("%s",s)!=1) return false;
        if(!strcmp(s,"()")) break;

        int v;
        sscanf(&s[1],"%d",&v);
        addnode(v,strchr(s,,)+1);
    }

    return true;
}


                /*宽度优先算法,用队列实现将结果存在向量中*/
bool bfs(vector<int>& ans)
{
    queue<Node*> q;
    ans.clear();

    q.push(root);

    while(!q.empty())
    {
        Node* u=q.front();
        q.pop();

        if(!u->have_value) return false;

        ans.push_back(u->value);

        if(u->left!=NULL) q.push(u->left);
        if(u->right!=NULL) q.push(u->right);
    }

    return true;
}


int main()
{

    vector<int> v;


    while(read_input())
    {
        if(!bfs(v) || faile==true)
            printf("%d\n",-1);
        else
            for(vector<int>::iterator i = v.begin(); i != v.end(); ++i)
                printf("%d ",*i);

        cout<<endl;    
    }

}

 

2.数组实现

#include <iostream>
#include <vector>
#include <queue>
#include <cstdio>
#include <cstring>

using namespace std;
#define maxn 1000


bool have_value[maxn];
int tleft[maxn];
int tright[maxn];
int value[maxn];
char s[100];
bool faile;

const int root=1;
int cnt;

void newtree()
{
    tleft[root]=0;
    tright[root]=0;

    have_value[root]=false;
    cnt=root;
}

int newnode()
{
    int u=++cnt;

    tleft[u]=0;
    tright[u]=0;

    have_value[u]=false;

    return u;
}

void addnode(int v,char* s)
{
    int n=strlen(s);

    int u=root;

    for(int i=0;i<n;i++)
    {
        if(s[i]==L)
        {
            if(tleft[u]==0)
                tleft[u]=newnode();
            u=tleft[u];
        }
        else if(s[i]==R)
        {
            if(tright[u]==0)
                tright[u]=newnode();
            u=tright[u];
        }
    }

    if(have_value[u]) faile=true;

    value[u]=v;
    have_value[u]=true;
}

bool read_input()
{
    faile=false;

    newtree();

    for(;;)
    {
        if(scanf("%s",s)!=1) return false;
        if(!strcmp(s,"()")) break;

        int v;
        sscanf(&s[1],"%d",&v);
        addnode(v,strchr(s,,)+1);
    }

    return true;
}

bool bfs(vector<int>& ans)
{
    queue<int> q;
    ans.clear();

    q.push(root);

    while(!q.empty())
    {
        int u=q.front();
        q.pop();

        if(!have_value[u]) return false;

        ans.push_back(value[u]);

        if(tleft[u]!=0) q.push(tleft[u]);
        if(tright[u]!=0) q.push(tright[u]);
    }

    return true;
}


int main()
{

    vector<int> v;


    while(read_input())
    {
        if(!bfs(v) || faile==true)
            printf("%d\n",-1);
        else
            for(vector<int>::iterator i = v.begin(); i != v.end(); ++i)
                printf("%d ",*i);

        cout<<endl;    
    }

}

 

书上的接口写的太棒了,换了种实现方式,代码基本上没改,比我自己写的接口不知道高到哪里去了.

第六章例题二叉树层次遍历

标签:names   结构体   nod   cout   int   push   front   pre   let   

原文地址:http://www.cnblogs.com/tclan126/p/7252781.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!