Skip to content

Instantly share code, notes, and snippets.

@aalexeev239
Created February 2, 2021 18:03
Show Gist options
  • Save aalexeev239/921a733bc35a16c850569e823f3465da to your computer and use it in GitHub Desktop.
Save aalexeev239/921a733bc35a16c850569e823f3465da to your computer and use it in GitHub Desktop.
TreeNode.ts
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
export class TreeNode {
val: number;
left: TreeNode | null;
right: TreeNode | null;
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
this.val = val === undefined ? 0 : val;
this.left = left === undefined ? null : left;
this.right = right === undefined ? null : right;
}
static fromArray(arr: number[]): TreeNode {
if (arr.length === 0) {
return null;
}
return this.createByIndex(arr, 0);
}
private static createByIndex(arr: number[], index: number): TreeNode {
if (index >= arr.length || arr[index] === null) {
return null;
}
const val = arr[index];
const left = this.createByIndex(arr, index * 2 + 1);
const right = this.createByIndex(arr, index * 2 + 2);
return new TreeNode(val, left, right);
}
toArray() {
const res = [];
const root = this;
let current = new Set<[TreeNode, number]>([[root, 0]]);
while (current.size !== 0) {
const next = new Set<[TreeNode, number]>();
current.forEach(([{ val, left, right }, index]) => {
res[index] = val;
if (left) {
next.add([left, index * 2 + 1]);
}
if (right) {
next.add([right, index * 2 + 2]);
}
});
current = next;
}
for (let i = 0; i < res.length; i++) {
if (res[i] === undefined) {
res[i] = null;
}
}
return res;
}
validate(expected: number[]) {
const arr = this.toArray();
const maxLen = Math.max(arr.length, expected.length);
let correct = true;
for (let i = 0; i < maxLen; i++) {
const isOk = arr[i] === expected[i] ? '✅' : '❌';
if (!isOk) {
correct = false;
}
console.log(`${isOk} ${arr[i]} ${expected[i]}`);
}
return correct;
}
print(): string {
let res = '';
const arr = this.toArray();
const levels = this.getLevel(arr.length);
for (let i = arr.length; i < Math.pow(2, levels + 1); i++) {
arr[i] = null;
}
for (let l = 0; l < levels; l++) {
const cur = arr.slice(Math.pow(2, l) - 1, Math.pow(2, l + 1) - 1);
const gap = ' '.repeat(Math.pow(2, levels - l + 1) - 2);
const shift = ' '.repeat(Math.pow(2, levels - l));
res += `${shift}${cur.map(this.printValueWithPad).join(gap)}` + '\n';
}
console.log(res);
return res;
}
private printValueWithPad (x: number) {
if (x === null || x === undefined) {
return '__';
}
const space = x < 10 ? ' ' : '';
return `${space}${x}`;
}
private getLevel = (num: number) => {
let res = 0;
while (num > 0) {
num >>= 1;
res++;
}
return res;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment