Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

garbages mess up the screen #47

Open
masuhar opened this issue Aug 31, 2023 · 0 comments
Open

garbages mess up the screen #47

masuhar opened this issue Aug 31, 2023 · 0 comments

Comments

@masuhar
Copy link
Contributor

masuhar commented Aug 31, 2023

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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant