紫书第六章-----数据结构基础(二叉树遍历)

二叉树的层次遍历

例题6-7 Trees on the level UVA - 122
下面程序参考刘汝佳《算法竞赛入门经典》(第2版)

程序(一)(常规指针版)

/*
    本程序参考刘汝佳《算法竞赛入门经典》(第2版)
    本程序注意,如果二叉树中有的结点没有赋值或多次赋值,则报not complete
*/

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

struct node{
    int v;
    node *l,*r;
    bool have_value;
    node(){
        l=r=NULL;
        have_value=false;
    }
};

const int maxn=256+5;
char s[maxn];//输入所用到的字符数组
node *root;//树根
bool re_value;//标记是否某个结点被多次赋值过

node *new_node(){
    return new node();
}

void add_node(int v,char *s){
    int n=strlen(s);
    node *tmp=root;
    for(int i=0;i<n;i++){
        if(s[i]=='L'){
            if(tmp->l==NULL) tmp->l=new_node();
            tmp=tmp->l;
        }
        else if(s[i]=='R'){
            if(tmp->r==NULL) tmp->r=new_node();
            tmp=tmp->r;
        }
    }
    if(tmp->have_value) re_value=true;
    tmp->v=v;
    tmp->have_value=true;
}

bool read(){
    while(1){
        if(scanf("%s",s)==EOF) return false;
        if(strcmp(s,"()")==0) break;
        int v;
        sscanf(&s[1],"%d",&v);//sscanf函数是从字符串中取出整数部分,这里从s[1]地址开始取
        add_node(v,strchr(s,',')+1);//这里strchr的作用是从s中的","之后一个元素的地址开始
    }
    return true;
}

bool bfs(vector<int>&ans){
    queue<node *>que;
    que.push(root);
    node *tmp=NULL;
    while(!que.empty()){
        tmp=que.front();que.pop();
        if(tmp->have_value==false) return false;
        ans.push_back(tmp->v);
        if(tmp->l){que.push(tmp->l);}
        if(tmp->r){que.push(tmp->r);}
    }
    return true;
}

void destroy_tree(node *tr){
    if(tr==NULL) return;
    destroy_tree(tr->l);
    destroy_tree(tr->r);
    delete tr;
}

int main()
{
    while(1){
        re_value=false;
        vector<int>ans;
        root=new_node();
        if(!read()) break;
        if(!bfs(ans) || re_value){
            cout<<"not complete"<<endl;
        }
        else{
            int len=ans.size();
            for(int i=0;i<len;i++){
                cout<<ans[i]<<((i==len-1)?"\n":" ");
            }
        }
        destroy_tree(root);
    }
    return 0;
}

程序(二)(指针+内存池)

下面程序使用了内存池技术,即维护一个列表,这个列表中有许多相应的结点,先把很多节点入队,然后需要开辟内存的时候就从队列里面出来一个,需要释放内存的时候入队即可,这样可以反复使用这样的一段内存,即内存池技术

/*
    本程序参考刘汝佳《算法竞赛入门经典》(第2版)
    本程序注意,如果二叉树中有的结点没有赋值或多次赋值,则报not complete
*/

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

struct node{
    int v;
    node *l,*r;
    bool have_value;
    node(){
        l=r=NULL;
        have_value=false;
    }
};

const int maxn=256+5;
char s[maxn];//输入所用到的字符数组
node *root;//树根
bool re_value;//标记是否某个结点被多次赋值过

//以下是内存池技术(维护一个列表)
queue<node *>free_node;
node nd[maxn];

void init(){
    for(int i=0;i<maxn;i++){
        free_node.push(&nd[i]);//初始化内存池
    }
}

node *new_node(){
    node *tmp=free_node.front();
    tmp->l=tmp->r=NULL;
    tmp->have_value=false;
    free_node.pop();
    return tmp;
}

void destroy_node(node *tmp){
    free_node.push(tmp);
}
//以上是内存池技术(维护一个列表)

void add_node(int v,char *s){
    int n=strlen(s);
    node *tmp=root;
    for(int i=0;i<n;i++){
        if(s[i]=='L'){
            if(tmp->l==NULL) tmp->l=new_node();
            tmp=tmp->l;
        }
        else if(s[i]=='R'){
            if(tmp->r==NULL) tmp->r=new_node();
            tmp=tmp->r;
        }
    }
    if(tmp->have_value) re_value=true;
    tmp->v=v;
    tmp->have_value=true;
}

bool read(){
    while(1){
        if(scanf("%s",s)==EOF) return false;
        if(strcmp(s,"()")==0) break;
        int v;
        sscanf(&s[1],"%d",&v);//sscanf函数是从字符串中取出整数部分,这里从s[1]地址开始取
        add_node(v,strchr(s,',')+1);//这里strchr的作用是从s中的","之后一个元素的地址开始
    }
    return true;
}

bool bfs(vector<int>&ans){
    queue<node *>que;
    que.push(root);
    node *tmp=NULL;
    while(!que.empty()){
        tmp=que.front();que.pop();
        if(tmp->have_value==false) return false;
        ans.push_back(tmp->v);
        if(tmp->l){que.push(tmp->l);}
        if(tmp->r){que.push(tmp->r);}
    }
    return true;
}

void destroy_tree(node *tr){
    if(tr==NULL) return;
    destroy_tree(tr->l);
    destroy_tree(tr->r);
    free_node.push(tr);
}

int main()
{
    while(1){
        init();
        re_value=false;
        vector<int>ans;
        root=new_node();
        if(!read()) break;
        if(!bfs(ans) || re_value){
            cout<<"not complete"<<endl;
        }
        else{
            int len=ans.size();
            for(int i=0;i<len;i++){
                cout<<ans[i]<<((i==len-1)?"\n":" ");
            }
        }
        destroy_tree(root);
    }
    return 0;
}

程序(三)(数组模拟二叉树)

/*
    本程序参考刘汝佳《算法竞赛入门经典》(第2版)
    本程序注意,如果二叉树中有的结点没有赋值或多次赋值,则报not complete
*/

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

const int maxn=256+5;
char s[maxn];//输入所用到的字符数组
bool re_value;//标记是否某个结点被多次赋值过
const int root=1;
int cnt;

typedef struct{
    int v;
    bool have_value;
    int l,r;
}Node;

Node node[maxn];


void new_tree(){
    node[root].l=node[root].r=0;
    node[root].have_value=false;
    cnt=root;
}

int new_node(){
    int u=++cnt;
    node[u].l=node[u].r=0;
    node[u].have_value=false;
    return u;
}

void add_node(int v,char *s){
    int n=strlen(s);
    int tmp=root;
    for(int i=0;i<n;i++){
        if(s[i]=='L'){
            if(node[tmp].l==0) node[tmp].l=new_node();
            tmp=node[tmp].l;
        }
        else if(s[i]=='R'){
            if(node[tmp].r==0) node[tmp].r=new_node();
            tmp=node[tmp].r;
        }
    }
    if(node[tmp].have_value) re_value=true;
    node[tmp].v=v;
    node[tmp].have_value=true;
}

bool read(){
    while(1){
        if(scanf("%s",s)==EOF) return false;
        if(strcmp(s,"()")==0) break;
        int v;
        sscanf(&s[1],"%d",&v);//sscanf函数是从字符串中取出整数部分,这里从s[1]地址开始取
        add_node(v,strchr(s,',')+1);//这里strchr的作用是从s中的","之后一个元素的地址开始
    }
    return true;
}

bool bfs(vector<int>&ans){
    queue<int>que;
    que.push(root);
    int tmp;
    while(!que.empty()){
        tmp=que.front();que.pop();
        if(node[tmp].have_value==false) return false;
        ans.push_back(node[tmp].v);
        if(node[tmp].l){que.push(node[tmp].l);}
        if(node[tmp].r){que.push(node[tmp].r);}
    }
    return true;
}

int main()
{
    while(1){
        re_value=false;
        vector<int>ans;
        new_tree();
        if(!read()) break;
        if(!bfs(ans) || re_value){
            cout<<"not complete"<<endl;
        }
        else{
            int len=ans.size();
            for(int i=0;i<len;i++){
                cout<<ans[i]<<((i==len-1)?"\n":" ");
            }
        }
    }
    return 0;
}

二叉树的递归遍历

例题6-8 Tree UVA - 548

/*
    本程序参考刘汝佳《算法竞赛入门经典》(第2版)
*/

#include<iostream>
#include<string>
#include<sstream>
using namespace std;

const int maxn=10000+5;

int in_order[maxn],post_order[maxn],lc[maxn],rc[maxn];
int n;

//下面读入数据的方式非常好
bool read(int *a){
    string line;
    if(!getline(cin,line)) return false;
    stringstream ss(line);
    n=0;
    int x;
    while(ss>>x) a[n++]=x;
    return n>0;
}

//下面创建树的过程,自己好好体会,可以找一个例子,自己手动搞一下就明白了
int tree_create(int L1,int R1,int L2,int R2){
    if(L1>R1) return 0;
    int root=post_order[R2];
    int i=0;
    while(in_order[i]!=root) i++;
    int cnt=i-L1;
    lc[root]=tree_create(L1,i-1,L2,L2+cnt-1);
    rc[root]=tree_create(i+1,R1,L2+cnt,R2-1);
    return root;
}

int best,best_sum;

void dfs(int v,int sum){
    sum+=v;
    if(!lc[v] && !rc[v]){
        if(sum<best_sum || (sum==best_sum && v<best)){
            best=v;
            best_sum=sum;
        }
    }
    if(lc[v]) dfs(lc[v],sum);
    if(rc[v]) dfs(rc[v],sum);
}

int main()
{
    while(read(in_order)){
        read(post_order);
        tree_create(0,n-1,0,n-1);
        best_sum=1000000000;
        dfs(post_order[n-1],0);
        cout<<best<<endl;
    }
    return 0;
}

例题6-9 Not so Mobile UVA - 839

【先剖析下递归】
二叉树的遍历常用递归,为了深入理解递归,我利用斐波那契数的求法进行了逐句调试,并把递归调用详细过程绘制成如下图,相信只要真正理解了此图,会对递归有深入认识,并对二叉树的深搜有深刻认识。

/*
    斐波那契数
*/

#include<iostream>

using namespace std;

int f(int n){
    if(n==0 || n==1) return 1;
    else return f(n-2)+f(n-1);
}

int main()
{
    cout<<f(4)<<endl;
    return 0;
}

这里写图片描述

/*
    本程序参考刘汝佳《算法竞赛入门经典》(第2版)
*/

#include<iostream>

using namespace std;

bool solve(int &w){
    bool b1=true,b2=true;
    int w1,d1,w2,d2;
    cin>>w1>>d1>>w2>>d2;
    if(!w1) b1=solve(w1);//得到左子树的总重量,这里显示了w引用的厉害之处
    if(!w2) b2=solve(w2);//得到右子树的总重量,这里显示了w引用的厉害之处
    w=w1+w2;
    return b1 && b2 && (w1*d1==w2*d2);
}

int main()
{
    int T,w;
    cin>>T;
    while(T--){
        if(solve(w)) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
        if(T) cout<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值