Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created February 10, 2018 20:47
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/d8eab5dade4e4269dd5d6014e03e91dc to your computer and use it in GitHub Desktop.
Save jianminchen/d8eab5dade4e4269dd5d6014e03e91dc to your computer and use it in GitHub Desktop.
H tree - Feb. 10 - 12 pm mock interview - JavaScript
var totalLines = 0;
const drawLine = (x1, y1, x2, y2) => {
totalLines += 1;
let x = (x1 - x2);
let y = (y1 - y2);
let distance = Math.sqrt(x*x + y*y);
console.log('length', distance);
console.log('totalLines', totalLines);
};
const drawHTree = (x, y, length, depth) => {
if (depth == 0)
return;
let half_length = length / 2;
let top_y = y + half_length;
let bottom_y = y - half_length;
let left_x = x - half_length;
let right_x = x + half_length;
// Middle line
drawLine(left_x, y, right_x, y);
// Left line
drawLine(left_x, top_y, left_x, bottom_y);
// Right line
drawLine(right_x, top_y, right_x, bottom_y);
let new_length = length / Math.sqrt(2);
drawHTree(left_x, top_y, new_length, depth - 1);
drawHTree(left_x, bottom_y, new_length, depth - 1);
drawHTree(right_x, top_y, new_length, depth - 1);
drawHTree(right_x, bottom_y, new_length, depth - 1);
};
drawHTree(0, 0, 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment