faqts : Computers : Programming : Languages : C#

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

29 of 45 people (64%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

C#: Datastructure: Tree: Binary: Create: Simple: How to create a binary tree in C#?

May 26th, 2006 16:43
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 29 October 2003 - 07:03 am --------------------

C#: Datastructure: Tree: Binary: Create: Simple: How to create a 
binary tree in C#?

---

Here is shown a simple definition of an empty binary tree.

---

--- cut here ---------------------------------------------------------

public class CLASSTreeCreateExample {

 private CLASSTreeCreateExample leftpointerP;
 private String mydataS;
 private CLASSTreeCreateExample rightpointerP;

 public static void main( String[] args ) {
   // do something
 }
}

--- cut here ---------------------------------------------------------

---

It will define a node structure like the following:

 [left pointer - mydata - right pointer]

---
---

Here an example of the use of this binary tree.

---

public class CLASSTreeCreateExample {

 private CLASSTreeCreateExample leftpointerP;
 private String mydataS;
 private CLASSTreeCreateExample rightpointerP;

 public static void main( String[] args ) {
   // do something
   CLASSTreeCreateExample myTreeO = new CLASSTreeCreateExample();
   myTreeO.leftpointerP = null;
   myTreeO.mydataS = "some info to store in this tree";
   myTreeO.rightpointerP = null;
   System.out.println( "the current content of the leftpointer in this 
tree is " + myTreeO.leftpointerP );
   System.out.println( "the current content of the data in this tree 
is " + myTreeO.mydataS );
   System.out.println( "the current content of the rightpointer in 
this tree is " + myTreeO.leftpointerP );
 }
}

---

If you compile this source code, it will show:

---

+-------------------------------------------------------------------+
|> "c:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\csc.exe"           |
|  CLASSTreeCreateExample.cs                                        |
|                                                                   |
|Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4          |
|for Microsoft (R) .NET Framework version 1.1.4322                  |
|                                                                   |
|Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.|
|                                                                   |
|> CLASSTreeCreateExample.exe                                       |
|                                                                   |
|the current content of the leftpointer in this tree is             |
|                                                                   |
|the current content of the data in this tree is some info to store |
|in this tree|                                                      |
|                                                                   |
|the current content of the rightpointer in this tree is            |
|                                                                   |
|Press any key to continue . . .                                    |
+-------------------------------------------------------------------+

---
---

Note:

I could not really find anything useful on the Internet about
datastructures for C#, nor did I find something in my books that could
help me with an example.

So my approach was to use the source code from Java, which does the
same.

And this was a successful strategy.

It compiled after some minor replacing of words.

---
---

Internet: see also:

---

BBCBASIC: Datastructure: Tree: Binary: Create: Simple: How to create a 
binary tree in BBCBASIC?
http://www.faqts.com/knowledge_base/view.phtml/aid/26122/fid/768

---

C++: Datastructure: Tree: Binary: Create: Simple: How to create a 
binary tree in C++?
http://www.faqts.com/knowledge_base/view.phtml/aid/25923/fid/163

---

Delphi: Datastructure: Tree: Binary: Create: Simple: How to create a 
binary tree in Delphi?
http://www.faqts.com/knowledge_base/view.phtml/aid/26110/fid/175

---

Java: Datastructure: Tree: Binary: Create: Simple: How to create a 
binary tree in Java?
http://www.faqts.com/knowledge_base/view.phtml/aid/26106/fid/165

----------------------------------------------------------------------