Thread: Binary Tree constroctur

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    58

    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;
    	}
    
    }
    Last edited by aama100; 03-01-2008 at 02:13 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    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;
    }
    Last edited by aama100; 03-01-2008 at 02:19 PM.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Start here:
    http://eternallyconfuzzled.com/tuts/..._tut_bst1.aspx
    and then read the other tree tutorials, too.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM