IList <int> GetParentsBelowStackBased(TreeElement searchFromThis) { Stack <TreeElement> stack = new Stack <TreeElement>(); stack.Push(searchFromThis); var parentsBelow = new List <int>(); while (stack.Count > 0) { TreeElement current = stack.Pop(); if (current.hasChildren) { parentsBelow.Add(current.id); foreach (var T in current.children) { stack.Push(T); } } } return(parentsBelow); }
public static void TestTreeModelCanRemoveElements() { var root = new TreeElement { name = "Root", depth = -1 }; var listOfElements = new List <TreeElement>(); listOfElements.Add(root); var model = new TreeModel <TreeElement>(listOfElements); model.AddElement(new TreeElement { name = "Element" }, root, 0); model.AddElement(new TreeElement { name = "Element " + root.children.Count }, root, 0); model.AddElement(new TreeElement { name = "Element " + root.children.Count }, root, 0); model.AddElement(new TreeElement { name = "Sub Element" }, root.children[1], 0); model.RemoveElements(new[] { root.children[1].children[0], root.children[1] }); // Assert order is correct string[] namesInCorrectOrder = { "Root", "Element 2", "Element" }; Assert.AreEqual(namesInCorrectOrder.Length, listOfElements.Count, "Result count does not match"); for (int i = 0; i < namesInCorrectOrder.Length; ++i) { Assert.AreEqual(namesInCorrectOrder[i], listOfElements[i].name); } // Assert depths are valid TreeElementUtility.ValidateDepthValues(listOfElements); }
public void AddElement(T element, TreeElement parent, int insertPosition) { if (element == null) { throw new ArgumentNullException("element", "element is null"); } if (parent == null) { throw new ArgumentNullException("parent", "parent is null"); } if (parent.children == null) { parent.children = new List <TreeElement> (); } parent.children.Insert(insertPosition, element); element.parent = parent; TreeElementUtility.UpdateDepthValues(parent); TreeElementUtility.TreeToList(m_Root, m_Data); Changed(); }