总结一下模板\(≧▽≦)/

本文介绍了C++中二叉树的先序、中序、后序遍历,以及字典树和图的基础知识,包括建树、查找、删除等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

二叉树的各种操作:

#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct node
{
    char data;
    struct node *lchild,*rchild;
}*Tree;
void CreateBitree(Tree &p) //建树
{
    char ch;
    scanf("%c",&ch);
    if(ch==',')
        p=NULL;
    else
    {
        p=new node;
        p->data=ch;
        CreateBitree(p->lchild);
        CreateBitree(p->rchild);
    }
    return ;
}
void xpreorder(Tree p) //输出先序
{
    if (p)
    {
        printf("%c ",p->data);
        xpreorder(p->lchild);
        xpreorder(p->rchild);
    }
}
void zpreorder(Tree p) //输出中序
{
    if(p)
    {
        zpreorder(p->lchild);
        printf("%c",p->data);
        zpreorder(p->rchild);
    }
}
void hpreorder(Tree p) //输出后序
{
    if(p)
    {
        hpreorder(p->lchild);
        hpreorder(p->rchild);
        printf("%c",p->data);
    }
}
void ycount(Tree p,int &count) //求二叉树叶子节点
{
    if(p)
    {
        if(p->lchild==NULL&&p->rchild==NULL)
        {
            count++;
            return ;
        }
        if(p->lchild) ycount(p->lchild,count);
        if(p->rchild) ycount(p->rchild,count);
    }
}
int sleaf(Tree p)  //求二叉树深度
{
    int high=0;
    if(!p) return high;
    int n=sleaf(p->lchild);
    int m=sleaf(p->rchild);
    high=m>n?m:n;
    return high+1;
}
int main()
{
    node *p;
    CreateBitree(p);
    xpreorder(p);
    printf("\n");
    zpreorder(p);
    printf("\n");
    hpreorder(p);
    printf("\n");
    int countleaf=0;
    ycount(p,countleaf);
    printf("%d\n",countleaf);
    printf("%d\n",sleaf(p));
    return 0;
}

有先序和中序求后序:

#include<stdio.h>
#include<string.h>
#include<malloc.h>
struct node
{
    char data;
    struct node *lchild,*rchild;
};
void Bian(struct node *p)
{
    if(p->lchild!=NULL)
        Bian(p->lchild);
    if(p->rchild!=NULL)
        Bian(p->rchild);
    printf("%c",p->data);

}
struct node*buildtree(struct node *p,char *pre,char*mid,int n)
{
    if(n<=0)
        return NULL;
    p=(struct node*)malloc(sizeof(struct node));
    p->data=pre[0]; //先序的第一个数是根节点
    int q=strchr(mid,pre[0])-mid;  //根节点在中序中的位置好找到左子树和右子树
    p->lchild=buildtree(p->lchild,pre+1,mid,q); 
    p->rchild=buildtree(p->rchild,pre+q+1,mid+q+1,n-q-1); //无限递归
    return p;
}
int main()
{
    char pre[1001],mid[1001];
    gets(pre);
    gets(mid);
    int n=strlen(pre);
    struct node *root;
    root=buildtree(root,pre,mid,n);
    Bian(root);
    printf("\n");
    return 0;
}

中序和后序求先序:

void Bian(struct node *p)
{
    if(p)
    {
        printf("%c",p->data);
        Bian(p->lchild);
        Bian(p->rchild);
    }
}
struct node*buildtree(struct node *p,char *mid,char*aft,int n)
{
    if(n<=0)
        return NULL;
    p=(struct node*)malloc(sizeof(struct node));
    p->data=aft[n-1];
    int q=strchr(mid,aft[n-1])-mid;
    p->lchild=buildtree(p->lchild,mid,aft,q);
    p->rchild=buildtree(p->rchild,mid+q+1,aft+q,n-q-1);
    return p;
}


字典树:参考<Message Flood>

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
char str1[21000][15],str2[15];
struct node
{
    int flag;
    struct node *next[26]; //数字定义10,英文字母定义26
};
struct node *creat()
{
    int i;
    struct node *p;
    p=new node;
    for(i=0; i<26; i++)
    {
        p->next[i]=NULL;
    }
    p->flag=0;
    return p;
} //是根节点的26个方向都指向空
void Insert(struct node *p,char *s) //建字典树树
{
    int i,n,num;
    n=strlen(s);
    for(i=0; i<n; i++)
    {
        if(s[i]>='A'&&s[i]<='Z') num=s[i]-'A';
        else if(s[i]>='a'&&s[i]<='z') num=s[i]-'a';
        if(p->next[num]==NULL) p->next[num]=creat();//如果p指向的为空,则继续向下建树
        p=p->next[num];
    }
    p->flag=1;//记录完整的字符串
}
int Search(struct node *p,char *s) //查找
{
    int i,n,num;
    n=strlen(s);
    for(i=0; i<n; i++)
    {
        if(s[i]>='A'&&s[i]<='Z') num=s[i]-'A';
        else if(s[i]>='a'&&s[i]<='z') num=s[i]-'a';
        if(p->next[num]==NULL) return 1;
        p=p->next[num];
    }
    if(p->flag==1)
    {
        p->flag=0; //防止被查找的有重复的,被查找过便使其为0
        return 0;
    }
    return 1;
}
void Delete(struct node*p) //释放内存!!很重要!!否则会超时
{
    int i;
    for(i=0; i<26; i++)
    {
        if(p->next[i]!=NULL)
            Delete(p->next[i]);
    }
    free(p);
}
int main()
{
    int i,n,m;
    struct node *p;
    while(~scanf("%d",&n)&&n)
    {
        scanf("%d",&m);
        p=creat();
        for(i=0; i<n; i++)
        {
            scanf("%s",str1[i]);
        }
        for(i=0; i<m; i++)
        {
            scanf("%s",str2);
            Insert(p,str2);
        }
        int s=0;
        for(i=0;i<n;i++)
        {
            if(Search(p,str1[i]))
                ++s;
        }
        printf("%d\n",s);
        Delete(p);
    }
    return 0;
}

图的基础知识:之前bfs也不太会用,就当也算是bfs的模板吧

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
struct node
{
    int x;
    int y;
    int ans;
}q[2100];
int map[11000][1100],v[1100];
int k;
void bfs(int x)
{
    int e=0,s=0,i;
    node t,f;
    t.x=x,t.ans=0;
    q[e++]=t;
    v[t.x]=1;
    while(s<e)
    {
        t=q[s++];
        if(t.x==1)
        {
            printf("%d\n",t.ans);
            return ;
        }
        for(i=1; i<=k; i++)
        {
            f.x=i;
            if(v[f.x]==0&&map[t.x][f.x]==1)
            {
                f.ans=t.ans+1; 
                q[e++]=f;
                v[f.x]=1;
            }
        }
    }
    printf("NO\n");
    return ;
}
int main()
{
    int n,m,i,u,w,t;
    while(~scanf("%d%d",&k,&m))
    {
        memset(map,0,sizeof(map));
        memset(v,0,sizeof(v));
        for(i=0; i<m; i++)
        {
            scanf("%d%d",&u,&w);
            map[u][w]=1; //有向的
            //map[w][u]=1; 若都写为无向的
        }
        bfs(k);
    }
    return 0;
}

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
int map[11000][100];
int v[100];
int flag,k;
struct node
{
    int u,v,w;
    node *next;
}*head[11000];
void dfs(int i)
{
    if(flag==0)
    {
        printf("%d",i);
        flag=1;
    }
    else
        printf(" %d",i);
    for(int j=1;j<k;j++)
    {
        if(map[i][j]==1&&v[j]==0) //无限递归 只有当之间有路且没被访问过
        {
            v[j]=1; //标志被访问过
            dfs(j);
        }
    }
}
int main()
{
    struct node *p;
    int n,m,i,x,y;
    scanf("%d",&n);
    while(n--)
    {
        flag=0;
        scanf("%d%d",&k,&m);
        memset(map,0,sizeof(map));
        memset(v,0,sizeof(v));
        for(i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            map[x][y]=1;
            map[y][x]=1; //无向的
        }
        for(i=0;i<k;i++) //从小的编号开始
        {
            if(v[i]==0)
                dfs(i);
        }
        printf("\n");
    }
    return 0;
}

并查集

#include "stdio.h"
int bin[1002];
int findx(int x) //查找根结点
{
    int i,j
    int r=x;
    while(bin[r] !=r)
        r=bin[r];
    i = x;
    while (i != r) //本循环修改查找路径中所有节点
    {
        j = bin[i];
        bin[i] = r;
        i = j;
    }
    return r; //路径压缩。。很省时间
}
void merge(int x,int y)
{
    int fx,fy;
    fx = findx(x);
    fy = findx(y);
    if(fx != fy)
        bin[fx] = fy;
}
int main()
{
    int n,m,i,x,y,count;
    while(scanf("%d",&n),n)
    {
        for(i=1; i<=n; i++)
            bin[i] = i;
        for(scanf("%d",&m);)
        {
            scanf("%d %d",&x,&y);
            merge(x,y);
        }
        count=0;
        for(, i=1; i<=n; i++)
            if(bin[i] == i)
                count ++;
        printf("%d\n",count-1);
    }
}

整理的不是很好。。但是已经是我这个星期所学的知识都在这里了。。嘿嘿~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值