Kanon icon indicating copy to clipboard operation
Kanon copied to clipboard

garbages mess up the screen

Open masuhar opened this issue 1 year ago • 0 comments

image 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()

masuhar avatar Aug 31 '23 04:08 masuhar