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?

56 of 88 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++?

Oct 28th, 2003 22:58
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 07 October 2003 - 10:23 pm --------------------

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.

---

#include <iostream.h>

struct tree {
  struct tree *leftpointerP;
  struct tree *rightpointerP;
  void *mydata;
};


main() {
}

---

It will define a node structure like the following:

 [left pointer - mydata - right pointer]

---
---

Here an example of the use of this binary tree.

---

#include <iostream.h>

struct tree {
  struct tree *leftpointerP;
  struct tree *rightpointerP;
  string mydataS;
};

struct tree *rootp;

main() {

 rootp = ( struct tree * ) malloc ( sizeof( struct tree ) ); // new 
(=create space in memory for your tree)

 cout << "the current content of the leftpointer in this tree is "; 
rootp->leftpointerP; // show the content of your leftpointer

 cout << "\n";

 cout << "the current content of the data in this tree is "; rootp-
>mydataS; // show the current content of your data

 cout << "\n";

 cout << "the current content of the leftpointer in this tree is "; 
rootp->rightpointerP; // show the content of your rightpointer

 cout << "\n";
}

---

If you compile this source code, it will show:

+------------------------------------------------------+
|> bcc32.exe -Ic:\borland\bcc55\include                |
|  -Lc:\borland\bcc55\lib mytreeexample.cpp            |
|                                                      |
|> mytreeexample.exe                                   |
|                                                      |
|the current content of the leftpointer in this tree is|
|                                                      |
|the current content of the data in this tree is       |
|                                                      |
|the current content of the leftpointer in this tree is|
|                                                      |
|Press any key to continue . . .                       |
+------------------------------------------------------+

---
---

Book: see also:

---

[book: Flamig, Bryan - practical datastructures in C++ - p. 5 -
 'treating datastructures in the abstract' - 
http://search.barnesandnoble.com/booksearch/isbnInquiry.asp?
userid=2TFIGCDXI3&isbn=0471009555&itm=1]

---

[book: Schildt, Herbert - advanced C - p. 68 - 'binary trees' - 
http://www.amazon.com/exec/obidos/tg/detail/-
/0078812089/qid=1067386182/sr=8-1/ref=sr_8_1/103-2842924-9105401?
v=glance&n=507846]

---
---

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

---

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

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