|
| 1 | +package com.thealgorithms.datastructures.trees; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +/** |
| 7 | + * Point is a simple class that represents a point in 2D space. |
| 8 | + * |
| 9 | + * @see <a href="https://en.wikipedia.org/wiki/Point_(geometry)">Point</a> |
| 10 | + * @author <a href="https://github.com/sailok">Sailok Chinta</a> |
| 11 | + */ |
| 12 | +class Point { |
| 13 | + public double x; |
| 14 | + public double y; |
| 15 | + |
| 16 | + Point(double x, double y) { |
| 17 | + this.x = x; |
| 18 | + this.y = y; |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * BoundingBox is a simple class that represents a bounding box in 2D space. |
| 24 | + * |
| 25 | + * @see <a href="https://en.wikipedia.org/wiki/Bounding_box">Bounding Box</a> |
| 26 | + * @author <a href="https://github.com/sailok">Sailok Chinta</a> |
| 27 | + */ |
| 28 | +class BoundingBox { |
| 29 | + public Point center; |
| 30 | + public double halfWidth; |
| 31 | + |
| 32 | + BoundingBox(Point center, double halfWidth) { |
| 33 | + this.center = center; |
| 34 | + this.halfWidth = halfWidth; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Checks if the point is inside the bounding box |
| 39 | + * |
| 40 | + * @param point The point to check |
| 41 | + * @return true if the point is inside the bounding box, false otherwise |
| 42 | + */ |
| 43 | + public boolean containsPoint(Point point) { |
| 44 | + return point.x >= center.x - halfWidth && point.x <= center.x + halfWidth && point.y >= center.y - halfWidth && point.y <= center.y + halfWidth; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Checks if the bounding box intersects with the other bounding box |
| 49 | + * |
| 50 | + * @param otherBoundingBox The other bounding box |
| 51 | + * @return true if the bounding box intersects with the other bounding box, false otherwise |
| 52 | + */ |
| 53 | + public boolean intersectsBoundingBox(BoundingBox otherBoundingBox) { |
| 54 | + return otherBoundingBox.center.x - otherBoundingBox.halfWidth <= center.x + halfWidth && otherBoundingBox.center.x + otherBoundingBox.halfWidth >= center.x - halfWidth && otherBoundingBox.center.y - otherBoundingBox.halfWidth <= center.y + halfWidth |
| 55 | + && otherBoundingBox.center.y + otherBoundingBox.halfWidth >= center.y - halfWidth; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * QuadTree is a tree data structure that is used to store spatial information |
| 61 | + * in an efficient way. |
| 62 | + * |
| 63 | + * This implementation is specific to Point QuadTrees |
| 64 | + * |
| 65 | + * @see <a href="https://en.wikipedia.org/wiki/Quadtree">Quad Tree</a> |
| 66 | + * @author <a href="https://github.com/sailok">Sailok Chinta</a> |
| 67 | + */ |
| 68 | +public class QuadTree { |
| 69 | + private final BoundingBox boundary; |
| 70 | + private final int capacity; |
| 71 | + |
| 72 | + private List<Point> pointList; |
| 73 | + private boolean divided; |
| 74 | + private QuadTree northWest; |
| 75 | + private QuadTree northEast; |
| 76 | + private QuadTree southWest; |
| 77 | + private QuadTree southEast; |
| 78 | + |
| 79 | + public QuadTree(BoundingBox boundary, int capacity) { |
| 80 | + this.boundary = boundary; |
| 81 | + this.capacity = capacity; |
| 82 | + |
| 83 | + this.pointList = new ArrayList<>(); |
| 84 | + this.divided = false; |
| 85 | + this.northWest = null; |
| 86 | + this.northEast = null; |
| 87 | + this.southWest = null; |
| 88 | + this.southEast = null; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Inserts a point into the tree |
| 93 | + * |
| 94 | + * @param point The point to insert |
| 95 | + * @return true if the point is successfully inserted, false otherwise |
| 96 | + */ |
| 97 | + public boolean insert(Point point) { |
| 98 | + if (point == null) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + // Ignore points that don't belong to this quad tree |
| 103 | + if (!boundary.containsPoint(point)) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + // if the space is not already occupied, add it to the list |
| 108 | + if (pointList.size() < capacity) { |
| 109 | + pointList.add(point); |
| 110 | + return true; |
| 111 | + } |
| 112 | + |
| 113 | + // if subdivision hasn't happened, divide the tree |
| 114 | + if (!divided) { |
| 115 | + subDivide(); |
| 116 | + } |
| 117 | + |
| 118 | + // try to add the point in one of the four quadrants |
| 119 | + if (northWest.insert(point)) { |
| 120 | + return true; |
| 121 | + } |
| 122 | + |
| 123 | + if (northEast.insert(point)) { |
| 124 | + return true; |
| 125 | + } |
| 126 | + |
| 127 | + if (southWest.insert(point)) { |
| 128 | + return true; |
| 129 | + } |
| 130 | + |
| 131 | + if (southEast.insert(point)) { |
| 132 | + return true; |
| 133 | + } |
| 134 | + |
| 135 | + return false; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Create four children that fully divide this quad into four quads of equal area |
| 140 | + */ |
| 141 | + private void subDivide() { |
| 142 | + double quadrantHalfWidth = boundary.halfWidth / 2; |
| 143 | + |
| 144 | + northWest = new QuadTree(new BoundingBox(new Point(boundary.center.x - quadrantHalfWidth, boundary.center.y + quadrantHalfWidth), quadrantHalfWidth), this.capacity); |
| 145 | + northEast = new QuadTree(new BoundingBox(new Point(boundary.center.x + quadrantHalfWidth, boundary.center.y + quadrantHalfWidth), quadrantHalfWidth), this.capacity); |
| 146 | + southWest = new QuadTree(new BoundingBox(new Point(boundary.center.x - quadrantHalfWidth, boundary.center.y - quadrantHalfWidth), quadrantHalfWidth), this.capacity); |
| 147 | + southEast = new QuadTree(new BoundingBox(new Point(boundary.center.x + quadrantHalfWidth, boundary.center.y - quadrantHalfWidth), quadrantHalfWidth), this.capacity); |
| 148 | + divided = true; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Queries all the points that intersect with the other bounding box |
| 153 | + * |
| 154 | + * @param otherBoundingBox The other bounding box |
| 155 | + * @return List of points that intersect with the other bounding box |
| 156 | + */ |
| 157 | + public List<Point> query(BoundingBox otherBoundingBox) { |
| 158 | + List<Point> points = new ArrayList<>(); |
| 159 | + |
| 160 | + if (!boundary.intersectsBoundingBox(otherBoundingBox)) { |
| 161 | + return points; |
| 162 | + } |
| 163 | + |
| 164 | + // filter the points that intersect with the other bounding box |
| 165 | + points.addAll(pointList.stream().filter(otherBoundingBox::containsPoint).toList()); |
| 166 | + |
| 167 | + if (divided) { |
| 168 | + points.addAll(northWest.query(otherBoundingBox)); |
| 169 | + points.addAll(northEast.query(otherBoundingBox)); |
| 170 | + points.addAll(southWest.query(otherBoundingBox)); |
| 171 | + points.addAll(southEast.query(otherBoundingBox)); |
| 172 | + } |
| 173 | + |
| 174 | + return points; |
| 175 | + } |
| 176 | +} |
0 commit comments