Algorithm Problem – Binary Tree to List

Posted on March 31, 2007. Filed under: algorithmics, computer science, programming |

Here is an interesting problem i came across. If you are bored (and smart), give it a shot.

Some refreshers, and an example you can use:

An ordered binary tree looks like this:

tree

A circular doubly linked list looks like this:

list

 

 

The problem at hand:

Suggest a recursive algorithm which takes the ordered binary tree and rearranges the internal pointers to make a circular doubly linked list out of it. The “small” pointer should play the role of “previous” and the “large” pointer should play the role of “next”. The list should be arranged so that the nodes are in increasing order. Return the head pointer to the new list.

So here’s your final draft:

treelist

Go play.

Make a Comment

Make a Comment: ( 11 so far )

blockquote and a tags work here.

11 Responses to “Algorithm Problem – Binary Tree to List”

RSS Feed for Nirmal Thacker Comments RSS Feed

Hey, nice and thoughtful. My 10 minute solution is as follows:

The way a tree transforms into a linked list is when it just start going to one side. in this case its : 5-4-3-2-1 which is 4 to the left of 5…3 to the left of 4…etc…(you get the point). now the question is how do we go there…

Well we know that from balanced binary trees that when we get a disbalance, we do rotations to make it balanced. (a binary tree which is like 5-4-3-2-1 is the most disbalanced tree right?). Now we have a fairly balanced tree (assumed) so we’ll again do rotations to convert it to a disbalanced, one offshoot tree (like we want to). We write a recursive function (im not putting the implementation here) which will go as deep as finding a tree which is not a disbalanced tree. For example in above picture,,, the 1-2-3 sub tree. well do a rotation on 2…make it 3-2-1 (all in one direction to left). we keep doing it for every subtree that is balanced or not in the required form. and finally we’ll have the required linked list form. Now during that we didnt really do anything about making it a doubly linked list…which is fairly easy making every1’s right pointer point to the parent and the LEFT most node’s right pointer point to the root, point head at that node and there you have a doubly linked list binary tree. :) leme know about my solution. and yea, i have something similar at my blog as well. would you like to take a look at : http://zakimirza.wordpress.com/2007/03/25/quiz-2-tree-problem-ds/

Zaki, interesting technique!.. besides examining the efficiency of your algorithm would be a subject of another post..its really interesting cause i jus realized that given two different binary search trees on a given set of n keys, it is always possible to transform one tree into the other, using only a sequence of O(n) rotations. However since your recursive function digs into the tree… and now i wanna experiment this with several trees…and pulls out balance trees, converts them into a list, im wondering if the efficiency would be decent.
Nevertheless , its a super perspective.

Your blog is sure good! Great post here! http://zakimirza.wordpress.com/2007/03/28/about-quizes-and-blog/

Thank you for appriciating my blog. I think the efficiency in the worst case can be when the three is totally balanced. it would be fun to see how this bahaves on a AVL or redblack. But its gotta be some factor below n for sure. lets c.

that asks for another quiz btw :) given two differnt trees, convert between them OPTIMALLY. have you worked on graph drawing? this kind of problems arise there sparingly. For example, give a very dense graph, you might want to visualize it in different ways etc.

yes, i did have a graph theory course at college, however i’ve not tried many algorithms with graph theory concepts…
an avl and the red-black would be below O(n) i agree…

[...] on April 3rd, 2007. Zaki has presented an interesting perspective at the original problem [...]

The very order 1, 2, 3, 4, 5 indicates that what you require is in in-order. Though I haven’t thought strongly about its exact implmentation, I think this should be possible.

How about the following (based on inorder walk).

// explore returns a circular linked list // at each level.
explore(node):
lnode = explore(node.left)
lnode.left.right = node
node.left = lnode.left
rnode = explore(node.right)
rnode.left.right = lnode
lnode.left = rnode.left
rnode.left = node
node.right = rnode
return lnode

this runs in O(n) and works for a perfectly balanced tree. For an imbalanced tree we’ll need to create self loops if a left/right child is missing. I didn’t test this thoroughly but it seems to work.

I think I got a solution from this using Python.. :P

def to_list(tree):
if tree is None:
return None
else:
return [tree.key, tree.left, tree.right]

Oops! Nevermind


Where's The Comment Form?

  •  

    March 2007
    M T W T F S S
    « Feb   Apr »
     1234
    567891011
    12131415161718
    19202122232425
    262728293031  
  • a

  • Archives

  • Blog Stats

    • 52,530 hits
  •       

    This work is licensed under a Creative Commons Attribution-No Derivative Works 3.0 Unported

    Beware however that this refers only to parts which are obviously written by me and do not have any other information about licencing. Quoted text, pictures and other content created by others is copyrighted by the corresponding authors. If you are in doubt, ask before republishing any content.

Liked it here?
Why not try sites on the blogroll...