public class Treenode {
int value;
Treenode left;
Treenode right;
Treenode(int value) {
this.value = value;
}
}
class TreeTest {
static void main(String[] args) {
List<Treenode> nodes = new ArrayList<Treenode>()
nodes.add(new Treenode(2))
nodes.add(new Treenode(3))
nodes.add(new Treenode(1))
nodes.add(new Treenode(0))
def root = this.createTree(nodes)
println root.value
}
static createTree(ArrayList<Treenode> list){
def parent = list.get(0)
while (list.size()>1) {
def left = list.get(0)
def right = list.get(1)
parent = new Treenode(2*left['value']+3*right['value'])
parent.left=left
parent.right=right
list.putAt(list.size(),parent)
list.remove(left)
list.remove(right)
}
parent
}
}