prg-titech / Kanon

A live programming environment specialized for data structure programming.
https://prg-titech.github.io/Kanon/
MIT License
68 stars 4 forks source link

garbages mess up the screen #47

Open masuhar opened 1 year ago

masuhar commented 1 year ago

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