Created
September 23, 2020 13:14
-
-
Save AlexAegis/a72d46c96882898941c554bd284e2cb7 to your computer and use it in GitHub Desktop.
Inverting a binary tree in one line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class Node<T> { | |
public left?: Node<T>; | |
public right?: Node<T>; | |
public constructor(public value: T) {} | |
public invert(): Node<T> { | |
return ([this.left, this.right] = [this.right?.invert(), this.left?.invert()]) && this; | |
} | |
public toString(): string { | |
return `[${this.left?.toString() ?? '_'},${this.value},${this.right?.toString() ?? '_'}]`; | |
} | |
} | |
const b = new Node(1); | |
const a = new Node(2); | |
const d = new Node(3); | |
const c = new Node(4); | |
const e = new Node(5); | |
a.left = b; | |
a.right = c; | |
c.left = d; | |
c.right = e; | |
/* | |
A | |
/ \ | |
B C | |
/ \ | |
D E | |
*/ | |
console.log(a.toString()); // [[_,1,_],2,[[_,3,_],4,[_,5,_]]] | |
a.invert(); | |
console.log(a.toString()); // [[[_,5,_],4,[_,3,_]],2,[_,1,_]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment