数据结构——四叉树

四叉树(Quadtree)是一种用于表示和管理二维空间的树状数据结构。它将二维空间递归地分割成四个象限,每个象限可以继续分割,以实现对空间的更精细的划分。四叉树通常用于解决空间搜索和查询问题,例如碰撞检测、图像压缩、地理信息系统等领域。
在这里插入图片描述
特别适合大规模的广阔室外场景管理。一般来说如果游戏场景是基于地形的(甚至没有高度)(如城市、平原、2D场景),那么适合用四叉树来管理。而如果游戏场景在高度轴上也有大量物体需要管理(如太空、高山),那么适合用八叉树来管理。

#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;
}

  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Keil MDK中使用树可以通过定义树节点结构体和使用指针来实现。下面是一个简单的示例代码: ```c #include <stdio.h> #include <stdlib.h> // 定义树节点结构体 typedef struct node { int data; struct node *left; struct node *right; } TreeNode; // 创建节点 TreeNode* create_node(int data) { TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode)); node->data = data; node->left = NULL; node->right = NULL; return node; } // 插入节点 TreeNode* insert_node(TreeNode* root, int data) { if (root == NULL) { return create_node(data); } if (data < root->data) { root->left = insert_node(root->left, data); } else { root->right = insert_node(root->right, data); } return root; } // 计算树的深度 int tree_depth(TreeNode* root) { if (root == NULL) { return 0; } int left_depth = tree_depth(root->left); int right_depth = tree_depth(root->right); return (left_depth > right_depth ? left_depth : right_depth) + 1; } int main() { // 创建根节点 TreeNode* root = create_node(5); // 插入节点 root = insert_node(root, 3); root = insert_node(root, 7); root = insert_node(root, 1); root = insert_node(root, 4); root = insert_node(root, 6); root = insert_node(root, 8); // 计算树的深度并输出 int depth = tree_depth(root); printf("Tree Depth: %d\n", depth); return 0; } ``` 在这个示例代码中,我们首先定义了一个树节点结构体,包含了节点数据和左右子节点指针。然后我们定义了创建节点和插入节点的函数,使用递归的方式实现。最后我们定义了计算树深度的函数,并在main函数中使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值