faqts : Computers : Programming : Languages : Java

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

23 of 45 people (51%) answered Yes
Recently 3 of 10 people (30%) answered Yes

Entry

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

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


----------------------------------------------------------------------
--- Knud van Eeden --- 29 October 2003 - 04:53 am --------------------

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

---

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

---

--- cut here: begin --------------------------------------------------

// Java stand alone
//
// Steps:
// 1. Type the statements in your favorite text editor
// 2. Save as <your class name>.java
// 3. Compile with 'Javac.exe <your classname>.java'
// 4. Run with 'Java.exe <your classname>'

public class CLASSTreeCreateExample {

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

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

--- cut here: end ----------------------------------------------------

---

It will define a node structure like the following:

 [left pointer - mydata - right pointer]

---
---

Here an example of the use of this binary tree.

---

// Java stand alone
//
// Steps:
// 1. Type the statements in your favorite text editor
// 2. Save as <your class name>.java
// 3. Compile with 'Javac.exe <your classname>.java'
// 4. Run with 'Java.exe <your classname>'

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.rightpointerP );
 }
}

---

If you compile this source code, it will show:

---

+------------------------------------------------------------+
|> javac.exe CLASSTreeCreateExample.java                     |
|                                                            |
|> java CLASSTreeCreateExample                               |
|                                                            |
|the current content of the leftpointer in this tree is null |
|                                                            |
|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 null|
|                                                            |
|Press any key to continue . . .                             |
+------------------------------------------------------------+

---
---

Book: see also:

---

[book: idea: Hubbard, John, R. - Schaum's Outline programming with 
Java - p. 152 - 'Recursive classes' - 
http://www.amazon.com/exec/obidos/tg/detail/-
/0071342109/qid=1067400571/sr=1-1/ref=sr_1_1/103-2842924-9105401?
v=glance&s=books]

---

[book: Hubbard, John, R. - Schaum's Outline of Data Structures with 
Java - http://www.amazon.com/exec/obidos/tg/detail/-
/0071361286/ref=pd_bxgy_img_2/103-2842924-9105401?v=glance&s=books]

---
---

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/26116/fid/791

---

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

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