Problems creating Binary Tree

This is a discussion on Problems creating Binary Tree within the C++ Programming forums, part of the General Programming Boards category; Hi all, I'm doing an exercise from a book to design and draw a binary tree, with the constructor simply ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    329

    Problems creating Binary Tree

    Hi all,

    I'm doing an exercise from a book to design and draw a binary tree, with the constructor simply taking the number of levels as a constructor.

    I have it creating the correct number of nodes for the required level, however I am having trouble using the module operator within the constructor.

    This code works:

    Code:
    Binary_tree::Binary_tree(int l)
    :levels(l)
    {
    		if(levels>0)
    			add(Point(100,100));
    			nodes=pow(2.0, levels)-1;
    			
    			for(int i = 1; i< nodes; ++i) {
    				
    				add(Point(point(i-1).x+50, point(i-1).y+50));	
    			}
    }
    The problem is I obviously want the nodes to go left/right. So I used the modulo remainder to determine if the node is odd/even as such:
    Code:
    Binary_tree::Binary_tree(int l)
    :levels(l)
    {
    		if(levels>0)
    			add(Point(100,100));
    			nodes=pow(2.0, levels)-1;
    			
    			for(int i = 1; i< nodes; ++i) {
    				if(i%2 == 0)
    				
    				add(Point(point(i-1).x+50, point(i-1).y+50));	
    			}
    }
    However, I get a windows error box appearing saying vector subscript out of range? Any ideas?

    Thanks.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Please ignore. Have figured it out.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,628
    By the way, you could avoid #include <cmath> and the use of std::pow by changing:
    Code:
    nodes=pow(2.0, levels)-1;
    to:
    Code:
    nodes = (1U << levels) - 1;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by laserlight View Post
    By the way, you could avoid #include <cmath> and the use of std::pow by changing:
    Code:
    nodes=pow(2.0, levels)-1;
    to:
    Code:
    nodes = (1U << levels) - 1;
    What does the 1U mean?

    Thanks.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,628
    1 Unsigned.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Tree Search
    By C++Newbie in forum C++ Programming
    Replies: 7
    Last Post: 04-05-2011, 01:17 AM
  2. best STL method to implement a binary tree
    By MatthewDoucette in forum C++ Programming
    Replies: 8
    Last Post: 06-16-2006, 07:08 AM
  3. BST (Binary search tree)
    By praethorian in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2005, 08:11 AM
  4. binary search and search using binary tree
    By Micko in forum C++ Programming
    Replies: 9
    Last Post: 03-18-2004, 09:18 AM
  5. Templated Binary Tree... dear god...
    By Nakeerb in forum C++ Programming
    Replies: 15
    Last Post: 01-17-2003, 01:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21