/// <summary> /// Gets the next unvisited. /// </summary> /// <returns></returns> public SortedTreeNode GetNextUnvisited() { SortedTreeNode result = null; if (Visited) { return(null); } foreach (KeyValuePair <int, SortedTreeNode> KVPNode in mChildNodes) { result = KVPNode.Value.GetNextUnvisited(); if (result != null) { return(result); } } Visited = true; return(this); }
/// <summary> /// Connects the tree to the node. /// </summary> /// <param name="NodeToWhichWeConnectsTheTreeValue">The node to which we connects the tree value.</param> /// <param name="NodeToWhichWeConnectsTheTreeConnectorNumer">The node to which we connects the tree connector numer.</param> /// <param name="TreeToBeConnected">The tree to be connected.</param> /// <param name="TreeToBeConnectedConnectorNumer">The tree to be connected connector numer.</param> public void ConnectTheTreeToTheNode(T NodeToWhichWeConnectsTheTreeValue, int NodeToWhichWeConnectsTheTreeConnectorNumer, SortedTree <T> TreeToBeConnected, int TreeToBeConnectedConnectorNumer) { //sprawdzamy czy polaczenie moze nastapic if (!TestTreeIfCanBeConnected(NodeToWhichWeConnectsTheTreeValue, NodeToWhichWeConnectsTheTreeConnectorNumer, TreeToBeConnected, TreeToBeConnectedConnectorNumer)) { throw new SortedTreeNodeException("Cannot connect such tree"); } //wyszukujemy node do ktorego chcemy podlaczyc drzewo SortedTreeNode NodeToWhichWeConnectsTheTree = GetNode(NodeToWhichWeConnectsTheTreeValue); if (NodeToWhichWeConnectsTheTree == null) { throw new SortedTreeNodeException("Cannot find parent node"); } //klonujemy drzewo ktore dolaczamy aby uniknac ew. zmian w tym drzewie (chcemy miec pewnosc ze ktos zmieniajac oryginal to drzewo (this) pozostanie nie zmienione) SortedTree <T> clonnedtree = (SortedTree <T>)((ICloneable)TreeToBeConnected).Clone(); clonnedtree.SetParentTree(this); NodeToWhichWeConnectsTheTree.AddNode(clonnedtree.GetRoots()[0], NodeToWhichWeConnectsTheTreeConnectorNumer, TreeToBeConnectedConnectorNumer); ParentCleanup(); }
/// <summary> /// Gets the node. /// </summary> /// <param name="Value">The value.</param> /// <returns>node if found and null otherwise</returns> public SortedTreeNode GetNode(T Value) { SortedTreeNode nodetobereturned = null; foreach (SortedTreeNode Node in m_Roots) { if (Node.Value.Equals(Value)) { nodetobereturned = Node; } else { nodetobereturned = Node.GetNode(Value); } if (nodetobereturned != null) { return(nodetobereturned); } } return(null); }
/// <summary> /// Gets the node. /// </summary> /// <param name="Value">The value.</param> /// <returns></returns> public SortedTreeNode GetNode(T Value) { SortedTreeNode nodetobereturned = null; foreach (KeyValuePair <int, SortedTreeNode> KVPNode in mChildNodes) { if (KVPNode.Value.Value.Equals(Value)) { nodetobereturned = KVPNode.Value; } else { nodetobereturned = KVPNode.Value.GetNode(Value); } if (nodetobereturned != null) { return(nodetobereturned); } } return(null); }
/// <summary> /// Removes the value. /// </summary> /// <param name="ValueToBeRemoved">The value to be removed.</param> /// <param name="Shallow">if set to <c>true</c> [shallow] removal is done (all children are moved to root if they are not connected to any others elements).</param> public void RemoveValue(T ValueToBeRemoved, bool Shallow) { SortedTreeNode NodeToBeRemoved = GetNode(ValueToBeRemoved); SortedTreeNode node; if (NodeToBeRemoved == null) { throw new SortedTreeNodeException("the value cannot be found on the tree"); } if (Shallow) { //because it is not allowed to change the collection and make foreach on it: SortedTreeNodeSortedList childnodes = NodeToBeRemoved.GetChildNodes(); node = childnodes.GetFirstNodeFromThisCollection(); while ((node = childnodes.GetFirstNodeFromThisCollection()) != null) { RemoveConnectionBetweeenParentAndChildAndMoveElementToRoots(NodeToBeRemoved.Value, node.Value); childnodes = NodeToBeRemoved.GetChildNodes(); } } //removal of connection above node that is removed SortedTreeNodeSortedList parentnodes = NodeToBeRemoved.ParentNodes; node = parentnodes.GetFirstNodeFromThisCollection(); while ((node = parentnodes.GetFirstNodeFromThisCollection()) != null) { RemoveConnectionBetweeenParentAndChildAndMoveElementToRoots(node.Value, NodeToBeRemoved.Value); parentnodes = NodeToBeRemoved.ParentNodes; } foreach (KeyValuePair <int, SortedTreeNode> kvpnode in parentnodes) { RemoveConnectionBetweeenParentAndChildAndMoveElementToRoots(kvpnode.Value.Value, NodeToBeRemoved.Value); } RemoveSortedNodeFromRoots(NodeToBeRemoved); ParentCleanup(); }
/// <summary> /// Adds the node. /// </summary> /// <param name="ParentNodeValue">The parent node value. If this value is null this element is added as root element</param> /// <param name="NodeNumber">The node number.</param> /// <param name="NewValue">The new value.</param> /// <param name="ParentConnectorNumber">The parent connector number.</param> /// <returns></returns> public SortedTreeNode AddNode(T ParentNodeValue, int NodeNumber, T NewValue, int ParentConnectorNumber) { if (ParentNodeValue == null) { return(AddNode(NewValue)); } SortedTreeNode ParentNode = GetNode(ParentNodeValue); if (ParentNode == null) { throw new SortedTreeNodeException("Cannot find parent node"); } SortedTreeNode currentnode = GetNode(NewValue); RaiseTreeHasChangedEvent(); if (currentnode == null) { return(ParentNode.AddNode(NodeNumber, NewValue, ParentConnectorNumber)); } else { return(ParentNode.AddNode(NodeNumber, currentnode, ParentConnectorNumber)); } }
/// <summary> /// Sets the enumerator to its initial position, which is before the first element in the collection. /// </summary> /// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception> void IEnumerator.Reset() { IEnumeratorCurrentNode = null; MarkAllUnvisited(); }