数据结构——四叉树

四叉树(Quadtree)是一种用于表示和管理二维空间的树状数据结构。它将二维空间递归地分割成四个象限,每个象限可以继续分割,以实现对空间的更精细的划分。四叉树通常用于解决空间搜索和查询问题,例如碰撞检测、图像压缩、地理信息系统等领域。

特别适合大规模的广阔室外场景管理。一般来说如果游戏场景是基于地形的(甚至没有高度)(如城市、平原、2D场景),那么适合用四叉树来管理。而如果游戏场景在高度轴上也有大量物体需要管理(如太空、高山),那么适合用八叉树来管理。

cpp 复制代码
#include <iostream>

// 定义二维点的结构体
struct Point {
    float x;
    float y;
    Point(float _x, float _y) : x(_x), y(_y) {}
};

// 定义四叉树节点
struct QuadTreeNode {
    Point point;
    QuadTreeNode* topLeft;
    QuadTreeNode* topRight;
    QuadTreeNode* bottomLeft;
    QuadTreeNode* bottomRight;

    QuadTreeNode(Point _point) : point(_point), topLeft(nullptr), topRight(nullptr), bottomLeft(nullptr), bottomRight(nullptr) {}
};

class QuadTree {
private:
    QuadTreeNode* root;
    int maxDepth; // 最大深度

    // 在指定深度下递归插入节点
    QuadTreeNode* insert(QuadTreeNode* node, Point point, int depth) {
        if (node == nullptr) {
            return new QuadTreeNode(point);
        }

        // 根据点的位置选择象限
        if (point.x < node->point.x && point.y < node->point.y) {
            node->bottomLeft = insert(node->bottomLeft, point, depth + 1);
        } else if (point.x >= node->point.x && point.y < node->point.y) {
            node->bottomRight = insert(node->bottomRight, point, depth + 1);
        } else if (point.x < node->point.x && point.y >= node->point.y) {
            node->topLeft = insert(node->topLeft, point, depth + 1);
        } else {
            node->topRight = insert(node->topRight, point, depth + 1);
        }

        return node;
    }

    // 在指定深度下递归搜索节点
    bool search(QuadTreeNode* node, Point point, int depth) {
        if (node == nullptr) {
            return false;
        }

        if (node->point.x == point.x && node->point.y == point.y) {
            return true;
        }

        // 根据点的位置选择象限
        if (point.x < node->point.x && point.y < node->point.y) {
            return search(node->bottomLeft, point, depth + 1);
        } else if (point.x >= node->point.x && point.y < node->point.y) {
            return search(node->bottomRight, point, depth + 1);
        } else if (point.x < node->point.x && point.y >= node->point.y) {
            return search(node->topLeft, point, depth + 1);
        } else {
            return search(node->topRight, point, depth + 1);
        }
    }

public:
    QuadTree(float minX, float minY, float maxX, float maxY, int depth) : root(nullptr), maxDepth(depth) {}

    // 插入一个点
    void insert(Point point) {
        root = insert(root, point, 0);
    }

    // 搜索一个点是否存在
    bool search(Point point) {
        return search(root, point, 0);
    }
};

int main() {
    QuadTree quadtree(0.0f, 0.0f, 100.0f, 100.0f, 4); // 创建四叉树,定义边界和最大深度

    Point point1(10.0f, 20.0f);
    Point point2(80.0f, 90.0f);

    quadtree.insert(point1); // 插入点1
    quadtree.insert(point2); // 插入点2

    Point searchPoint(80.0f, 90.0f);
    bool found = quadtree.search(searchPoint); // 搜索点2
    if (found) {
        std::cout << "Point found in the quadtree." << std::endl;
    } else {
        std::cout << "Point not found in the quadtree." << std::endl;
    }

    return 0;
}
相关推荐
之歆5 小时前
Spring AI入门到实战到原理源码-MCP
java·人工智能·spring
yangminlei6 小时前
Spring Boot3集成LiteFlow!轻松实现业务流程编排
java·spring boot·后端
qq_318121596 小时前
互联网大厂Java面试故事:从Spring Boot到微服务架构的技术挑战与解答
java·spring boot·redis·spring cloud·微服务·面试·内容社区
计算机毕设VX:Fegn08956 小时前
计算机毕业设计|基于springboot + vue医院设备管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
J_liaty6 小时前
Spring Boot整合Nacos:从入门到精通
java·spring boot·后端·nacos
Mr__Miss6 小时前
保持redis和数据库一致性(双写一致性)
数据库·redis·spring
阿蒙Amon7 小时前
C#每日面试题-Array和ArrayList的区别
java·开发语言·c#
daidaidaiyu7 小时前
Spring IOC 源码学习 一文学习完整的加载流程
java·spring
666HZ6667 小时前
数据结构2.0 线性表
c语言·数据结构·算法
Knight_AL7 小时前
Spring 事务传播行为 + 事务失效原因 + 传播行为为什么不用其他模式
数据库·sql·spring