/// <summary>
 /// Sets the child in the given branch.
 /// </summary>
 public void SetChild(Branch branch, DecompositionNode child)
 {
     if (branch == Branch.Left)
     {
         this.Left = child;
     }
     else
     {
         this.Right = child;
     }
 }
        /// <summary>
        /// A deep copy of this subtree.
        /// </summary>
        /// <param name="tree">The original tree.</param>
        /// <param name="parent">The parent of the copied node.</param>
        /// <returns>A deep copy of the node.</returns>
        public DecompositionNode CopyTree(DecompositionTree tree, DecompositionNode parent)
        {
            DecompositionNode node = tree.Nodes[this.Index] = new DecompositionNode(this, tree);

            node.Parent = parent;
            if (!this.IsLeaf)
            {
                node.Left  = this.Left.CopyTree(tree, node);
                node.Right = this.Right.CopyTree(tree, node);
            }
            return(node);
        }
 /// <summary>
 /// Attach the child to the parent in the specific branch. Set the child as the root of the tree if no parent is provided.
 /// </summary>
 public void Attach(DecompositionNode parent, DecompositionNode child, Branch branch)
 {
     child.Parent = parent;
     child.Branch = branch;
     if (parent == null)
     {
         this.Root = child;
     }
     else
     {
         parent.SetChild(branch, child);
     }
 }
 public DecompositionNode(DecompositionNode node, DecompositionTree tree = null)
 {
     this.Set          = new BitSet(node.Set);
     this.Left         = node.Left;
     this.Right        = node.Right;
     this.Parent       = node.Parent;
     this.Branch       = node.Branch;
     this.Width        = node.Width;
     this.SubTreeWidth = node.SubTreeWidth;
     this.SubTreeSum   = node.SubTreeSum;
     this.Index        = node.Index;
     this.vertex       = node.vertex;
     this.Tree         = tree != null ? tree : node.Tree;
 }
        /// <summary>
        /// Returns the node at the given position. The position index of a parent is one plus the index of the right child; the position of the left child is less than the position of the right child.
        /// </summary>
        public DecompositionNode Find(int position)
        {
            int offset = 0;
            DecompositionNode result = this.Root;

            while (offset + result.SubTreeSize != position)
            {
                if (position <= offset + result.Left.SubTreeSize)
                {
                    result = result.Left;
                }
                else
                {
                    offset += result.Left.SubTreeSize;
                    result  = result.Right;
                }
            }
            return(result);
        }
 /// <summary>
 /// The set of all nodes in this subtree.
 /// </summary>
 /// <param name="mode">The order in which we will travers the nodes in the subtree.</param>
 /// <returns>The nodes in the subtree.</returns>
 public IEnumerable <DecompositionNode> SubTree(TreeTraversal mode)
 {
     if (mode == TreeTraversal.ParentFirst)
     {
         Queue <DecompositionNode> queue = new Queue <DecompositionNode>();
         queue.Enqueue(this);
         while (queue.Count > 0)
         {
             DecompositionNode node = queue.Dequeue();
             if (!node.IsLeaf)
             {
                 queue.Enqueue(node.Left);
                 queue.Enqueue(node.Right);
             }
             yield return(node);
         }
     }
     else
     {
         DecompositionNode         finished = this;
         Stack <DecompositionNode> stack    = new Stack <DecompositionNode>();
         stack.Push(this);
         while (stack.Count > 0)
         {
             DecompositionNode node = stack.Peek();
             if (node.IsLeaf || node.Right == finished)
             {
                 stack.Pop();
                 finished = node;
                 yield return(node);
             }
             else
             {
                 stack.Push(node.Right);
                 stack.Push(node.Left);
             }
         }
     }
 }