/// <summary> /// Applies preorder ordering to this tree /// </summary> /// <returns>tree as preorder string</returns> public String preorder() { if (this.root == null) return ""; String res = "Vertex " + this.root.ToString(); if (this.root.OutgoingEdges == null) return res; foreach(Edge e in this.root.OutgoingEdges) { res += "\nEDGE " + e.ToString() + "\n"; Tree subtree = new Tree(SplitSize, Depth - 1, VertexAttributes, EdgeAttributes); subtree.root = e.Bottom; res += subtree.preorder(); } return res; }
/// <summary> /// Counts Vertexs in this tree. For test /// </summary> /// <returns></returns> public int countVertexs() { int n = 1; if (root.OutgoingEdges == null) return n; foreach(Edge e in root.OutgoingEdges) { Tree subtree = new Tree(SplitSize, Depth - 1, VertexAttributes, EdgeAttributes); subtree.root = e.Bottom; n += subtree.countVertexs(); } return n; }
/// <summary> /// Generates a random tree with a given splitsize, depth and list of attributes for either vertexes and for edges. /// </summary> /// <param name="EdgeAttributes">List of attributes of each edge in the future tree</param> /// <param name="VertexAttributes">List of attributes of each vertex in the future tree</param> /// <returns>a random tree</returns> public static Tree getRandomTree( int Depth, int SplitSize, LinkedList<Attribute> VertexAttributes, LinkedList<Attribute> EdgeAttributes) { //generate per level: each Vertex gets its childs //Console.WriteLine("depth: " + Depth + "; splitsize: " + SplitSize); Tree tree = new Tree(SplitSize, Depth, VertexAttributes, EdgeAttributes); Vertex root = new Vertex(1, GetRandomAttributes(true, VertexAttributes, EdgeAttributes).ToArray()); tree.root = root; Queue<Vertex> stack = new Queue<Vertex>(); stack.Enqueue(tree.root); Vertex curVertex; int n = 2; for (int l = 1; l < Depth || stack.Count != 0; l++) { if (stack.Count != 0 && l >= Depth) l--; curVertex = stack.Dequeue(); //Console.WriteLine("curVertex:" + curVertex); for (int s = 0; s < SplitSize; s++) { Vertex childVertex = new Vertex(n++, GetRandomAttributes(true, VertexAttributes, EdgeAttributes).ToArray(), new LinkedList<Edge>(), l); if (l != Depth - 1) { stack.Enqueue(childVertex); //Console.WriteLine("childVx: " + childVertex.ToString()); } curVertex.append(new Edge(n, curVertex, childVertex, GetRandomAttributes(false, VertexAttributes, EdgeAttributes))); } } return tree; }
/// <summary> /// Writes the xml with vertexes printed in preorder /// </summary> /// <param name="writer"></param> private void preorderXML_vertexes(XmlWriter writer) { if (this.root == null) return; if (this.root.OutgoingEdges == null) return; this.vertexToXML(writer, this.root); this.edgesToXML(writer, this.root); writer.WriteEndElement(); //Vertex foreach (Edge e in this.root.OutgoingEdges) { Tree subtree = new Tree(SplitSize, Depth - 1, VertexAttributes, EdgeAttributes); subtree.root = e.Bottom; subtree.preorderXML_vertexes(writer); } }