BLOG.CSHARPHELPER.COM: Recursively draw a binary tree in C#
Recursively draw a binary tree in C#
This example demonstrates recursion. Recursion occurs when a method calls itself. It may call itself directly (simple recursion) or indirectly by calling another method that calls the first (indirect recursion). It may also call itself multiple times as in this example (multiple recursion).
In all cases, the method must have some sort of stopping condition to prevent it from recursing forever. In this example and many others, the recursive method takes a depth parameter. In each recursive call to the method, depth is reduced by 1. When depth reaches 1 (or in some programs 0), the recursion stops.
The following code shows this example's recursive DrawBranch method. This method draws a single branch in the tree in a given direction with a certain length.
// Recursively draw a binary tree branch. private void DrawBranch(Graphics gr, Pen pen, int depth, float x, float y, float length, float theta, float length_scale, float dtheta) { // See where this branch should end. float x1 = (float)(x + length * Math.Cos(theta)); float y1 = (float)(y + length * Math.Sin(theta));
The method first draws its own branch. Then if depth is greater than 1, the method recursively calls itself to draw two new branches turned by angles dtheta and -dtheta from the current branch. The new branches' lengths are the length of the current branch times length_scale so branches get shorter as you move out in the tree. (Although you can set length_scale > 1 for some interesting effects.)
By experimenting with the program's parameters, you can get some interesting effects. For example, try Depth = 13 and DTheta = 60. One warning, however. The program draws 2depth - 1 lines so the number of lines grows very quickly as depth increases. Don't try very large depths (for example 50) until you know how fast your computer is.
1/13/2013 10:26 PM
Eddie Bole wrote: A nice fractal tree. Would it be possible to have the branches alternating with different colours? Reply to this
1/14/2013 1:33 PMRod Stephens wrote: Sure. You would just need to keep track of the color you're using and then move to a new color on a new branch.
I'm not sure exactly what you're looking for here. For example, if you have alternate branches side-by-side use different colors, you'd basically have all left branches one color and all right branches another.
Or you could give each branch a random color.
Or you could give all branches at a given level the same color so the colors change the farther up the tree you go. I'll post an example that demonstrates that approach in a couple days (after I finish the current discussion of anonymous methods and lambda statements).
(If you want to see one of the other approaches, let me know and I'll post that, too.) Reply to this
gracias por tu buena ayuda, pues eres de gran ayuda a todos aquello que quieren dedicarse a la ciencia de la investigacion.
Reply to this
A nice fractal tree. Would it be possible to have the branches alternating with different colours?
Reply to this
Sure. You would just need to keep track of the color you're using and then move to a new color on a new branch.
I'm not sure exactly what you're looking for here. For example, if you have alternate branches side-by-side use different colors, you'd basically have all left branches one color and all right branches another.
Or you could give each branch a random color.
Or you could give all branches at a given level the same color so the colors change the farther up the tree you go. I'll post an example that demonstrates that approach in a couple days (after I finish the current discussion of anonymous methods and lambda statements).
(If you want to see one of the other approaches, let me know and I'll post that, too.)
Reply to this
I've posted an example at Draw a colored binary tree in C#.
Reply to this