Binary trees are a very useful data structure, and are used in things like data storage, and parsing of mathematical expressions. Implementing a binary tree is C# is not hard at all.
The first thing we need to do is define a class for the node of the tree. BinaryNode. This contains four variables: value which is an object containing whatever data you have set the node to. leftChildId and rightChildId are the ids of the child nodes linked to this node. parentId is the id pf the parent node to this node, and is not strictly necessary, but it does make modifying the tree much simpler - and more efficient.
The rest of the class is purely functions to get and set these values.
Next up we have the binary tree class, which has an arraylist, which gets populated with the nodes.
When a new tree is created, the first node - the root - is automatically created. This is done by creating a new instance of a BinaryNode, setting the value, and then adding it to the arraylist.
We then have functions to add nodes to the left or the right of the node - addLeftNode and addRightNode.
These functions create a new node, set the value and parent id, and then add the node to the arraylist, returning back the id of the new node. Then we assign the new id to either the left or right child value of the parent node.
The next functions get and set the value of a particular node.
The removeNode function removes a node from the tree, as well as all children nodes attached to it. It does this recursively, moving down the tree and deleting nodes as it goes upwards until it reaches the actual node we specified, which gets deleted last. The parent node’s child id then gets set to -1, which indicated that there is not longer a child attached.
The following two functions, setLeftChildNode and setRightChildNode takes an existing child node, and moves it from its current position and reattaches it to the specified node on either the left or right side depending on the function called.
Now we should have a fully operational tree, which you can add and remove nodes to, as well as getting and setting the values of the nodes.
Originally posted on my old blog, Smoky Cogs, on 26 Oct 2009