Thread: binary trees

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    15

    binary trees

    i hav started started studying binary trees.i m stuck with one problem,which is as follows:-
    1)create a binary tree(not a binary search tree) in C using elements from a array[25] such that for every index m , left child is the 2m index value in array and right child will be in 2m+1-th index

    for example:- For a node A[3] the left child is the A[2*3] index element and right child is in A[(2*3) +1 ] th position. So left child of A[3] is A[6] and right is A[7].
    and similarly for other nodes.
    please help me solving the above problem as i thought a lot over it but could n't find a good solution.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Err... what do you find problematic about it?
    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

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    15
    i dont know how to create this kind of tree ,beacuse if i want to insert
    A[6], it should become left child of A[3] , i cant understand the logic behind the problem;
    array A[25] contains integers (random)
    the tree should look like this
    A[1]
    / \
    A[2] A[3]
    / \ / \
    A[4] A[5] A[6] A[7]

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Animesh Gaitond
    i cant understand the logic behind the problem
    The idea is that the links are implied by the order of the elements in the array. So to insert at index 6, you insert at index 6. That is all there is to it (for now). Now, if you are asked to print the elements in pre-order, for example, then you follow the "links" by performing the index calculations for the child nodes.
    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

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    15
    how should the function of creating the tree look like?how will recursion take place in the given function?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Animesh Gaitond
    how should the function of creating the tree look like?
    Since there are apparently no constraints on the elements, you can simply treat it as insertion into an array, and that is what it is.

    Quote Originally Posted by Animesh Gaitond
    how will recursion take place in the given function?
    Using simple arithmetic: if you know the index of the root of the subtree, you can access its children by computing their indices.
    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 trees
    By Nick6pcm in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2008, 07:10 PM
  2. Converting From Binary Tree to Threaded Binary Trees
    By elton_fan in forum C Programming
    Replies: 15
    Last Post: 11-08-2007, 11:41 PM
  3. Binary Trees MINI MAXING, probability trees
    By curlious in forum General AI Programming
    Replies: 3
    Last Post: 09-30-2005, 10:57 AM
  4. Binary Trees
    By confuted in forum C++ Programming
    Replies: 1
    Last Post: 04-22-2003, 04:55 PM
  5. traversing binary trees or partial trees
    By sballew in forum C Programming
    Replies: 4
    Last Post: 12-05-2001, 09:19 PM