Skip to content

Instantly share code, notes, and snippets.

@CharStiles
Last active December 20, 2018 00:16
Show Gist options
  • Save CharStiles/3e07a19c6a58214850cfef6447a5e9ed to your computer and use it in GitHub Desktop.
Save CharStiles/3e07a19c6a58214850cfef6447a5e9ed to your computer and use it in GitHub Desktop.
var tree
var pad = 10
class Node{
constructor(v){ // v is a string
this.val = v;
}
}
class Tree{
constructor(){
this.edges = [] // in pairs
this.nodes = []
}
addNode(v){ // v is node type
this.nodes[v.val] = v // indexed by value
}
addEdge(parent,child){// list parent first both are nodes
this.edges.push([parent,child])
}
// v is a string
find(v){
if (this.nodes[v] != undefined){
console.log("found")
return this.nodes[v]
}
else{
console.log("not found")
return undefined
}
}
}
function setup() {
createCanvas(400, 400);
tree = new Tree()
var root = new Node("I am root")
tree.addNode(root)
var n1 = new Node("n1")
tree.addNode(n1)
var n2 = new Node("n2")
tree.addNode(n2)
var n3 = new Node("n3")
tree.addNode(n3)
tree.addEdge(root,n1)
tree.addEdge(root,n2)
tree.addEdge(root,n3)
var f = tree.find("not in the tree")
console.log(f);
var t = tree.find("n1")
console.log(t);
}
setup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment