binary trees

This is a discussion on binary trees within the C Programming forums, part of the General Programming Boards category; i hav started started studying binary trees.i m stuck with one problem,which is as follows:- 1)create a binary tree(not a ...

  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
    22,256
    Err... what do you find problematic about it?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,256
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,256
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 08: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, 11:57 AM
  4. Binary Trees
    By confuted in forum C++ Programming
    Replies: 1
    Last Post: 04-22-2003, 05: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

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