public SpriteRenderer GetChildOfRecycle(SpriteRenderer[] array, ChildSide side) { float center = array[0].bounds.center.x; SpriteRenderer result = array[0]; switch (side) { case ChildSide.Left: for (int i = 0; i < array.Length; i++) { if (array[i].bounds.center.x < center) { center = array[i].bounds.center.x; result = array[i]; } } break; case ChildSide.Right: for (int j = 0; j < array.Length; j++) { if (array[j].bounds.center.x > center) { center = array[j].bounds.center.x; result = array[j]; } } break; } return(result); }
/// <summary> /// Adds the given node as a child this node /// </summary> /// <param name="child">child to add</param> /// <returns>true if the child was added, false otherwise</returns> public bool AddChild(BinaryTreeNode <T> child, ChildSide side) { // don't add duplicate children if (left == child || right == child) { return(false); } else if ((side == ChildSide.Left && left != null) || (side == ChildSide.Right && right != null)) { // don't write over existing child return(false); } else { // add as child and add self as parent if (side == ChildSide.Left) { left = child; } else { right = child; } child.Parent = this; return(true); } }
/// <summary> /// Adds the given node to the tree. If the given node is /// null the method returns false. If the parent node is null /// or isn't in the tree the method returns false. If the given /// node is already a child of the parent node the method returns /// false /// </summary> /// <param name="node">node to add</param> /// <param name="side">side to add to for parent</param> /// <returns>true if the node is added, false otherwise</returns> public bool AddNode(BinaryTreeNode <T> node, ChildSide side) { if (node == null || node.Parent == null || !nodes.Contains(node.Parent)) { return(false); } else if (node.Parent.Left == node || node.Parent.Right == node) { // node already a child of parent return(false); } else { // add child as tree node and as a child to parent nodes.Add(node); return(node.Parent.AddChild(node, side)); } }