PDA

Просмотр полной версии : Собственный класс реализующий дерево


snorik
20.10.2006, 21:32
Есть задача: Создать класс, который реализует дерево определенной глубины с заданным кол-вом разветвлений от каждой ветви.

Сделал, но неправильно работает Array в классе, подскажите как решить проблему!

мой класс выглядит так:

class cTree {
private var branchName:String;
private var currentDepth:Number;
private var numberOfBranches:Number;
private var childBranches:Array = new Array();

public function cTree(brname:String, cdepth:Number, branchnes:Number)
{
this.branchName = brname;
this.currentDepth = cdepth;
this.numberOfBranches = branchnes;
if(this.currentDepth > 0) {
for(var n = 0; n < this.numberOfBranches; n++)
this.childBranches.push(new cTree(this.branchName + " - " + n,this.currentDepth - 1, this.numberOfBranches));
}
}

public function traceTree():Void
{
trace("depth:" + this.currentDepth + " nodeName:" + this.branchName);
if(this.currentDepth > 0) {
for(var n = 0; n < numberOfBranches; n++)
childBranches[n].traceTree();
}
}
}

silin
20.10.2006, 22:57
надо в конструкторе явно прописать создание нового массива
if(currentDepth > 0) {
childBranches=[];
for(var n = 0; n < numberOfBranches; n++)
childBranches.push(new cTree(branchName + " - " + n,currentDepth - 1, numberOfBranches));
}

snorik
21.10.2006, 02:32
огромное спасибо! заработало! тогда, как я понял, new Array() вначале уже не нужен