private void CalculateSize(Graphics g, float left, Y2BinaryTreeNode <int> node) { if (node != null) { string text = node.Value.ToString(); SizeF size = g.MeasureString(text, pictureBox1.Font); float x = left - (RADIUS + size.Width) / 2; if (node.HasLeftChild) { float p2 = x - Math.Abs(node.Value - node.LeftChild.Value) * _ratio; if (p2 < _minLeft) { _minLeft = p2; } if (p2 > _maxLeft) { _maxLeft = p2; } CalculateSize(g, p2, node.LeftChild); } if (node.HasRightChild) { float p2 = x + Math.Abs(node.RightChild.Value - node.Value) * _ratio; if (p2 < _minLeft) { _minLeft = p2; } if (p2 > _maxLeft) { _maxLeft = p2; } CalculateSize(g, p2, node.RightChild); } } }
private void DrawTreeNode(Graphics g, PointF p, Y2BinaryTreeNode <int> node, bool highlight) { if (node != null) { string text = node.Value.ToString(); SizeF size = g.MeasureString(text, _font); float ellipseWidth = RADIUS + size.Width; float ellipseHeight = RADIUS + size.Height; float left = p.X - ellipseWidth / 2; float top = p.Y - ellipseHeight / 2; Pen pen = _penNormal; if (node.HasLeftChild) { PointF p1 = p; PointF p2 = p; p1.X = left + ellipseWidth / 2; p2.X -= (node.Value - node.LeftChild.Value) * _ratio; p2.Y += VER_DISTANCE; bool hlight = false; if (_queue != null && _queue.Count > 0) { if (_queue.Peek() == node.LeftChild.Value) { _queue.Dequeue(); pen = _penHighLight; hlight = true; } } g.DrawLine(pen, p1, p2); DrawTreeNode(g, p2, node.LeftChild, hlight); if (p2.X < _minLeft) { _minLeft = p2.X; } if (p2.X > _maxLeft) { _maxLeft = p2.X; } } if (node.HasRightChild) { PointF p1 = p; PointF p2 = p; p1.X = left + ellipseWidth / 2; p2.X += (node.RightChild.Value - node.Value) * _ratio; p2.Y += VER_DISTANCE; pen = _penNormal; bool hlight = false; if (_queue != null && _queue.Count > 0) { if (_queue.Peek() == node.RightChild.Value) { _queue.Dequeue(); pen = _penHighLight; hlight = true; } } g.DrawLine(pen, p1, p2); DrawTreeNode(g, p2, node.RightChild, hlight); if (p2.X < _minLeft) { _minLeft = p2.X; } if (p2.X > _maxLeft) { _maxLeft = p2.X; } } pen = highlight ? _penHighLight : _penNormal; g.FillEllipse(_brush, left, top, ellipseWidth, ellipseHeight); g.DrawEllipse(pen, left, top, ellipseWidth, ellipseHeight); g.DrawString(text, _font, Brushes.Black, left + RADIUS / 2, top + RADIUS / 2); } }