四叉树的C++实现

四叉树的C++实现数据结构抽象数据类型定义如下 ADTQuadTrees 数据对象 D D 是具有相同性质的具有二维结构的数据元素的集合 本实验为坐标数据 数据关系 R 若 D 为空集 则称为空树 若 D 仅含有一个数据元素 则 R 为空集 否则 R H H 是如下二元关系 1 在 D 中存在唯一的元素 root 它在关系 H 下无父节点 2 D 中任意元素 d 将其子节点划分为四个象限 将

ADT QuadTrees{ 
    数据对象D:D是具有相同性质的具有二维结构的数据元素的集合,本实验为坐标数据。 数据关系R:若D为空集,则称为空树;若D仅含有一个数据元素,则R为空集,否则R={ 
   H}, H是如下二元关系: (1) 在D中存在唯一的元素root,它在关系H下无父节点; (2) D中任意元素d将其子节点划分为四个象限,将其直接相邻的节点分别设为d[1],d[2],d[3],d[4](3) 若D-{ 
   root}≠ɸ,则存在D-{ 
   root} ={ 
   root[1],root[2],root[3],root[4]},且两两无交集; (4) 若d[i] ≠ɸ(i=1,2,3,4),则d[i]中存在唯一的xi ,<root, xi> ∈H,且存在d[i]上的关系Hi⊂H; H={ 
   <root, xi>, Hi},i=1,2,3,4; (5) (root[i],{ 
   Hi})i=1,2,3,4 是符合定义的四叉树; 基本操作: QuadTreeInit(QT);//构造空的四叉树 Compare(node A, node B);//返回B在A的第几个象限,返回值取之只能为1,2,3,4 StaightforwardInsertion(QT,K);//简单插入节点K(不考虑树的平衡) SophisticatedInsertion(QT,K);//插入节点K(考虑树的平衡) RegionSearch(QT,K,double L,R,B,T);//查找节点K所有在x=L,x=B,y=R,y=T所构成矩形区域的子节点 }ADT QuadTrees; 

四叉树与对应节点关系对照

以下是源码:

#include 
  
    #include 
   
     using namespace std; template 
    
      struct Point{ T x; T y; Point(){} Point(T _x, T _y) :x(_x), y(_y){} }; template 
     
       struct Node{ Node* R[4]; Point 
      
        pt; Node* parent; }; template 
       
         class QuardTree { public: QuardTree(); ~QuardTree(); void Insert(const Point 
        
          & pos); void BalanceInsert(const Point 
         
           & pos ); int nodeCount(); int TPLS(); int Height(); void RegionResearch(ElemType left, ElemType right, ElemType botom, ElemType top, int& visitednum,int& foundnum); void clear(); private: Node 
          
            * root; int Compare(const Node 
           
             * node, const Point 
            
              & pos); bool In_Region(Point 
             
               t, ElemType left, ElemType right, ElemType botom, ElemType top); bool Rectangle_Overlapse_Region(ElemType L, ElemType R, ElemType B, ElemType T, ElemType left, ElemType right, ElemType botom, ElemType top); void RegionResearch(Node 
              
                * t, ElemType left, ElemType right, ElemType botom, ElemType top, int& visitednum, int& foundnum); int Depth(Node 
               
                 * &); int nodeCount(const Node 
                
                  *); void clear(Node < ElemType>*& p); void Insert(Node 
                 
                   *& , const Point 
                  
                    & pos);//递归插入节点 }; template 
                   
                     QuardTree 
                    
                      ::QuardTree() { root = NULL; } template 
                     
                       QuardTree 
                      
                        ::~QuardTree() { clear(root); } template 
                       
                         int QuardTree 
                        
                          ::TPLS() { return Depth(root); } template 
                         
                           int QuardTree 
                          
                            ::Compare(const Node 
                           
                             * node, const Point 
                            
                              & pos) { if (pos.x == node->pt.x && pos.y == node->pt.y) return 0; if (pos.x >= node->pt.x && pos.y>node->pt.y) return 1; if (pos.x 
                             
                               pt.x && pos.y >= node->pt.y) return 2; if (pos.x <= node->pt.x && pos.y 
                              
                                pt.y) return 3; if (pos.x>node->pt.x && pos.y <= node->pt.y) return 4; return -1; } template 
                               
                                 void QuardTree 
                                
                                  ::BalanceInsert(const Point 
                                 
                                   & pos) { Node 
                                  
                                    * node = (Node 
                                   
                                     *)malloc(sizeof(Node 
                                    
                                      )); node->R[0] = NULL; node->R[1] = NULL; node->R[2] = NULL; node->R[3] = NULL; node->parent = NULL; node->pt = pos; if (root == NULL) { root = node; return; } Node 
                                     
                                       * temp = root; int direction = Compare(temp, pos); if (direction == 0) return; while (temp->R[direction - 1] != NULL) { temp = temp->R[direction - 1]; direction = Compare(temp, pos); if (direction == 0) return; } temp->R[direction - 1] = node; node->parent = temp; Node 
                                      
                                        * tp = temp->parent; if (tp == NULL) return; int r = Compare(tp, temp->pt); if (abs(direction-r) == 2) { Node 
                                       
                                         * leaf = node; if (tp->R[abs(3 - r)] == NULL ) { tp->R[r - 1] = NULL; temp->parent = leaf; leaf->R[r-1] = temp; temp->R[abs(3 - r)] = NULL; Node 
                                        
                                          * Rt = tp->parent; if (Rt == NULL) { root = leaf; leaf->parent = NULL; leaf->R[abs(3 - r)] = tp; tp->parent = leaf; return; } tp->parent = NULL; int dd = Compare(Rt, tp->pt); Rt->R[dd - 1] = leaf; leaf->parent = Rt; leaf->R[abs(3 - r)] = tp; tp->parent = leaf; } } } template 
                                         
                                           void QuardTree 
                                          
                                            ::Insert(Node 
                                           
                                             *& p, const Point 
                                            
                                              & pos) { if (p == NULL) { Node 
                                             
                                               * node = (Node 
                                              
                                                *)malloc(sizeof(Node 
                                               
                                                 )); node->R[0] = NULL; node->R[1] = NULL; node->R[2] = NULL; node->R[3] = NULL; node->pt = pos; p = node; return; } else { int d = Compare(p, pos); if (d == 0) return; Insert(p->R[d - 1], pos); } } template 
                                                
                                                  void QuardTree 
                                                 
                                                   ::Insert(const Point 
                                                  
                                                    & pos) { int direction, len = 0; Node 
                                                   
                                                     * node = (Node 
                                                    
                                                      *)malloc(sizeof(Node 
                                                     
                                                       )); node->R[0] = NULL; node->R[1] = NULL; node->R[2] = NULL; node->R[3] = NULL; node->pt = pos; if (root == NULL) { root = node; return; } direction = Compare(root, pos); Node 
                                                      
                                                        * temp = root; if (direction == 0) return;//节点已存在 len = 1; while (temp->R[direction - 1] != NULL) { temp = temp->R[direction - 1]; direction = Compare(temp, pos); if (direction == 0) return; } temp->R[direction - 1] = node; //Insert(root, pos);//递归插入节点 } template 
                                                       
                                                         int QuardTree 
                                                        
                                                          ::nodeCount() { return nodeCount(root); } template 
                                                         
                                                           int QuardTree 
                                                          
                                                            ::nodeCount(const Node 
                                                           
                                                             * node) { if (node == NULL) return 0; return 1 + nodeCount(node->R[0]) + nodeCount(node->R[1]) + nodeCount(node->R[2]) + nodeCount(node->R[3]); } template 
                                                            
                                                              bool QuardTree 
                                                             
                                                               ::In_Region(Point 
                                                              
                                                                t, T left, T right, T botom, T top) { return t.x >= left && t.x <= right && t.y >= botom && t.y <= top; } template 
                                                               
                                                                 bool QuardTree 
                                                                
                                                                  ::Rectangle_Overlapse_Region(ElemType L, ElemType R, ElemType B, ElemType T, ElemType left, ElemType right, ElemType botom, ElemType top) { return L <= right && R >= left && B <= top && T >= botom; //return true; }//优化查找速度 template 
                                                                 
                                                                   void QuardTree 
                                                                  
                                                                    ::RegionResearch(Node 
                                                                   
                                                                     * t, T left, T right, T botom, T top, int& visitednum, int& foundnum) { if (t == NULL) return; T xc = t->pt.x; T yc = t->pt.y; if (In_Region(t->pt, left, right, botom, top)){ ++foundnum; } if (t->R[0] != NULL && Rectangle_Overlapse_Region(xc, right, yc, top, left, right, botom, top)) { visitednum++; RegionResearch(t->R[0], xc>left?xc:left, right, yc>botom?yc:botom, top, visitednum, foundnum); } if (t->R[1] != NULL && Rectangle_Overlapse_Region(left, xc, yc, top, left, right, botom, top)) { visitednum++; RegionResearch(t->R[1], left, xc>right?right:xc, yc>botom?yc:botom, top, visitednum, foundnum); } if (t->R[2] != NULL && Rectangle_Overlapse_Region(left, xc, botom, yc, left, right, botom, top)) { visitednum++; RegionResearch(t->R[2], left, xc 
                                                                    
                                                                      R[3] != NULL && Rectangle_Overlapse_Region(xc, right, botom, yc, left, right, botom, top)) { visitednum++; RegionResearch(t->R[3], xc>left ? xc : left, right, botom, yc 
                                                                     
                                                                       void QuardTree 
                                                                      
                                                                        ::clear() { clear(root); } template 
                                                                       
                                                                         void QuardTree 
                                                                        
                                                                          ::clear(Node 
                                                                         
                                                                           * &p) { if (p == NULL) return; if (p->R[0]) clear(p->R[0]); if (p->R[1]) clear(p->R[1]); if (p->R[2]) clear(p->R[2]); if (p->R[3]) clear(p->R[3]); free(p); p = NULL; } template 
                                                                          
                                                                            void QuardTree 
                                                                           
                                                                             ::RegionResearch(T left, T right, T botom, T top, int& visitednum, int& foundnum) { RegionResearch(root, left, right, botom, top, visitednum,foundnum); } template 
                                                                            
                                                                              int QuardTree 
                                                                             
                                                                               ::Depth(Node 
                                                                              
                                                                                * &node) { if (node == NULL) return 0; int dep = 0; Node 
                                                                               
                                                                                 * tp = root; while (tp->pt.x!=node->pt.x || tp->pt.y!=node->pt.y) { dep++; tp = tp->R[Compare(tp, node->pt) - 1]; if (tp == NULL) break; } return dep + Depth(node->R[0]) + Depth(node->R[1]) + Depth(node->R[2]) + Depth(node->R[3]); } 
                                                                                
                                                                               
                                                                              
                                                                             
                                                                            
                                                                           
                                                                          
                                                                         
                                                                        
                                                                       
                                                                      
                                                                     
                                                                    
                                                                   
                                                                  
                                                                 
                                                                
                                                               
                                                              
                                                             
                                                            
                                                           
                                                          
                                                         
                                                        
                                                       
                                                      
                                                     
                                                    
                                                   
                                                  
                                                 
                                                
                                               
                                              
                                             
                                            
                                           
                                          
                                         
                                        
                                       
                                      
                                     
                                    
                                   
                                  
                                 
                                
                               
                              
                             
                            
                           
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
  

参考文献
Finkel R A, Bentley J L. Quad trees a data structure for retrieval on composite keys[J]. Acta Informatica, 1974, 4(1):1-9.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/221608.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月17日 下午5:42
下一篇 2026年3月17日 下午5:42


相关推荐

  • java不重启服务动态加载properties文件

    动态加载properties文件内容,不需要重启服务!1 、Maven 工程,在resource下新建一个properties文件

    2022年2月26日
    39
  • react 路由完整版「建议收藏」

    react 路由完整版「建议收藏」import{BrowserRouter}from’react-router-dom’1、用BrowserRouter管理整个应用 在index.js中,将<App/>用<BrowserRouter>包裹起来2、路由跳转 import{NavLink,Link}from’react-router-dom’ NavLink和Link都可以实现,且用法相同 <NavLinkto=’/about’>About</NavLink&g

    2022年4月28日
    55
  • 不能逃避的分离「建议收藏」

    生命的意义是什么,我一直在探索 !对我来说,刚刚过往一个年,过年在家的状态是简单单纯的,不用去思考很多事情,就是享受家的感觉,享受和家人一切的幸福。2018这个年在家待的时间比较久,因为外出工作,现在也在外面生活,和家人在一起的时间真的很少,从2015年毕业后到外面工作一直是一年回家一次,2017还算多,回家两次,但是即便是回家两次,真正在家里待的时间也是不多的,仔细算算在父母有限…

    2022年2月27日
    59
  • QT多线程中槽函数如何执行分析「建议收藏」

    周末天冷,索性把电脑抱到床上上网,这几天看了dbzhang800博客关于Qt事件循环的几篇Blog,发现自己对Qt的事件循环有不少误解。从来只看到现象,这次借dbzhang800的博客,就代码论事,因此了解到一些Qt深层的实现,虽然是在Qt庞大的构架里只算的是冰山的一角,确让人颇为收益。从dbzhang800的博客中转载两篇关于事件循环的文章,放…

    2022年4月13日
    112
  • PHP怎样对接腾讯混元大模型_设置鉴权参数调用混元生成文案【方法】

    PHP怎样对接腾讯混元大模型_设置鉴权参数调用混元生成文案【方法】

    2026年3月12日
    3
  • 输油管的布置数学建模matlab,输油管布置的数学模型

    输油管的布置数学建模matlab,输油管布置的数学模型题研究—m⋯一一鼢|毳褥穰麓羧◎李银敏王作顺刘刚(广西贵港75130部队537100)【摘要】本论文研究了输油管线铺设最小费用问题,对问题1建立优化模型,运用函数极值理论及MATLAB软件求出最优解并给出了相应的铺设方案.首先我们运用机理分析说明公用管线必与铁路垂直,简化了问题,通过研究最一…

    2022年6月17日
    21

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号