public bool Add(BinaryTreeNode node) { if (node == null) { return(true); } _binaryTree.Push(node.Data); if (_binaryTree.NumOfNodes == 1) { _listOfDrawLocations.Add(new DrawLocation(new PointF(425.0f, 15.0f), _binaryTree.MostRecentAddedNode)); } else { int currentNodeDepth = _binaryTree.MostRecentAddedNode.Depth; if (currentNodeDepth > _maxNodeDepth) { _maxNodeDepth = currentNodeDepth; } // Find the added node in the list of draw locations, find its parent, add its child to this list for (int i = 0; i < _binaryTree.NumOfNodes - 1; ++i) { if (_listOfDrawLocations[i].Node.Left == _binaryTree.MostRecentAddedNode) { PointF nodePos = _listOfDrawLocations[i].DrawPosition; _listOfDrawLocations.Add(new DrawLocation(nodePos, _binaryTree.MostRecentAddedNode)); break; } if (_listOfDrawLocations[i].Node.Right == _binaryTree.MostRecentAddedNode) { PointF nodePos = _listOfDrawLocations[i].DrawPosition; _listOfDrawLocations.Add(new DrawLocation(nodePos, _binaryTree.MostRecentAddedNode)); break; } } } int charLengthOfNodeData = _binaryTree.MostRecentAddedNode.Data.ToString().Length; if (charLengthOfNodeData > _maxCharLengthOfData) { _maxCharLengthOfData = charLengthOfNodeData; _nodeDrawSize.Width = (int)(_font.Size * _maxCharLengthOfData * 1.25f); if (_nodeDrawSize.Width < _nodeDrawSize.Height) { _nodeDrawSize.Width = _nodeDrawSize.Height; } } Add(node.Left); Add(node.Right); SetNodePositions(_binaryTree.RootNode); EstablishNodeLines(); return(true); }
public DrawLocation(PointF drawPosition, BinaryTreeNode node) { DrawPosition = drawPosition; Node = node; }