《入門經典》1

二叉樹層次遍歷 代碼(看書後自己寫的)

#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 20


struct 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 20


using 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 20


struct 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;
}







做個筆記

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章