Ejemplo n.º 1
0
        /// <summary>
        /// Returns the subtree which is rooted in the vertex in the parameters.
        /// </summary>
        /// <param name="vertexId"> The root of the subtree. </param>
        /// <param name="copy"> If true: A new tree is returned.
        /// If false: The subtree is created using the original tree. </param>
        /// <returns> The subtree. </returns>
        public Tree SubTree(int vertexId, bool copy = true)
        {
            traversal t = new traversal();

            if (!copy)
            {
                // Remove all vertices not in the Sub-tree

                IEnumerable <int> bunch = t.RecursivePreOrder((mtg)this, vertexId);

                foreach (int vid in parent.Keys)
                {
                    if (!bunch.Contains(vid))
                    {
                        RemoveVertex(vid);
                    }
                }

                root = vertexId;

                if (parent.ContainsKey(root))
                {
                    parent[root] = -1;
                }
                else
                {
                    parent.Add(root, -1);
                }

                return(this);
            }
            else
            {
                Dictionary <int, int> renumberedTree = new Dictionary <int, int>();
                Tree tree = new Tree();

                tree.root = 0;

                renumberedTree.Add(vertexId, tree.root);

                IEnumerable <int> subTree = t.RecursivePreOrder((mtg)this, vertexId);

                foreach (int vid in subTree)
                {
                    if (vid != vertexId)
                    {
                        int parent = renumberedTree[(int)Parent(vid)];
                        int v      = tree.AddChild(parent);
                        renumberedTree.Add(vid, v);
                    }
                }

                return(tree);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Add a tree after the children of the parent specified in the parameters.
        /// </summary>
        /// <param name="parentId"> Vertex identifier. </param>
        /// <param name="tree"> A rooted tree. </param>
        /// <returns> The dictionary which makes a correspondance between old identifier and new identifier. </returns>
        public Dictionary <int, int> AddChildTree(int parentId, Tree tree)
        {
            Dictionary <int, int> renumberedTree = new Dictionary <int, int>();
            int root   = tree.root;
            int rootId = AddChild(parentId);

            renumberedTree.Add(root, rootId);

            // PreOrder traversal from root and renumbering new children.

            traversal t = new traversal();

            foreach (int vertexId in t.RecursivePreOrder((mtg)tree, root))
            {
                if (vertexId == root)
                {
                    continue;
                }

                parentId = renumberedTree[(int)tree.Parent(vertexId)];
                int vid = AddChild(parentId);
                renumberedTree.Add(vertexId, vid);
            }

            return(renumberedTree);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Insert a tree before the specified vertex.
        /// </summary>
        /// <param name="vertexId"> Vertex identifier. </param>
        /// <param name="tree"> The tree to be inserted. </param>
        /// <returns> The new identifiers after the sibling has been added. </returns>
        public Dictionary <int, int> InsertSiblingTree(int vertexId, Tree tree)
        {
            Dictionary <int, int> renumberedTree = new Dictionary <int, int>();

            int root   = tree.root;
            int rootId = InsertSibling(vertexId);

            renumberedTree.Add(root, rootId);

            // PreOrder traversal from root and renumbering the sibling's vertices.

            traversal t = new traversal();

            foreach (int vertex in t.RecursivePreOrder((mtg)tree, vertexId))
            {
                int parent = renumberedTree[(int)tree.Parent(vertex)];
                int v      = AddChild(parent);

                renumberedTree.Add(vertex, v);
            }

            return(renumberedTree);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Return the subtree rooted on the vertex in the parameters.
        /// </summary>
        /// <param name="vertexId"> Vertex identifier. </param>
        /// <param name="copy"> If true: return a new tree holding the subtree.
        /// If false: The subtree is created using the original tree. </param>
        public new PropertyTree SubTree(int vertexId, bool copy = true)
        {
            traversal t = new traversal();

            if (!copy)
            {
                IEnumerable <int> bunch       = t.RecursivePreOrder((mtg)this, vertexId);
                IEnumerable <int> removeBunch = this.Vertices().Except(bunch);

                foreach (int vid in removeBunch)
                {
                    RemoveVertexProperties(vid);

                    // Remove parent edge

                    int parentId = (int)Parent(vid);

                    if (parentId != -1)
                    {
                        children[parentId].Remove(vid);
                        parent.Remove(vid);
                    }

                    // Remove children edges

                    foreach (int child in Children(vid))
                    {
                        parent[child] = -1;
                    }

                    if (children.ContainsKey(vid))
                    {
                        children.Remove(vid);
                    }
                }

                SetRoot(vertexId);

                return(this);
            }
            else
            {
                Dictionary <int, int> renumberedTree = new Dictionary <int, int>();

                PropertyTree tree = new PropertyTree();
                SetRoot(0);

                foreach (string name in Properties().Keys)
                {
                    tree.AddProperty(name);
                }

                renumberedTree.Add(vertexId, tree.root);
                tree.AddVertexProperties(tree.root, GetVertexProperties(vertexId));

                IEnumerable <int> subTree = t.RecursivePreOrder((mtg)this, vertexId);

                foreach (int vid in subTree)
                {
                    if (vid != vertexId)
                    {
                        int parentId = (int)Parent(vid);

                        if (parentId != -1)
                        {
                            int parent = renumberedTree[parentId];
                            int v      = tree.AddChild(parent);
                            renumberedTree.Add(vid, v);
                        }

                        tree.AddVertexProperties(vid, GetVertexProperties(vid));
                    }
                }

                return(tree);
            }
        }