about summary refs log tree commit diff
diff options
context:
space:
mode:
authorhanemile <hanemile@protonmail.com>2018-11-06 16:16:09 +0100
committerhanemile <hanemile@protonmail.com>2018-11-06 16:16:09 +0100
commitceaa8056213ac425d4a808c4b5685d9d6abda81e (patch)
tree5f5d3700fd29d021698f99607bdc43924bbc7591
parenta1860696f2c27ef0745a104002a4eb5a9de5f485 (diff)
Rebuild the function that builds the quadtree and changes all the empty quadtrees from the example to nil
-rw-r--r--structs.go14
-rw-r--r--structs_test.go50
2 files changed, 42 insertions, 22 deletions
diff --git a/structs.go b/structs.go
index 5f916bd..76964fa 100644
--- a/structs.go
+++ b/structs.go
@@ -154,18 +154,22 @@ func (quadtree *Quadtree) Insert(point Coord) {
 	quadtree.pointsSlice = append(quadtree.pointsSlice, point)
 }
 
+// Print() is a method that can be used on a quadtree.
+// It prints all the root nodes of the quadtree recursively
+// The tree is printed from the bottom up in the following order: NW, NE, SW, SE
 func (quadtree *Quadtree) Print() {
-	if quadtree.northEast != nil {
-		quadtree.northEast.Print()
-	}
+	// Define an end condition: if the quadtree-leaf is nil, the root does not continue
 	if quadtree.northWest != nil {
 		quadtree.northWest.Print()
 	}
-	if quadtree.southEast != nil {
-		quadtree.southEast.Print()
+	if quadtree.northEast != nil {
+		quadtree.northEast.Print()
 	}
 	if quadtree.southWest != nil {
 		quadtree.southWest.Print()
 	}
+	if quadtree.southEast != nil {
+		quadtree.southEast.Print()
+	}
 	fmt.Println(quadtree)
 }
diff --git a/structs_test.go b/structs_test.go
index 3abb47f..0989753 100644
--- a/structs_test.go
+++ b/structs_test.go
@@ -165,6 +165,7 @@ func TestInsert(t *testing.T) {
 func TestPrint(t *testing.T) {
 
 	// Define a testQuadTree
+
 	var testQuadTree = Quadtree{
 		nodeCapacity: 0,
 		boundary: BoundingBox{
@@ -185,11 +186,26 @@ func TestPrint(t *testing.T) {
 				halfDimension: 2,
 			},
 			pointsSlice: nil,
-			northWest:   &Quadtree{},
-			northEast:   &Quadtree{},
-			southWest:   &Quadtree{},
-			southEast:   &Quadtree{},
-			depth:       1,
+			northWest: &Quadtree{
+				nodeCapacity: 0,
+				boundary: BoundingBox{
+					center: Coord{
+						x: 3,
+						y: 3,
+					},
+					halfDimension: 1,
+				},
+				pointsSlice: nil,
+				northWest:   nil,
+				northEast:   nil,
+				southWest:   nil,
+				southEast:   nil,
+				depth:       2,
+			},
+			northEast: nil,
+			southWest: nil,
+			southEast: nil,
+			depth:     1,
 		},
 		northEast: &Quadtree{
 			nodeCapacity: 0,
@@ -201,10 +217,10 @@ func TestPrint(t *testing.T) {
 				halfDimension: 2,
 			},
 			pointsSlice: nil,
-			northWest:   &Quadtree{},
-			northEast:   &Quadtree{},
-			southWest:   &Quadtree{},
-			southEast:   &Quadtree{},
+			northWest:   nil,
+			northEast:   nil,
+			southWest:   nil,
+			southEast:   nil,
 			depth:       1,
 		},
 		southWest: &Quadtree{
@@ -217,10 +233,10 @@ func TestPrint(t *testing.T) {
 				halfDimension: 2,
 			},
 			pointsSlice: nil,
-			northWest:   &Quadtree{},
-			northEast:   &Quadtree{},
-			southWest:   &Quadtree{},
-			southEast:   &Quadtree{},
+			northWest:   nil,
+			northEast:   nil,
+			southWest:   nil,
+			southEast:   nil,
 			depth:       1,
 		},
 		southEast: &Quadtree{
@@ -233,10 +249,10 @@ func TestPrint(t *testing.T) {
 				halfDimension: 2,
 			},
 			pointsSlice: nil,
-			northWest:   &Quadtree{},
-			northEast:   &Quadtree{},
-			southWest:   &Quadtree{},
-			southEast:   &Quadtree{},
+			northWest:   nil,
+			northEast:   nil,
+			southWest:   nil,
+			southEast:   nil,
 			depth:       1,
 		},
 		depth: 0,