changing a tree into a list question..

This is a discussion on changing a tree into a list question.. within the C Programming forums, part of the General Programming Boards category; Because if head == root, there wasn't a left side to do. Look up three lines....

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

  2. #17
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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,185
    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.

Page 2 of 2 FirstFirst 12
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, 12: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

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