protected QuadNode <T> SearchColumn(QuadNode <T> root, QuadNode <T> node) { if (root == null) { return(null); } if (root == node) { return(root); } else { return(SearchColumn(root.Bottom, node)); } }
public bool AddNodeToBottom(QuadNode <T> node, int column) { int newNodeHeight = GetColumnHeight(column); //add node to node above if (newNodeHeight != 0) { QuadNode <T> aboveNode = GetTheMostBottomNode(column); if (aboveNode != null) { aboveNode.Bottom = node; node.Top = aboveNode; } } else { _columnsRoots[column] = node; } //add node to left column if (column > 0) { QuadNode <T> leftNode = SearchColumn(_columnsRoots[column - 1], newNodeHeight); if (leftNode != null) { leftNode.Right = node; node.Left = leftNode; } } //add node to right column if (column < NumberOfColumns - 1) { QuadNode <T> rightNode = SearchColumn(_columnsRoots[column + 1], newNodeHeight); if (rightNode != null) { rightNode.Left = node; node.Right = rightNode; } } return(true); }
public Neighbours IsAdjacentToNode(QuadNode <T> node) { if (Top != null && Top == node) { return(Neighbours.Top); } if (Left != null && Left == node) { return(Neighbours.Left); } if (Bottom != null && Bottom == node) { return(Neighbours.Bottom); } if (Right != null && Right == node) { return(Neighbours.Right); } return(Neighbours.None); }
/// <summary> /// Switchs the position of the node with a neighbouring node. Has in consideration the references of the neigbour node neighbours. /// </summary> /// <param name="neighbour">The relative position of the node we want to switch places with.</param> /// <returns>Returns <see langword="false"/> if the neigbour node is null.</returns> public bool SwitchNodePosition(Neighbours neighbour) { switch (neighbour) { case Neighbours.Top: if (Top == null) { return(false); } QuadNode <T> temporary = Top.MemberwiseClone() as QuadNode <T>; QuadNode <T> substitute = Top; ////assign nodes to the neigbour nodes of the involved pieces if (substitute.Left != null) { substitute.Left.Right = this; } if (substitute.Right != null) { substitute.Right.Left = this; } if (substitute.Top != null) { substitute.Top.Bottom = this; } if (this.Left != null) { this.Left.Right = substitute; } if (this.Bottom != null) { this.Bottom.Top = substitute; } if (this.Right != null) { this.Right.Left = substitute; } ////--------------------------------------- ////assign nodes of the involved pieces substitute.Left = this.Left; substitute.Top = this; substitute.Bottom = this.Bottom; substitute.Right = this.Right; this.Top = temporary.Top; this.Left = temporary.Left; this.Right = temporary.Right; this.Bottom = substitute; return(true); case Neighbours.Left: if (this.Left == null) { return(false); } temporary = this.Left.MemberwiseClone() as QuadNode <T>; substitute = this.Left; //deal with neigbors of the involved pieces if (substitute.Left != null) { substitute.Left.Right = this; } if (substitute.Top != null) { substitute.Top.Bottom = this; } if (substitute.Bottom != null) { substitute.Bottom.Top = this; } if (this.Top != null) { this.Top.Bottom = substitute; } if (this.Right != null) { this.Right.Left = substitute; } if (this.Bottom != null) { this.Bottom.Top = substitute; } //assign end nodes of the involved pieces substitute.Left = this; substitute.Top = this.Top; substitute.Bottom = this.Bottom; substitute.Right = this.Right; this.Top = temporary.Top; this.Left = temporary.Left; this.Bottom = temporary.Bottom; this.Right = substitute; return(true); case Neighbours.Bottom: if (this.Bottom == null) { return(false); } temporary = this.Bottom.MemberwiseClone() as QuadNode <T>; substitute = this.Bottom; //deal with neigbors of the involved pieces if (substitute.Left != null) { substitute.Left.Right = this; } if (substitute.Bottom != null) { substitute.Bottom.Top = this; } if (substitute.Right != null) { substitute.Right.Left = this; } if (this.Top != null) { this.Top.Bottom = substitute; } if (this.Left != null) { this.Left.Right = substitute; } if (this.Right != null) { this.Right.Left = substitute; } //assing end nodes of the involved pieces this.Bottom.Bottom = this; substitute.Left = this.Left; substitute.Top = this.Top; substitute.Right = this.Right; this.Top = substitute; this.Left = temporary.Left; this.Bottom = temporary.Bottom; this.Right = temporary.Right; return(true); case Neighbours.Right: if (this.Right == null) { return(false); } temporary = this.Right.MemberwiseClone() as QuadNode <T>; substitute = this.Right; //assign end nodes of the neighbour nodes of the involved pieces if (substitute.Top != null) { substitute.Top.Bottom = this; } if (substitute.Right != null) { substitute.Right.Left = this; } if (substitute.Bottom != null) { substitute.Bottom.Top = this; } if (this.Top != null) { this.Top.Bottom = substitute; } if (this.Left != null) { this.Left.Right = substitute; } if (this.Bottom != null) { this.Bottom.Top = substitute; } //assign end nodes of the involved pieces substitute.Right = this; substitute.Left = this.Left; substitute.Bottom = this.Bottom; substitute.Top = this.Top; this.Top = temporary.Top; this.Left = substitute; this.Bottom = temporary.Bottom; this.Right = temporary.Right; return(true); default: break; } return(false); }
/// <summary> /// Recursive function that looks for a path from a root node to an endNode passing through objects of the same type. /// </summary> /// <param name="root"></param> /// <param name="endNode"></param> /// <param name="history"> We add a List to dont repeat nodes.</param> /// <returns>Returns true if a path exists.</returns> public bool CanConnectThroughType(QuadNode <T> root, QuadNode <T> endNode, List <QuadNode <T> > history) { //nulll checks if (root.TypeID != endNode.TypeID) { return(false); } //add this node to the history list. history.Add(root); //enters the if statement if the root.Top is not null and we never checked the root.Top node //Is the same for the rest of the nodes (Left, Bottom, Right). if (root.Top != null && !history.Contains(root.Top)) { if (root.Top == endNode) { return(true); } if (CanConnectThroughType(root.Top, endNode, history)) { return(true); } } if (root.Left != null && !history.Contains(root.Left)) { if (root.Left == endNode) { return(true); } if (CanConnectThroughType(root.Left, endNode, history)) { return(true); } } if (root.Bottom != null && !history.Contains(root.Bottom)) { if (root.Bottom == endNode) { return(true); } if (CanConnectThroughType(root.Bottom, endNode, history)) { return(true); } } if (root.Right != null && !history.Contains(root.Right)) { if (root.Right == endNode) { return(true); } if (CanConnectThroughType(root.Right, endNode, history)) { return(true); } } return(false); }
public QuadNode <T> GetNode(QuadNode <T> node, int column) { return(SearchColumn(_columnsRoots[column], node)); }