private void btnFindMax_Click(object sender, EventArgs e) { lblInfo.Text = ""; if (intTree != null) { highlightedNode = intTree.FindMax(); pbTree.Refresh(); } else { lblInfo.Text = "Tree is empty"; } }
private void drawTree(BinarySearchTree_ <int> intTree, int highlightItem) { // Tree Painting Routine. First draws the connections between // the nodes and then the actual nodes if (intTree != null) { Bitmap result = new Bitmap(pbTree.Width, pbTree.Height); using (Graphics g = Graphics.FromImage(result)) { nodeLinkImage = new Bitmap(pbTree.Width, pbTree.Height); Graphics gb = Graphics.FromImage(nodeLinkImage); nodeImage = new Bitmap(pbTree.Width, pbTree.Height, PixelFormat.Format32bppArgb); Graphics gn = Graphics.FromImage(nodeImage); gn.Clear(Color.Transparent); int totalLevels = intTree.getMaxLevel() + 1; int treeMin = intTree.FindMin(); int treeMax = intTree.FindMax(); //g = pbTree.CreateGraphics(); g.Clear(this.BackColor); Font boldFont = new Font(this.Font, FontStyle.Bold); SizeF s = g.MeasureString(treeMax.ToString(), boldFont); int span = treeMax - treeMin; int totalNodes = intTree.treeNodes.Count; int width = pbTree.Width - CIRCLE_SIZE - HIGHLIGHT_OFFSET * 2; int height = pbTree.Height - CIRCLE_SIZE - HIGHLIGHT_OFFSET * 2; int widthIncrement = (int)((double)width / totalNodes); int heightIncrement = (int)((double)height / totalLevels); //g = pbTree.CreateGraphics(); //g.DrawLine(pen1, 250, 50, 400, 200); List <TreeNode <int> > sortedList = intTree.treeNodes.OrderBy(o => o.Element).ToList(); List <int> elements = sortedList.Select(o => o.Element).ToList(); int x = HIGHLIGHT_OFFSET; foreach (TreeNode <int> t in sortedList) { int y = t.Level * heightIncrement + HIGHLIGHT_OFFSET; RectangleF rect = new RectangleF(x, y, CIRCLE_SIZE, CIRCLE_SIZE); Brush brush1 = Brushes.Black; if (t.Parent != null) { int parentX = elements.IndexOf(t.Parent.Element) * widthIncrement + CIRCLE_SIZE / 2; int parentY = t.Parent.Level * heightIncrement + CIRCLE_SIZE / 2; gb.DrawLine(pen1, parentX, parentY, x + CIRCLE_SIZE / 2, y + CIRCLE_SIZE / 2); } gn.FillEllipse(brush1, rect); StringFormat stringFormat = new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }; string text = t.Element.ToString(); float fontScale = Math.Max(s.Width / rect.Width, s.Height / rect.Height); // using (Font font = new Font(boldFont.FontFamily, boldFont.SizeInPoints / fontScale, GraphicsUnit.Point)) using (Font font = new Font(this.Font.FontFamily, this.Font.SizeInPoints / fontScale, FontStyle.Bold)) { gn.DrawString(text, font, Brushes.White, rect, stringFormat); } if (highlightItem != 0 && t.Element == highlightItem) { Pen highlightPen = new System.Drawing.Pen(Color.Red, 2F);; //int highlightX = elements.IndexOf(t.Element) * widthIncrement - 2; //int highlightY = t.Level * heightIncrement - 2; RectangleF rectHighlight = new RectangleF(x - 2, y - 2, CIRCLE_SIZE + 4, CIRCLE_SIZE + 4); gn.DrawEllipse(highlightPen, rectHighlight); } x += widthIncrement; } g.DrawImage(nodeLinkImage, 0, 0); g.DrawImage(nodeImage, 0, 0); pbTree.Image = result; } } else { pbTree.Image = null; pbTree.BackColor = SystemColors.Control; } }