# Binary Tree constroctur

• 03-01-2008
aama100
Binary Tree constroctur
hey guys ... It is my first time to practice with binary tree thing. I am not sure how my constructor is supposed to be .. I have written this code can anyone give me any hint for my constructor ?

Code:

```#include <iostream> using namespace std; class BinaryTreeImplementation{ private: public:         int key;                 BinaryTreeImplementation* root;         BinaryTreeImplementation* leftLeaf;         BinaryTreeImplementation* rightLeaf;                 BinaryTreeImplementation{                 leftLeaf = NULL;                 rightLeaf = NULL;         }                  void addNewNode(); }; void BinaryTreeImplementation::addNewNode(){         BinaryTreeImplementation* root = newNode();                   root->left = newNode();           root->right = newNode();                 root->left = leftLeaf;         root->right= rightLeaf; } int main(){ int num=0; while(1){                 cout << "(1)Enter a newe calculation string" << endl;         cout << "(2)Print the current tree in infix notation" << endl;         cout << "(3)Prting the current tree in prefix notation" << endl;         cout << "(4)Pring the current tree in postfix notation" << endl;         cout << "(5)Caculate the result of the current tree" << endl;         cout << "(6)Exit the program"<<endl;         cin >> num;                 if(num=1){         }         if(num=2){         }                if(num=3){         }         if(num=4){         }         if(num=5){         }         if(num=6){                 break;         }         if( num < 1 || num > 6){                 cout << " Error! Please choose one of the following options" << endl;         } }```
• 03-01-2008
Daved
Looks fine to me, except null isn't really a keyword. Use 0 instead. You might also want to use the initializer list, although that won't have much effect in this case.
• 03-01-2008
matsp
This code belongs somewhere in a function - currently it's not in a function and would not compile.
Code:

```        root->left = leftLeaf;         root->right= rightLeaf;```

Shouldn't this be private?
Code:

```        BinaryTreeImplementation *root;         BinaryTreeImplementation *leftLeaf;         BinaryTreeImplementation *rightLeaf;```
--
Mats
• 03-01-2008
iMalc
The members you have in the class does not make sense:

If you intend to use BinaryTreeImplementation for each node in the tree, then it is a mistake to have a root member in each one, unless it is actually a parent pointer, in which case it is perhaps badly named, and is lacking initialisation in the constructor.

If it is not used for each node, and instead just wraps the pointer to the tree then it has no business having left and right leaf pointers.
• 03-01-2008
aama100
I have updated the code above but honestly I am really confused about how to do a tree thing.. I have a book and I am reading an online tutorial but it doesn't seem help much... if anyone has a good tutorial for how to construct a binary tree or something ... ?

I added this to create a new node, but I am not sure how it will work?

Code:

```void BinaryTreeImplementation::addNewNode(){         BinaryTreeImplementation* root = newNode();                   root->left = newNode();           root->right = newNode();                 root->left = leftLeaf;         root->right= rightLeaf; }```
• 03-01-2008
matsp
Your posted code "looses" the newNode() by overwriting the left/right nodes with rightLeaf/leftLeaf. So that is diefnitely wrong - which is the right thing depends on what you are actually trying to achieve.

I think iMalc have a point - you should decide whether you are implementing a node or the root of a binary tree.
• 03-01-2008
CornedBee
Start here:
http://eternallyconfuzzled.com/tuts/..._tut_bst1.aspx
and then read the other tree tutorials, too.