Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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