You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The code below first creates a list of leaves, and then builds a tree by combining leaves. After creation, the list nodes no longer needed, but they stay on the canvas.
It would be nice to hide the nodes that are not reachable from local variables.
class Leaf {
constructor(char, count) {
this.char = char
this.count = count
}
}
class Node {
constructor(left,right) {
this.left = left
this.right = right
this.count= left.count + right.count
}
}
class Pool {
constructor(tree, next) {
this.tree = tree
this.next = next
}
build() {
var n = new Node(this.tree, this.next.tree)
return this.next.next.insert(n)
}
insert(newNode) {
if (newNode.count <= this.tree.count) {
return new Pool(newNode, this)
} else {
return new Pool(this.tree, this.next.insert(newNode))
}
}
}
class Sentinel {
insert(newNode) {
return new Pool(newNode, this)
}
}
var a = new Leaf("A",10)
var b = new Leaf("B", 5)
var c = new Leaf("C", 2)
var candidate = new Pool(a, new Pool(b, new Pool(c, new Sentinel())))
candidate = candidate.build()
candidate = candidate.build()
The text was updated successfully, but these errors were encountered:
The code below first creates a list of leaves, and then builds a tree by combining leaves. After creation, the list nodes no longer needed, but they stay on the canvas.
It would be nice to hide the nodes that are not reachable from local variables.
The text was updated successfully, but these errors were encountered: