Thread: changing a tree into a list question..

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because if head == root, there wasn't a left side to do. Look up three lines.

  2. #17
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    1. List-ify the left half of the tree.
    2. List-ify the right half of the tree.
    3. Link the rightmost element of the left half to the root.
    4. Link the root to the leftmost element of the right half.
    5. Done.

    The special cases are where the left or right halves are empty lists.

    The following prototype may inspire you:

    Code:
    void *tree2list_helper(node *root, node **head, node **tail);
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #18
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    head equals root only once in the most left node (the smallest)

    we dont want it to run on this case

    thanks

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by transgalactic2 View Post
    head equals root only once in the most left node (the smallest)

    we dont want it to run on this case

    thanks
    head equals root for every node that doesn't have a left child. (Notice that root is the parameter to the function, not the actual root of the tree.)

  5. #20
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    where can i find a tutorial on binary tree recursion

    i searched in google:
    Code:
    http://www.google.co.il/search?hl=iw&sa=X&oi=spell&resnum=0&ct=result&cd=1&q=binary+tree+recursion&spell=1
    and there is no tutorials for this kind of stuff

    ??

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The problem is perhaps that recursion wasn't part of the subject, but the tutorial here on Cprogramming about binary trees is using recursion:
    http://www.cprogramming.com/tutorial/lesson18.html

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  2. Resource manager tree
    By VirtualAce in forum Game Programming
    Replies: 23
    Last Post: 09-07-2007, 10:27 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. question about linklist
    By fj8283888 in forum C Programming
    Replies: 1
    Last Post: 04-14-2004, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM