public void Given_a_linked_list_when_remove_then_the_nth_should_be_removed() { var head = InitNodes(); var solution = new RemoveNode(); var actual = solution.RemoveFromEnd(head, 2); Assert.Equal(_node1.Val, actual.Val); Assert.Equal(_node2.Val, _node1.Next.Val); Assert.Equal(_node3.Val, _node1.Next.Next.Val); Assert.Equal(_node5.Val, _node1.Next.Next.Next.Val); }
public MapNodeVm(Node node, NodesViewModel nodesVm, RoutesViewModel routesVm, RoadsViewModel roadsVm) : base(node.LatLng) { Node = node; this.nodesVm = nodesVm; this.routesVm = routesVm; this.roadsVm = roadsVm; RemoveNodeCommand = new RemoveNode(nodesVm, routesVm, roadsVm); FindRoadCommand = new FindRoad(Node, roadsVm); nodesVm.NodesChanged += (a, b) => this.OnPropertyChanged(new PropertyChangedEventArgs("Nodes")); Shape = new MapNodeShape(this); }
private void RemoveCurrentNode() { TreeNode selected = treeBookmarks.SelectedNode; string id; if (selected == null) { id = null; } else { id = (string)selected.Tag; } RemoveNode?.Invoke(this, new RemoveNodeEventArgs(id)); }
/******************************************************************** * Removes an enemy from the list and returns it. The LinkedListNode * that held the enemy is placed back in the freeNodeList. This also uses * the delegate function to choose which node to remove (and remove it). * * @param list The list to remove from * @param enemy Enemy used in removal by search (can be null otherwise) * @param removeNodeFunc The function that chooses which node to remove (and removes it) * @return The enemy removed (null if not found or empty list) ********************************************************************/ private Enemy RetrieveEnemy(LinkedList <Enemy> list, Enemy enemy, RemoveNode removeNodeFunc) { if (list.Count.Equals(0)) { return(null); } LinkedListNode <Enemy> node = removeNodeFunc(list, enemy); if (node == null) { return(null); } Enemy enemyRetrieved = node.Value; AddFreeNode(node); return(enemyRetrieved); }
/******************************************************************** * Wrapper for RetrieveEnemy(LinkedList<Enemy> list, Enemy enemy, RemoveNode removeNodeFunc) ********************************************************************/ private Enemy RetrieveEnemy(LinkedList <Enemy> list, RemoveNode removeNodeFunc) { return(RetrieveEnemy(list, null, removeNodeFunc)); }
public void Clear() { _head = null; _count = 0; RemoveNode?.Invoke("Work Clear Method"); }
public bool Remove(T value) { BinaryTreeNode <T> current; BinaryTreeNode <T> parent; // Search the NODE for removal. current = FindWithParent(value, out parent); if (current == null) { return(false); } _count--; // The first option: the deleted node does not have a right descendant. if (current.Right == null) { // Remove the root if (parent == null) { _head = current.Left; RemoveNode?.Invoke("The head changed"); } else { int result = parent.CompareTo(current.Value); if (result > 0) { //If the value of the parent node is greater //than the value of the node being deleted, //make the left descendant of the current node // to the left descendant of the parent node. parent.Left = current.Left; RemoveNode?.Invoke("make the left descendant of the current node to the left descendant of the parent node"); } else if (result < 0) { // If the value of the parent node is less than the value //of the node being deleted, make the left descendant //of the current node the right descendant of the //parent node. parent.Right = current.Left; RemoveNode?.Invoke("make the left descendant of the current node to the right descendant of the parent node"); } } } // The second option: the deleted node has a right descendant, which has no left descendant. else if (current.Right.Left == null) { current.Right.Left = current.Left; // Remove the root if (parent == null) { _head = current.Right; RemoveNode?.Invoke("The head changed"); } else { int result = parent.CompareTo(current.Value); if (result > 0) { // If the value of the parent node is greater than the value of the node being deleted - // make the right descendant of the current node the left descendant of the parent node. parent.Left = current.Right; RemoveNode?.Invoke("make the right descendant of the current node the left descendant of the parent node"); } else if (result < 0) { // If the value of the parent node is less than the value of the node being deleted - // make the right descendant of the current node the right descendant of the parent node. parent.Right = current.Right; RemoveNode?.Invoke("make the right descendant of the current node the right descendant of the parent node"); } } } // The third option: the deleted node has a right descendant that has a left descendant. else { BinaryTreeNode <T> leftmostParent = current.Right; BinaryTreeNode <T> leftmost = current.Right.Left; // search for the leftmost descendant while (leftmost.Left != null) { leftmostParent = leftmost; leftmost = leftmost.Left; } // The right subtree of the leftmost node becomes the left subtree of its parent node. leftmostParent.Left = leftmost.Right; RemoveNode?.Invoke("The right subtree of the leftmost node becomes the left subtree of its parent node"); // Assign the leftmost node as the left descendant - the left descendant of the deleted node, // and as the right descendant is the right descendant of the node to be deleted. leftmost.Left = current.Left; leftmost.Right = current.Right; RemoveNode?.Invoke("Assign the leftmost node as the left descendant - the left descendant of the deleted node," + "and as the right descendant is the right descendant of the node to be deleted. "); if (parent == null) { _head = leftmost; RemoveNode?.Invoke("The head changed"); } else { int result = parent.CompareTo(current.Value); if (result > 0) { // If the value of the parent node (parent) is greater than the value of the node being deleted (current) - // make the leftmost descendant of the deleted node (leftmost) - the left descendant of its parent (parent). parent.Left = leftmost; RemoveNode?.Invoke("make the leftmost descendant of the deleted node (leftmost) - the left descendant of its parent (parent)."); } else if (result < 0) { // If the value of the parent node (parent) is less than the value of the node being deleted (current) - // make the leftmost descendant of the deleted node (leftmost) - the right descendant of its parent (parent). parent.Right = leftmost; RemoveNode?.Invoke("make the leftmost descendant of the deleted node(leftmost) -the right descendant of its parent(parent)."); } } } return(true); }