zoukankan      html  css  js  c++  java
  • projectpopulation

    四叉树法完成

    #include <stdio.h>

    #include <stdlib.h>

    #ifndef QuadTree

    typedef struct quadtreenode *Quadtree;

    #endif

    #define MAX_SIZE (20000)

    struct quadtreenode

    {

    int totalnum;/*the total population number in the area*/

    Quadtree leftback;

    Quadtree leftfront;

    Quadtree rightback;

    Quadtree rightfront;

    int up,down,left,right;/*record the edge*/

    };

    int sum;

    Quadtree Create(Quadtree T)/*create a quadtree node*/

    {

    T=(Quadtree)malloc(sizeof(struct quadtreenode));

    T->leftback=T->leftfront=T->rightback=T->rightfront=NULL;

    T->totalnum=0;

    return T;

    }

    Quadtree Insert(int X,int Y,int N,Quadtree T)

    {

    int xu,xd,yl,yr;

    yl=T->left;yr=T->right;

    xu=T->up;xd=T->down;

    if(yl==yr&&xu==xd)/*if it is a leaf node then return T*/

    {

    return T;

    }

    else/*(x,y)is in the leftback region*/

    if(xu<=X&&X<=(xu+xd)/2&&yl<=Y&&Y<=(yr+yl)/2){

    if(T->leftback==NULL){/*create the region and set totalnum be the region's total number*/

    T->leftback=Create(T->leftback);

    T->leftback->left=yl;

    T->leftback->right=(yr+yl)/2;

    T->leftback->up=xu;

    T->leftback->down=(xu+xd)/2;

    T->leftback->totalnum=N;/*first set the region's population*/

    }

    else

    T->leftback->totalnum+=N;/*add the new come in population number*/

    T->leftback=Insert(X,Y,N,T->leftback);/*insert to the small region*/

    }

    else/*(x,y)is in the rightback region*/

    if(xu<=X&&X<=(xu+xd)/2&&(yr+yl)/2<Y&&Y<=yr){

    if(T->rightback==NULL){

    T->rightback=Create(T->rightback);

    T->rightback->left=(yr+yl)/2+1;

    T->rightback->right=yr;

    T->rightback->up=xu;

    T->rightback->down=(xu+xd)/2;

    T->rightback->totalnum=N;

    }

       else

    T->rightback->totalnum+=N;

       T->rightback=Insert(X,Y,N,T->rightback);

    }

    else/*(x,y)is in the leftfront region*/

    if((xu+xd)/2<X&&X<=xd&&yl<=Y&&Y<=(yr+yl)/2){

    if(T->leftfront==NULL){

        T->leftfront=Create(T->leftfront);

    T->leftfront->left=yl;

    T->leftfront->right=(yl+yr)/2;

    T->leftfront->up=(xu+xd)/2+1;

    T->leftfront->down=xd;

    T->leftfront->totalnum=N;

    }

           else

    T->leftfront->totalnum+=N;

           T->leftfront=Insert(X,Y,N,T->leftfront);

    }

    else/*(x,y)is in the rightfront region*/

    if((xu+xd)/2<X&&X<=xd&&(yr+yl)/2<Y&&Y<=yr)

    {

    if(T->rightfront==NULL){

           T->rightfront=Create(T->rightfront);

    T->rightfront->left=(yr+yl)/2+1;

    T->rightfront->right=yr;

    T->rightfront->up=(xu+xd)/2+1;

    T->rightfront->down=xd;

    T->rightfront->totalnum=N;

    }

    else

    T->rightfront->totalnum+=N;

               T->rightfront=Insert(X,Y,N,T->rightfront);

    }

    return T;

    }

    void inorder(int Xmin,int Xmax,int Ymin,int Ymax,Quadtree T)

    {

    if(T){

    if(T->up>=Xmin&&T->down<=Xmax&&T->left>=Ymin&&T->right<=Ymax)

    sum=sum+T->totalnum;/*when the region is in the retangles,add to sum*/

    else{/*if the region is cover the retangles then traversal the smallregion*/

    if(!(Xmin>T->down||Ymin>T->right||Xmax<T->up||Ymax<T->left)){

    inorder(Xmin,Xmax,Ymin,Ymax,T->leftback);

    inorder(Xmin,Xmax,Ymin,Ymax,T->rightback);

    inorder(Xmin,Xmax,Ymin,Ymax,T->leftfront);

    inorder(Xmin,Xmax,Ymin,Ymax,T->rightfront);

    }

    }

    }

    }

    void CountSum(Quadtree T)

    {

    int Xmin,Xmax,Ymin,Ymax;

    while(scanf("%d", &Xmin) == 1) {

            scanf("%d%d%d", &Xmax, &Ymin, &Ymax);

    sum=0;

       inorder(Xmin,Xmax,Ymin,Ymax,T);/*do inorder traversal and calculate all the population*/

         printf("%d\n",sum);

        }   

    }

    Quadtree Insertion(Quadtree T)

    {

    int X,Y,N;

    while(scanf("%d", &X) == 1) {

       scanf("%d%d", &Y, &N);

    T->totalnum+=N;

       T=Insert(X,Y,N,T);/*keep inserting*/

    }    

    return T;

    }

    int main()

    {

    char c,s[20];

    Quadtree T;

    /*make the root be empty and stand for the whole area*/

    T=NULL;

    T=Create(T);

    T->left=T->up=1;

    T->right=T->down=MAX_SIZE;

    while(scanf("%s",s)!=EOF)/*stop when the file is over*/

    {

    c=s[0];

    switch (c){

       case 'Q':

       CountSum(T);/*count out the population in the region*/

       break;

       case 'I':

       T=Insertion(T);/*insert the population number in the right region*/

       break;

    case 'E':

    T=NULL;/*clean the tree*/

    T=Create(T);

                   T->left=T->up=1;

                   T->right=T->down=MAX_SIZE;

    break;

       }

    }

    return 0;

    }

    附加一AVL-tree搜索

    #include <stdio.h>

    #include <stdlib.h>

    #ifndef _AvlTree_H

    struct AvlNode;

    typedef struct AvlNode *Position;

    typedef struct AvlNode *AvlTree;

    typedef int ElementType;

    AvlTree Insert(int X,int Y,AvlTree T,int N);

    int Max(int a,int b);

    #endif  //_AvlNode_H

    struct AvlNode

    {

    ElementType Element;

    int ElementX;

    int ElementY;

    AvlTree Left;

    AvlTree Right;

    int Height;

    };

    AvlTree T;

    int sum;

    static int

    Height(Position P)

    {

    if(P==NULL)

    return -1;

    else

    return P->Height;

    }

    int Max(int a,int b)

    {

    if(a>b)

    return a;

    else

    return b;

    }

    static Position

    SingleRotateWithLeft(Position K2)

    {

    Position K1;

    K1=K2->Left;

    K2->Left=K1->Right;

    K1->Right=K2;

    K2->Height=Max(Height(K2->Left),Height(K2->Right))+1;

    K1->Height=Max(Height(K1->Left),K2->Height)+1;

    return K1;

    }

    static Position

    SingleRotateWithRight(Position K2)

    {

    Position K1;

    K1=K2->Right;

    K2->Right=K1->Left;

    K1->Left=K2;

    K2->Height=Max(Height(K2->Left),Height(K2->Right))+1;

    K1->Height=Max(Height(K1->Right),K2->Height)+1;

    return K1;

    }

    static Position

    DoubleRotateWithLeft(Position K3)

    {

    K3->Left=SingleRotateWithRight(K3->Left);

    return SingleRotateWithLeft(K3);

    }

    static Position

    DoubleRotateWithRight(Position K3)

    {

    K3->Right=SingleRotateWithLeft(K3->Right);

    return SingleRotateWithRight(K3);

    }

    AvlTree

    Insert(int X,int Y,AvlTree T,int N)

    {

    if(T==NULL)

    {

    T=(AvlTree)malloc(sizeof(struct AvlNode));

    if(T==NULL)

    exit(0);

    else

    {

    T->Element=N;

    T->ElementX=X;T->ElementY=Y;

    T->Height=0;

    T->Left=T->Right=NULL;

    }

    }

    else

    if(X<T->ElementX||(X==T->ElementX&&Y<T->ElementY))

    {

    T->Left=Insert(X,Y,T->Left,N);

    if(Height(T->Left)-Height(T->Right)==2)

    if(X<T->Left->ElementX||(X==T->Left->ElementX&&Y<T->Left->ElementY))

    T=SingleRotateWithLeft(T);

    else

    T=DoubleRotateWithLeft(T);

    }

    else

    if(X>T->ElementX||(X==T->ElementX&&Y>T->ElementY))

    {

    T->Right=Insert(X,Y,T->Right,N);

    if(Height(T->Right)-Height(T->Left)==2)

    if(X>T->Right->ElementX||(X==T->Right->ElementX&&Y>T->Right->ElementY))

    T=SingleRotateWithRight(T);

    else

    T=DoubleRotateWithRight(T);

    }

    else

    if(X==T->ElementX&&Y==T->ElementY)

    T->Element+=N;

    T->Height=Max(Height(T->Left),Height(T->Right))+1;

    return(T);

    }

    void inorder(int Xmin,int Xmax,int Ymin,int Ymax,AvlTree T)

    {

    if(T){

    if(T->ElementX>=Xmin)

    inorder(Xmin,Xmax,Ymin,Ymax,T->Left);

    if(T->ElementX>=Xmin&&T->ElementY>=Ymin&&T->ElementX<=Xmax&&T->ElementY<=Ymax)

    sum=sum+T->Element;

    if(T->ElementX<=Xmax)

    inorder(Xmin,Xmax,Ymin,Ymax,T->Right);

    }

    }

    void CountSum()

    {

    int Xmin,Xmax,Ymin,Ymax;

    while(scanf("%d", &Xmin) == 1) {

            scanf("%d%d%d", &Xmax, &Ymin, &Ymax);

         sum=0;

       inorder(Xmin,Xmax,Ymin,Ymax,T);

         printf("%d\n",sum);

        }   

    }

    void Insertion()

    {

    int X,Y,N;

    while(scanf("%d", &X) == 1) {

       scanf("%d%d", &Y, &N);

       T = Insert(X,Y,T,N);

    }    

    }

    int main()

    {

    char c,s[20];

    freopen("data.txt","r",stdin);

    while(scanf("%s",s)!=EOF)

    {

    c=s[0];

    switch (c){

       case 'Q':

       CountSum();

       break;

       case 'I':

       Insertion();

       break;

    case 'E':

    T=NULL;

    break;

       }

    }

    fclose(stdin);

    return 0;

    }

  • 相关阅读:
    Python3.6中PyInstaller不能对文件进行打包问题
    itchat和matplotlib的结合使用爬取微信信息
    NumPy笔记
    Spyder在windows下常用快捷键
    React 省市区三级联动
    react-router 4.0中跳转失灵
    React+ajax+java 上传图片并预览
    CMDB与自动化运维,一切尽在掌握中?
    XSS跨站脚本攻击
    shell脚本?
  • 原文地址:https://www.cnblogs.com/chuxiking/p/1932930.html
Copyright © 2011-2022 走看看