© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•5mo ago•
64 replies
Full Spectrum Wulfenite

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
  }
}
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
BehaviorTreeNode
(
BehaviorTreeSequenceNode
BehaviorTreeSequenceNode
,
BehaviorTreeSelectorNode
BehaviorTreeSelectorNode
,
BehaviorTreeExecutorNode
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
    }
}
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.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

✅ Deep copy struct
C#CC# / help
13mo ago
Create COPY of GitHub repo remote
C#CC# / help
4y ago
Deep Linking Widows
C#CC# / help
3y ago
❔ Deep clone variable
C#CC# / help
3y ago