Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created February 10, 2018 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/2afc390c3bd4eefea9227907e94cd337 to your computer and use it in GitHub Desktop.
Save jianminchen/2afc390c3bd4eefea9227907e94cd337 to your computer and use it in GitHub Desktop.
H Tree - Feb. 10, 10 am - code review peer's code
//
class HTree
{
public static void DrawHTree (int x, int y, int length, int depth) {
if (length == 0) {
return;
}
DrawHTree(x, y, length, depth, 0);
}
public static void DrawHTree (int x, int y, int length, int depth, int currentDepth) {
if (depth == currentDepth) {
return;
}
int halfLength = length / 2; // 1
int xLeft = x - halfLength; // -1
int xRight = x + halfLength; // 1
int yTop = y + halfLength; // 1
int yBottom = y - halfLength; // -1
DrawLine(xLeft, y, xRight, y); // (-1,0) -> (1,0)
DrawLine(xLeft, yTop, xLeft, yBottom); // (-1,1) -> (-1,-1)
DrawLine(xRight, yTop, xRight, yBottom); // (1, 1) -> (1,-1)
int newLength = length / Math.Sqrt(2);
currentDepth++;
DrawHTree(xLeft, yTop, newLength, currentDepth); // (-1,1) 1
DrawHTree(xLeft, yBottom, newLength, currentDepth); // (-1,-1) 1
DrawHTree(xRight, yTop, newLength, currentDepth); //
DrawHTree(xRight, yBottom, newLength, currentDepth);
}
public static void DrawLine(int x1, int y1, int x2, int y2) {
Console.WriteLine("(" + x1 + "," + y1 + ")");
Console.WriteLine("(" + x2 + "," + y2 + ")");
}
// if (depth == givenLength) { // 0, 1, 2
//return;
//}
// halfLength = length / 2 => 1
// (x - halfLength, y) (x + halfLength, y)
// (x + halfLength, y + halfLength) (x + halfLength, y - halfLength)
// (x - halfLength, y + halfLength) (x - halfLength, y - halfLength)
// drawHTree (x + halfLength, y + halfLength, length/Sqrt(2), depth)
// drawHTree (x + halfLength, y - halfLength, length/Sqrt(2), depth)
//drawHTree (x - halfLength, y + halfLength, length/Sqrt(2), depth)
//drawHTree (x - halfLength, y - halfLength, length/Sqrt(2), depth)
static void Main()
{
System.Console.WriteLine("Practice makes Perfect!");
}
}
// x, y (0,0) length (2)
// if (depth == givenLength) { // 0, 1, 2
//return;
//}
// halfLength = length / 2 => 1
// (x - halfLength, y) (x + halfLength, y)
// (x + halfLength, y + halfLength) (x + halfLength, y - halfLength)
// (x - halfLength, y + halfLength) (x - halfLength, y - halfLength)
// drawHTree (x + halfLength, y + halfLength, length/Sqrt(2), depth)
// drawHTree (x + halfLength, y - halfLength, length/Sqrt(2), depth)
//drawHTree (x - halfLength, y + halfLength, length/Sqrt(2), depth)
//drawHTree (x - halfLength, y - halfLength, length/Sqrt(2), depth)
O(4^d) -> 1 + 4 + 4 * 4 + ... + 4^(depth - 1) = 4^depth - 1 /(4- 1)= O(4^depth)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment