Binary Trees in c#

Started by
8 comments, last by snk_kid 18 years, 3 months ago
Binary trees are defined in Haskell or OCaml easily: Haskell: data BinaryTree a = Leaf a | Branch (BinaryTree a) a (BinaryTree a) Ocaml: type 'a binary_tree = Leaf of 'a | Tree of 'a binary_tree * 'a binary_tree;; As I know there is not such a beautiful way to define binary trees in c#. But what is the best way to define such data structures in c#?(Specially without using unsafe code.) Thanks
Advertisement
Why do you want a binary tree? In most cases binary trees can be replaced by use of one of the .NET Framework container classes.
Maybe, but I want to define a binary tree myself.
At the very basic you can do something like:
  class Node {    internal Node left = null;    internal Node right = null;    internal object data = null;  }

Add functions and wrappers as needed.
I would write

abstract class Tree<T>{}class Leaf<T> : Tree<T>{    public Leaf(T item)    {        this.Item = item;    }    public T Item;}class Branch<T> : Tree<T>{    public Branch(Tree<T> left, T item, Tree<T> right)    {        this.Left = left;        this.Item = item;        this.Right = right;    }    public Tree<T> Left;    public T Item;    public Tree<T> Right;}


(of course, you could C#-ify it a bit more by making the fields private and using properties, the Item field should be in the superclass using this design, etc. etc.)
class Node {
internal Node left = null;
internal Node right = null;
internal object data = null;
}
That is OK; but it is not going to use the memory efficiently.
The generic way seems to be interesting!
Quote:Original post by Kambiz
That is OK; but it is not going to use the memory efficiently.

If you can look at that and say it's not going to use memory efficient, but can't adapt it to something more memory efficient, you're never going to hack it as a computer programmer.
The problem is what you mean by "beautiful way".

With Haskell/OCaml, you would have your algorithms use pattern matching

match tree with    Leaf item                -> ...  | Tree (left, item, right) -> ...


Which translated litteraly in C# would look like
if ( tree is Leaf<T> ){    Leaf<T> leaf = (Leaf<T>) tree;    ...}else if ( tree is Branch<T> ){    Branch<T> branch = (Branch<T>) tree;    ...}

but that's not exactly what is considered good C# code. OO-style, using polymorphism, supposedly better, which I can best describe as crappy, especially if all you to do is implement algorithms such as checking for membership, adding items, etc. It's one algorithm, there aren't going to be more types of tree nodes, so why would you want to cut your algorithm into pieces and spread it over multiple classes? OO works in the wrong dimension in this case.

So, no, there's no beautiful way of doing it in C# according to my tastes.
The version that SamLowry gave is an example of the composite design pattern, it's how you simluate/emulate algebraic data types in OO languages that do not support algebraic data types or unions directly. In some cases this form is more space efficient at the cost of some time run-time efficiency, in your case since you want both internal and terminal nodes to have values/elements it doesn't make much difference in terms of space efficiency.

The only downside to using the composite pattern is typically you want to know about specific types, this is painful for languages (such as C# & C++) that do not support multimethods natively in which case you either simulate multimethods or apply the visitor design pattern. In your case it doesn't appear to be worth the trouble so using SiCrane's form makes slightly more sense although i'd change it slightly to use generics instead.

[Edited by - snk_kid on January 1, 2006 12:45:19 PM]

This topic is closed to new replies.

Advertisement