Deep copy of a NaryTree

I am trying to implement a copy constructor for a BehaviorTree (special case NaryTree). The tree is defined as:

public class BehaviorTree {
  private BehaviorTreeNode root;
  public BehaviorTree() {
    root = new BehaviorTreeNode();
  }
  public BehaviorTree(BehaviorTree other) {
    // Implementation goes here
  }
}


And the nodes are derived classes of BehaviorTreeNode (BehaviorTreeSequenceNode, BehaviorTreeSelectorNode, BehaviorTreeExecutorNode). The base class is defined as:

public abstract class BehaviorTreeNodeBase
{
    protected static int ID;
    protected int _id;
    public BehaviorTreeNodeBase firstChildNode;
    public BehaviorTreeNodeBase siblingNode;

    public virtual BehaviorTreeState EvaluateNode(IBehaviorTreeAgent instance) { return BehaviorTreeState.None; }
    public abstract void AddNode(BehaviorTreeNodeBase other);

    public abstract void RemoveNode(BehaviorTreeNodeBase other);

    public int id
    {
        get { return _id; }
    }
}

public class BehaviorTreeNode : BehaviorTreeNodeBase
{
    public BehaviorTreeNode() {
        _id = ID;
        ID++;
    }

    public BehaviorTreeNode(BehaviorTreeNode other)
    {
        this._id = other._id;
    }

    public override void AddNode(BehaviorTreeNodeBase other)
    {
        //Add child node
    }

    public override void RemoveNode(BehaviorTreeNodeBase other)
    {
        //Remove child node
    }
}


I am looking for some direction on how to approach this. I know I could either do an iterative approach using a stack or a queue, or recursive, but I am going back and forth between the two and not sure which would be better for a desktop application.
Was this page helpful?