Skip to content

Commit f2a3eee

Browse files
author
Jacob Priddy
committed
Added maze deallocation.
1 parent 0fc8c2a commit f2a3eee

File tree

5 files changed

+28
-50
lines changed

5 files changed

+28
-50
lines changed

PathFinder/Globals.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct Coordinate
1919
struct Branch
2020
{
2121

22-
Branch(Coordinate a)
22+
Branch(Coordinate a = Coordinate())
2323
{
2424
Left = NULL;
2525
Right = NULL;
@@ -30,14 +30,6 @@ struct Branch
3030
Pos = a;
3131
}
3232

33-
~Branch()
34-
{
35-
delete Left;
36-
delete Right;
37-
delete Up;
38-
delete Down;
39-
}
40-
4133
//Points to the left branch
4234
Branch* Left;
4335

@@ -54,7 +46,7 @@ struct Branch
5446
int Data = -1;
5547

5648
//Coordinate of the branch.
57-
Coordinate Pos = Coordinate();
49+
Coordinate Pos;
5850
};
5951

6052
#endif

PathFinder/PathFinder.cpp

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,39 @@
11
#include "PathFinder.h"
22
#include "Globals.h"
33

4-
5-
PathFinder::PathFinder()
6-
{
7-
}
8-
9-
104
PathFinder::~PathFinder()
115
{
12-
13-
14-
6+
this->NavDeepDelete(this->TreeRoot);
157
}
168

179
bool PathFinder::NavDeepDelete(Branch *Check)
1810
{
19-
// this deallocates the memory
20-
21-
bool u = false;
22-
bool d = false;;
23-
bool l = false;
24-
bool r = false;;
25-
26-
while (true)
11+
if (Check)
2712
{
28-
NavDeepDelete(Check->Up);
29-
NavDeepDelete(Check->Down);
30-
NavDeepDelete(Check->Left);
31-
NavDeepDelete(Check->Right);
13+
if(
14+
this->NavDeepDelete(Check->Up) &&
15+
this->NavDeepDelete(Check->Down) &&
16+
this->NavDeepDelete(Check->Left) &&
17+
this->NavDeepDelete(Check->Right)
18+
)
19+
{
20+
delete Check;
21+
return true;
22+
}
23+
return false;
3224
}
33-
34-
if (u == true && d == true && l == true && r == true)
25+
else
3526
{
36-
std::cout << "Deleted a branch!" << std::endl;
37-
delete Check;
3827
return true;
3928
}
40-
4129
}
4230

43-
PathFinder::PathFinder(std::vector<std::vector<int>> maze, Coordinate start, Coordinate end)
31+
PathFinder::PathFinder(std::vector<std::vector<int>> maze, Coordinate start, Coordinate end, Branch * Root)
4432
{
4533
Maze = maze;
4634
StartOfMaze = start;
4735
EndOfMaze = end;
36+
this->TreeRoot = Root;
4837
}
4938

5039

PathFinder/PathFinder.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
class PathFinder
88
{
9+
protected:
10+
Branch * TreeRoot;
911
public:
10-
PathFinder();
11-
~PathFinder();
12-
PathFinder(std::vector<std::vector<int>> maze, Coordinate start, Coordinate end);
12+
PathFinder(std::vector<std::vector<int>> maze, Coordinate start, Coordinate end, Branch * Root);
1313

1414

1515
std::vector<std::vector<int>> Maze;
@@ -34,6 +34,6 @@ class PathFinder
3434

3535
//Checks if coordinates are indeed inside maze
3636
bool CheckValidCoordinates(Coordinate a);
37-
37+
~PathFinder();
3838
};
3939

PathFinder/PathFinder.vcxproj.filters

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<Filter>Header Files</Filter>
2828
</ClInclude>
2929
<ClInclude Include="Globals.h">
30-
<Filter>Source Files</Filter>
30+
<Filter>Header Files</Filter>
3131
</ClInclude>
3232
</ItemGroup>
3333
</Project>

PathFinder/Source.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,17 @@ int main()
4040
Coordinate BeginMaze = Coordinate(0,0);
4141
Coordinate EndMaze = Coordinate(6,21);
4242

43-
PathFinder A = PathFinder(Maze, BeginMaze, EndMaze);
43+
// This gets automatically deallocated when the program ends.
44+
Branch * Root = new Branch(BeginMaze);
4445

45-
46-
Branch Root(BeginMaze);
47-
48-
Branch *RealRoot = &Root;
46+
PathFinder MazeSolver = PathFinder(Maze, BeginMaze, EndMaze, Root);
4947

5048
vector<Coordinate> Path;
5149
Path.push_back(BeginMaze);
5250

53-
A.BranchAll(BeginMaze, Path, RealRoot);
51+
MazeSolver.BranchAll(BeginMaze, Path, Root);
5452

55-
Path = A.GetShortest(EndMaze);
53+
Path = MazeSolver.GetShortest(EndMaze);
5654

5755
cout << "Path Size:" << endl;
5856
cout << Path.size() << endl;
@@ -91,7 +89,6 @@ int main()
9189
}
9290
}
9391

94-
A.NavDeepDelete(RealRoot);
9592

9693
//Pauses Program
9794
system("PAUSE");

0 commit comments

Comments
 (0)