《入门经典》1

来源:互联网 发布:胶州加工中心编程招聘 编辑:程序博客网 时间:2024/06/01 08:10

二叉树层次遍历 代码(看书后自己写的)

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <queue>#include <vector>using namespace std;struct node{    int value;    node *left,*right;    node():value(0),left(NULL),right(NULL){}};char a[20];vector<int> ans;queue<node*> q;void addnode(node *start,int value,char *dir){    char *i = dir;    if(*i == ')')    {        start -> value = value;    }    else    {        node *temp = start;        for(;*i != ')';++i)        {            if(*i == 'L')            {                if(temp->left == NULL)                    temp->left = (node*)malloc(sizeof(node));                temp = temp->left;            }            if(*i == 'R')            {                if(temp->right == NULL)                    temp->right = (node*)malloc(sizeof(node));                temp = temp->right;            }        }        temp -> value = value;    }}int input(node *start){    int v=0;    if(a[1] == ')')        return 1;    sscanf(&a[1],"%d",&v);    addnode(start,v,strchr(a,',')+1);    return 0;}void BFS(node *start){    q.push(start);    while(!q.empty())    {        node *temp = q.front();        q.pop();        printf("%d ",temp->value);        if(temp->left != NULL)  q.push(temp->left);        if(temp->right != NULL) q.push(temp->right);    }}int main(){    node *start = (node*)malloc(sizeof(node));    while(scanf("%s",a))    {        if(input(start))            break;    }    BFS(start);    return 0;}

二叉树遍历代码(照着书写的)

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <queue>#include <vector>#define maxn 20struct node{    int value;    bool have_value;    node *left,*right;    node():value(0),have_value(false),left(NULL),right(NULL){}};using namespace std;char s[maxn];node *root;bool failed;node*newnode() {return new node;}void addnode(int value,char *s){    int i=0,n=strlen(s);    node *u = root;    for(;i<n;++i)    {        if(s[i] == 'L')        {            if(u -> left == NULL)                u -> left = newnode();            u = u->left;        }        if(s[i] == 'R')        {            if(u -> right == NULL)                u -> right = newnode();            u = u->right;        }    }    if(u -> have_value) failed = true;    u -> value = value;    u -> have_value = true;}bool read_input(){    failed = false;    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){    node *u = root;    ans.clear();    queue<node*>q;    q.push(u);    while(!q.empty())    {        node *temp = q.front();        q.pop();        if(!temp->have_value) return false;        ans.push_back(temp->value);        if(temp -> left != NULL)            q.push(temp->left);        if(temp -> right != NULL)            q.push(temp->right);    }    return true;}int main(){    vector<int> ans;    read_input();    bfs(ans);    size_t i = 0;    for(;i<ans.size();++i)        printf("%d ",ans[i]);    return 0;}
数组方式 建立树与 遍历(BFS)
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <queue>#include <vector>#define maxn 20using namespace std;int value[256];int mleft[256];int mright[256];bool have_value[256];int root = 1;int cnt = root;char s[maxn];bool failed;int newnode(){    ++cnt;    mleft[cnt] = 0;    mright[cnt] = 0;    have_value[cnt] = false;    return cnt;}void addnode(int v,char *s){    int i=0,n=strlen(s);    int u = root;    for(;i<n;++i)    {        if(s[i] == 'L')        {            if(mleft[u] == 0)                mleft[u] = newnode();            u = mleft[u];        }        if(s[i] == 'R')        {            if(mright[u] == 0)                mright[u] = newnode();            u = mright[u];        }    }    if(have_value[u]) failed = true;    value[u] = v;    have_value[u] = true;}void remove_tree(int root){    mleft[root] = mright[root] = 0;    have_value[root] = false;}bool read_input(){    failed = false;    remove_tree(root);    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){    int u = root;    ans.clear();    queue<int>q;    q.push(u);    while(!q.empty())    {        int temp = q.front();        q.pop();        if(!have_value[temp]) return false;        ans.push_back(value[temp]);        if(mleft[temp] != 0)            q.push(mleft[temp]);        if(mright[temp] != 0)            q.push(mright[temp]);    }    return true;}int main(){    vector<int> ans;    read_input();    bfs(ans);    size_t i = 0;    for(;i<ans.size();++i)        printf("%d ",ans[i]);    return 0;}

结构体 加 指针 加 动态化为静态 加 内存池 实现

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <queue>#include <vector>#define maxn 20struct node{    int value;    bool have_value;    node *left,*right;    node():value(0),have_value(false),left(NULL),right(NULL){}};using namespace std;char s[maxn];node *root;bool failed;node nodes[256];queue<node*>pool;void minit(){    int i;    for(i=0;i<256;++i)        pool.push(&nodes[i]);}node*newnode()//改变{    node *a = pool.front();    pool.pop();    a -> left = a -> right = 0;    a -> have_value = false;    return a;}void addnode(int value,char *s){    int i=0,n=strlen(s);    node *u = root;    for(;i<n;++i)    {        if(s[i] == 'L')        {            if(u -> left == NULL)                u -> left = newnode();            u = u->left;        }        if(s[i] == 'R')        {            if(u -> right == NULL)                u -> right = newnode();            u = u->right;        }    }    if(u -> have_value) failed = true;    u -> value = value;    u -> have_value = true;}bool read_input(){    failed = false;    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){    node *u = root;    ans.clear();    queue<node*>q;    q.push(u);    while(!q.empty())    {        node *temp = q.front();        q.pop();        if(!temp->have_value) return false;        ans.push_back(temp->value);        if(temp -> left != NULL)            q.push(temp->left);        if(temp -> right != NULL)            q.push(temp->right);    }    return true;}int main(){    minit();    vector<int> ans;    read_input();    bfs(ans);    size_t i = 0;    for(;i<ans.size();++i)        printf("%d ",ans[i]);    return 0;}

做个笔记

0 0
原创粉丝点击