• Create Account

## Binary Trees in c#

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

9 replies to this topic

### #1Kambiz  Members

758
Like
0Likes
Like

Posted 01 January 2006 - 04:12 AM

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

### #2SiCrane  Moderators

11528
Like
0Likes
Like

Posted 01 January 2006 - 04:15 AM

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.

### #3Kambiz  Members

758
Like
0Likes
Like

Posted 01 January 2006 - 04:21 AM

Maybe, but I want to define a binary tree myself.

### #4SiCrane  Moderators

11528
Like
0Likes
Like

Posted 01 January 2006 - 04:23 AM

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.

### #5SamLowry  Members

1865
Like
0Likes
Like

Posted 01 January 2006 - 04:24 AM

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.)

### #6Kambiz  Members

758
Like
0Likes
Like

Posted 01 January 2006 - 04:27 AM

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.

### #7Kambiz  Members

758
Like
0Likes
Like

Posted 01 January 2006 - 04:28 AM

The generic way seems to be interesting!

### #8SiCrane  Moderators

11528
Like
0Likes
Like

Posted 01 January 2006 - 04:37 AM

Quote:
 Original post by KambizThat 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.

### #9SamLowry  Members

1865
Like
0Likes
Like

Posted 01 January 2006 - 04:42 AM

The problem is what you mean by "beautiful way".

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.

### #10snk_kid  Members

1312
Like
0Likes
Like

Posted 01 January 2006 - 04:45 AM

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]

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.