Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม

Point Quadtrees ในโครงสร้างข้อมูล

Point Quadtree (ควอดทรีแบบจุด)

ปรับจาก Binary Tree 來แทน 2D Point Data แบ่งลักษณะ Quadtrees ทั้งหมด

Efficient สำหรับ 2D Ordered Data Points: O(log n) Time (Expected) แต่ k-d Trees Surpass จริง สำหรับ Generalized Binary Search

Construction Algorithm

  1. Given Next Point to Insert → หา Cell ที่มันอยู่ → Add to Tree
  2. New Point แบ่ง Cell เป็น 4 Quadrants ด้วย Vertical/Horizontal Lines ผ่าน Point → Cells เป็น Rectangles (ไม่จำเป็น Square)
  3. แต่ละ Node เก็บ 1 Input Point

Height Sensitivity

Tree Height ขึ้นกับ Insertion Order: Insert ผิดลำดับ → Height = O(n) (กลายเป็น Linked List)
ถ้า Point Set เป็น Static → Pre-process สร้าง Balanced Tree ได้

Node Structure

คล้าย Binary Tree Node แต่มี 4 Pointers (แทน Left/Right):

  • quad['NW'], quad['NE'], quad['SW'], quad['SE']
  • NW=North-West, NE=North-East, SW=South-West, SE=South-East
  • Point: Key (x, y coordinates) + Value (เช่น Name)

ตัวอย่างโครงสร้าง Node (C-style)

struct PointQuadNode {
    double x, y;           // Key coordinates
    char* value;           // Associated data
    struct PointQuadNode *NW, *NE, *SW, *SE; // 4 Quadrant pointers
};