Skip to content

Commit e263edc

Browse files
authored
Add QuadTree data structure (#5681)
1 parent e38611e commit e263edc

File tree

3 files changed

+235
-0
lines changed

3 files changed

+235
-0
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@
218218
* [PostOrderTraversal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/PostOrderTraversal.java)
219219
* [PreOrderTraversal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/PreOrderTraversal.java)
220220
* [PrintTopViewofTree](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/PrintTopViewofTree.java)
221+
* [QuadTree](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/QuadTree.java)
221222
* [RedBlackBST](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/RedBlackBST.java)
222223
* [SameTreesCheck](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/SameTreesCheck.java)
223224
* [SegmentTree](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/SegmentTree.java)
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.thealgorithms.datastructures.trees;
2+
3+
import java.util.List;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.Test;
6+
7+
public class QuadTreeTest {
8+
int quadTreeCapacity = 4;
9+
BoundingBox boundingBox = new BoundingBox(new Point(0, 0), 500);
10+
QuadTree quadTree = new QuadTree(boundingBox, quadTreeCapacity);
11+
12+
@Test
13+
public void testNullPointInsertIntoQuadTree() {
14+
Assertions.assertFalse(quadTree.insert(null));
15+
}
16+
17+
@Test
18+
public void testInsertIntoQuadTree() {
19+
Assertions.assertTrue(quadTree.insert(new Point(10, -10)));
20+
Assertions.assertTrue(quadTree.insert(new Point(-10, 10)));
21+
Assertions.assertTrue(quadTree.insert(new Point(-10, -10)));
22+
Assertions.assertTrue(quadTree.insert(new Point(10, 10)));
23+
Assertions.assertFalse(quadTree.insert(new Point(1050, 1050)));
24+
}
25+
26+
@Test
27+
public void testInsertIntoQuadTreeAndSubDivide() {
28+
Assertions.assertTrue(quadTree.insert(new Point(10, -10)));
29+
Assertions.assertTrue(quadTree.insert(new Point(-10, 10)));
30+
Assertions.assertTrue(quadTree.insert(new Point(-10, -10)));
31+
Assertions.assertTrue(quadTree.insert(new Point(10, 10)));
32+
Assertions.assertTrue(quadTree.insert(new Point(-100, 100)));
33+
Assertions.assertTrue(quadTree.insert(new Point(100, -101)));
34+
Assertions.assertTrue(quadTree.insert(new Point(-100, -100)));
35+
Assertions.assertTrue(quadTree.insert(new Point(100, 100)));
36+
}
37+
38+
@Test
39+
public void testQueryInQuadTree() {
40+
quadTree.insert(new Point(10, -10));
41+
quadTree.insert(new Point(-10, 10));
42+
quadTree.insert(new Point(-10, -10));
43+
quadTree.insert(new Point(10, 10));
44+
quadTree.insert(new Point(-100, 100));
45+
quadTree.insert(new Point(100, -100));
46+
quadTree.insert(new Point(-100, -100));
47+
quadTree.insert(new Point(100, 100));
48+
49+
List<Point> points = quadTree.query(new BoundingBox(new Point(0, 0), 100));
50+
Assertions.assertEquals(8, points.size());
51+
52+
points = quadTree.query(new BoundingBox(new Point(5, 5), 5));
53+
Assertions.assertEquals(1, points.size());
54+
55+
points = quadTree.query(new BoundingBox(new Point(-200, -200), 5));
56+
Assertions.assertEquals(0, points.size());
57+
}
58+
}

0 commit comments

Comments
 (0)