public override TreeNetwork MakeNetwork(IEnumerable <Node> nodes, IEnumerable <Link> links) { System.Diagnostics.Debug.Assert(this.Angle == 0 || this.Angle == 180); System.Diagnostics.Debug.Assert(this.Alignment == TreeAlignment.BusBranching); System.Diagnostics.Debug.Assert(this.Path != TreePath.Source); var net = base.MakeNetwork(nodes, links); foreach (var v in net.Vertexes.ToArray()) { // ignore leaves of tree if (v.DestinationEdgesCount == 0) { continue; } if (v.DestinationEdgesCount % 2 == 1) { // if there's an odd number of real children, add two dummies var dummy = new TreeVertex(); dummy.Bounds = new Rect(); dummy.Focus = new Point(); net.AddVertex(dummy); net.LinkVertexes(v, dummy, null); } // make sure there's an odd number of children, including at least one dummy; // LayoutNodes will move the parent node to where this dummy child node is placed var dummy2 = new TreeVertex(); dummy2.Bounds = v.Bounds; dummy2.Focus = v.Focus; net.AddVertex(dummy2); net.LinkVertexes(v, dummy2, null); } return(net); }
private void AddTree(Tree tree) { var treeVertex = new TreeVertex(tree); _contents.AddVertex(treeVertex); tree.ForEach(e => AddTreeEntry(tree, e)); }
private void AddPath(string path, string value) { var name = SplitPath(path); var it = root; for (var i = 0; i < name.Length; i++) { if (!IsAttribute(name[i])) { var treeVertex = new TreeVertex(name[i]); if (it.RightSon == null || it.RightSon.FullName != treeVertex.FullName) { it.AddSon(treeVertex); } it = it.RightSon; } else { if (i != name.Length - 1) { throw new InvalidOperationException($"Атрибут {name[i]} не последний в цепочке '{path}'"); } } } var lastName = name[name.Length - 1]; if (IsAttribute(lastName)) { it.AddAttribute(lastName, value); } else { it.AddText(value); } }
private void DisplayPostOrder(TreeVertex <T> root, List <T> result) { if (root == null) { return; } DisplayPostOrder(root.LeftChild, result); DisplayPostOrder(root.RightChild, result); result.Add(root.Data); }
public NameValueCollectionReader(NameValueCollection collection) { root = new TreeVertex("GlobalRoot"); var allKeys = collection.AllKeys.OrderBy(key => key, new NameValueCollectionKeyComparer()); foreach (var key in allKeys) { AddPath("Root." + key, collection[key]); } cur = root.LeftSon; }
private void ShiftAll(double direction, double absolute, TreeVertex root, TreeVertex v) { System.Diagnostics.Debug.Assert(root.Angle == 0 || root.Angle == 180); var loc = v.Center; loc.X += direction * Math.Abs(root.Center.Y - loc.Y) / 2; loc.X += absolute; v.Center = loc; foreach (var c in v.Children) { ShiftAll(direction, absolute, root, c); } }
public void AddSon(TreeVertex son) { son.depth = depth + 1; son.Parent = this; if (LeftSon == null) { LeftSon = RightSon = son; } else { RightSon.RightNeighbour = son; RightSon = son; } }
private bool Contains(T data, TreeVertex <T> current) { if (current == null) { return(false); } if (data.CompareTo(current.Data) < 0) { return(Contains(data, current.LeftChild)); } if (data.CompareTo(current.Data) > 0) { return(Contains(data, current.RightChild)); } return(true); }
protected override void AssignTreeVertexValues(TreeVertex v) { base.AssignTreeVertexValues(v); SetDirection(v, 0); if (v.Parent != null) { // The parent node will be moved to where the last dummy will be; // reduce the space to account for the future hole. if (v.Angle == 0 || v.Angle == 180) { v.LayerSpacing -= v.Bounds.Width; } else { v.LayerSpacing -= v.Bounds.Height; } } }
public void Add(T data) { if (Contains(data)) { return; } var newVertex = new TreeVertex <T>(data); Count++; if (IsEmpty) { Root = newVertex; return; } var current = Root; var parent = Root; while (true) { if (current == null) { if (newVertex < parent) { parent.LeftChild = newVertex; } else { parent.RightChild = newVertex; } return; } parent = current; current = newVertex < current ? current.LeftChild : current.RightChild; } }
protected override void AssignTreeVertexValues(TreeVertex v) { base.AssignTreeVertexValues(v); // Manually rotate the TextBlock and resize the background Rectangle // based on the level/depth in the tree so that the node/vertex is approximately // the correct (minimum) size, including the TextBlock and some space around it. // This is kludgy but will work both in WPF and in Silverlight // (there's no FrameworkElement.LayoutTransform in Silverlight). Node node = v.Node; FrameworkElement back = node.FindNamedDescendant("Background"); FrameworkElement tb = node.FindNamedDescendant("TextBlock"); // find how big the TextBlock is Size tsize = new Size(tb.ActualWidth, tb.ActualHeight); // length includes some space at both ends double textlen = tsize.Width + 20; PipeInfo pi = node.Data as PipeInfo; // the thickness of the "pipe" Rectangle double boxthickness = Math.Max(pi.Current, 20); if (v.Angle == 0 || v.Angle == 180) // if tree is growing sideways // the Background Rectangle is sized according to PipeInfo.Current // and is long enough to hold the text { back.Width = boxthickness; back.Height = textlen; node.SetAngle(tb, 90); // and the text is turned to 90 degrees } else { back.Width = textlen; back.Height = boxthickness; node.SetAngle(tb, 0); // normal upright text } // update the vertex with the new size v.Bounds = new Rect(0, 0, back.Width, back.Height); }
private void Shift(TreeVertex v) { var p = v.Parent; if (p != null && (v.Angle == 90 || v.Angle == 270)) { var g = p.Parent; if (g != null) { double shift = v.NodeSpacing; if (GetDirection(g) > 0) { if (g.Angle == 90) { if (p.Angle == 0) { SetDirection(v, 1); if (v.Angle == 270) { ShiftAll(2, -shift, p, v); } } else if (p.Angle == 180) { SetDirection(v, -1); if (v.Angle == 90) { ShiftAll(-2, shift, p, v); } } } else if (g.Angle == 270) { if (p.Angle == 0) { SetDirection(v, 1); if (v.Angle == 90) { ShiftAll(2, -shift, p, v); } } else if (p.Angle == 180) { SetDirection(v, -1); if (v.Angle == 270) { ShiftAll(-2, shift, p, v); } } } } else if (GetDirection(g) < 0) { if (g.Angle == 90) { if (p.Angle == 0) { SetDirection(v, 1); if (v.Angle == 90) { ShiftAll(2, -shift, p, v); } } else if (p.Angle == 180) { SetDirection(v, -1); if (v.Angle == 270) { ShiftAll(-2, shift, p, v); } } } else if (g.Angle == 270) { if (p.Angle == 0) { SetDirection(v, 1); if (v.Angle == 270) { ShiftAll(2, -shift, p, v); } } else if (p.Angle == 180) { SetDirection(v, -1); if (v.Angle == 90) { ShiftAll(-2, shift, p, v); } } } } } else // g == null: V is a child of the tree ROOT { var dir = ((p.Angle == 0) ? 1 : -1); SetDirection(v, dir); ShiftAll(dir, 0, p, v); } } foreach (var c in v.Children) { Shift(c); } }
// rather than adding a property to TreeVertex, just reuse an otherwise unused property: private static double GetDirection(TreeVertex v) { return(v.BreadthLimit); }
public BinaryTree(TreeVertex <T> root) : this(root.Data) { }
private static void SetDirection(TreeVertex v, double dir) { v.BreadthLimit = dir; }
public BinaryTree(T data) : this() { Root = new TreeVertex <T>(data); Count = 1; }
public BinaryTree() { Root = null; }
public bool Remove(T data) { if (Root == null) { return(false); } TreeVertex <T> current = Root; TreeVertex <T> parent = null; int result = current.Data.CompareTo(data); while (result != 0) { if (result > 0) { parent = current; current = current.LeftChild; } else if (result < 0) { parent = current; current = current.RightChild; } if (current == null) { return(false); } result = current.Data.CompareTo(data); } if (current.RightChild == null) { if (parent == null) { Root = current.LeftChild; } else { result = parent.Data.CompareTo(current.Data); if (result > 0) { parent.LeftChild = current.LeftChild; } else if (result < 0) { parent.RightChild = current.LeftChild; } } } else if (current.RightChild.LeftChild == null) { current.RightChild.LeftChild = current.LeftChild; if (parent == null) { Root = current.RightChild; } else { result = parent.Data.CompareTo(current.Data); if (result > 0) { parent.LeftChild = current.RightChild; } else if (result < 0) { parent.RightChild = current.RightChild; } } } else { TreeVertex <T> leftMost = current.RightChild.LeftChild, leftMostParent = current.RightChild; while (leftMost.LeftChild != null) { leftMostParent = leftMost; leftMost = leftMost.LeftChild; } leftMostParent.LeftChild = leftMost.RightChild; leftMost.LeftChild = current.LeftChild; leftMost.RightChild = current.RightChild; if (parent == null) { Root = leftMost; } else { result = parent.Data.CompareTo(current.Data); if (result > 0) { parent.LeftChild = leftMost; } else if (result < 0) { parent.RightChild = leftMost; } } } Count--; return(true); }
public TreeVertex(Bridge bridge) { this.bridge = bridge; parent = null; }
public static void JoinTree(TreeVertex root1, TreeVertex root2) { root1.parent = root2; }
public Edge(TreeVertex v1, TreeVertex v2, int Time) { this.v1 = v1; this.v2 = v2; this.Time = Time; }