public void AddNode(char element) { // Place in left subtree if (element < Data) { if (Left == null) { Left = new TreeNode(element); } else { Left.AddNode(element); } } else { if (Right == null) { Right = new TreeNode(element); } else { Right.AddNode(element); } } }
public void AddNode(Node node) { if (Left == Empty) { Left = node; } else if (node.Type == NodeType.Value) { if (Left.Type == NodeType.Operator && Left.CanAddValueNode()) { Left.AddNode(node); } else if (Right == Empty) { Right = node; } else if (Right.Type == NodeType.Operator && Right.CanAddValueNode()) { Right.AddNode(node); } } else if (node.Type == NodeType.Operator) { if (Right != Empty) { if (node.Priority >= Right.Priority) { node.AddNode(Right); Right = node; } else { Right.AddNode(node); } } else if (Left != Empty) { if (node.Priority >= Left.Priority) { node.AddNode(Left); Left = node; } else { Left.AddNode(node); } } } }
/// <summary> /// Function to add a node as a child to this node. /// </summary> /// <param name="dimensions">Dimensions that the node will occupy.</param> public GlyphNode AddNode(Size dimensions) { if (!IsLeaf) { GlyphNode node = Left.AddNode(dimensions); // If we can't add the node to the left, then try the right. return(node ?? Right.AddNode(dimensions)); } // Do nothing if there's no more room. if (_noMoreRoom) { return(null); } // Ensure we can fit this node. if ((dimensions.Width > Region.Width) || (dimensions.Height > Region.Height)) { return(null); } // We have an exact fit, so there will be no more room for other nodes. if ((dimensions.Width == Region.Width) && (dimensions.Height == Region.Height)) { _noMoreRoom = true; return(this); } Left = new GlyphNode(this); Right = new GlyphNode(this); // Subdivide. var delta = new Size(Region.Width - dimensions.Width, Region.Height - dimensions.Height); if (delta.Width > delta.Height) { Left.Region = new Rectangle(Region.Left, Region.Top, dimensions.Width, Region.Height); Right.Region = new Rectangle(Region.Left + dimensions.Width, Region.Top, delta.Width, Region.Height); } else { Left.Region = new Rectangle(Region.Left, Region.Top, Region.Width, dimensions.Height); Right.Region = new Rectangle(Region.Left, Region.Top + dimensions.Height, Region.Width, delta.Height); } return(Left.AddNode(dimensions)); }