Introduction

This article is about binary trees. A Binary Tree contains unlimited number of nodes, the nodes can be removed, added, searched, etc. Here we will discuss on how to make a binary tree on c# code, and how to draw that on bitmap using GDI+.

Each
node on the binary tree has a unique value. for example 776 on the top of the
image is a unique value for the root node on the tree.

The rules of adding a new node to the tree are: starting from the root node,1. if the node's value is less than the root's value, it would be added to the left node of root node.2. if the node's value is greater than the root's value, it would be added to the right node of root node. Consider that adding a node to any node would have the same process as 1 and 2.

The rules of removing a node from the binary tree are:1. the node has no child => simply remove that node2. the node just has a left child => the left child of the removing node will take it's position on the tree3.
the node has right child, and the right child does not have any left
child => the right child of the node will take the position of the
removing node on the tree4. the node has right child, and the right
child also has left child => the most left child of the right child
will be removed (removing this node will cause a recursive algorithm!)
and take the position of the removing node.

Using the code

When the application starts, it randomly adds some nodes to the tree. By
pressing the add button (or pressing the enter key on textbox) the
value of the textbox wil be added as a node to the binary tree. By pressing the create button a new binary tree will be created. By pressing the remove button the node containing the value of textbox will be removed from the treeBy pressing the "Rnd Add" button a random value will be added to the three as a node. By pressing the save button the current image on the picturebox will be saved on the disk.

A complete description of how to use the code and it's methods
is represented on the main source code as XML parts. to understand it
completely we prefer you read the main source code attached to this
article.

The paint operation is really easy: each node
will draw itself and it's child nodes, the method of drawing is
recursive calling every child to draw itself then passing the result to
the parent so the parent can draw itself and this process happens for
all the nodes

Salam Mojtaba khan;
Your work is great but could you please add some function to draw bifurcating(binary) trees with the aid of Newick Format as input?
E.g. Input: ((3,(0,1)),(2,4)) Output: Graphical tree
It would certainly useful in many cases such as Bioinformatics->Phylogenetic trees study especially if it is not limited to binary trees --> E.g. Input: ((0,1,2,3),4,(5,6,(7,8)))
Dar panahe hagh

Dude you save my life.I dont have a lot of knowledge in all this graphic stuff.
I'm working with N nodes trees and stuff like that for a college project and I make it work in CONSOLE but with help of this article I'm gonna make it 10 times better .
Thank you I hope one day I could help you as much as you did now.
Thanks

hi,
the project is reusable on vs2008, you may need just to run the project with vs2008,
if did not opened well, you can create your own project and paste the codes and classes from the main source code to your own project.