class ThreadedBinaryTree {
constructor (entry, left=null, right=null) {
[this.entry,this.left,this.right] = [entry,left,right]
}
*inorder () {
if (this.left && !this.left.isThread) yield* this.left.to.inorder()
yield this;
if (this.right && !this.right.isThread) yield* this.right.to.inorder()
}
thread () {
let a = [...this.inorder()];
for (let i = 0; i < a.length; i++) {
if (i > 0 && !a[i].left) a[i].left = new Link(a[i-1], true);
if (i+1 < a.length && !a[i].right) a[i].right = new Link(a[i+1], true);
}
return this
}
inorderSuccessor () {
let u = this.right;
if (!u) return null;
if (u.isThread) return u.to;
while (u.to.left && !u.to.left.isThread) {
u = u.to.left
}
if (u) return u.to
return null
}
inorderPredecessor () {
}
}