3

As mentioned in the Title , I have a Binary search tree. I want to convert it to sorted doubly linklist using recursion.

My code

 for each node in tree
   find max of left sub-tree and assign its right to present node ,present node left to max 
   find min of right sub-tree and assign its left to present node ,present node right to max 
   and now recursively do same thing to other nodes in BST .

but this solution is not efficient as it reaches each node more than one time .In my quest of optimized code i got a link from google greatTreeList sollution . I have searched the same in SO both sollutions are same and worked for me. I didnt understand the append function of the sollution as it contains code

join(alast,b)
join(blast,a)

For tree whose nodes are inserted in following order 10,5,9,6,8,7,12,11,13,14

can anyone please explain how join(alast,b) join(blast,a)

are linking node in each recursion call.

Community
  • 1
  • 1
Imposter
  • 2,666
  • 1
  • 21
  • 31
  • You do not want to get an external list but to update the bst nodes, am I right ? I suppose node is a record containing pointers for the BST part (left_child, right_child) and pointers for the List part (previous_node, next_node). – Kwariz Sep 06 '12 at 09:45
  • @Kwariz :i dont want to use any external list. I want to know the logic of append function in greatTreeList solution. – Imposter Sep 06 '12 at 09:50

3 Answers3

1

In order to convert a binary search tree to a sorted doubly linked list, one typically performs an inorder depth-first traversal, building the list along the traversal.

Try to come up with code that performs an inorder depth-first traversal and prints the items of the binary tree in sorted order. From there, it should be easy to complete your task.

Philip
  • 5,795
  • 3
  • 33
  • 68
1

I think you are over thinking this actually quite easy task - extracting the data from a binary tree in order is as simple as doing a depth first traversal - this is the point of a binary tree that it very efficiently gives you the elements in the sorted order.

So what you need to do is a standard depth first walk of the tree and each time you find a node add it to you linked list.

This in order depth first recursion is fairly straight forward is pseudocode:

Traverse(Node N)
  Traverse(N.left);
  Add N to the linked list
  Traverse(N.right);

I suggest you try this manually on you example so you see how it works.

Elemental
  • 7,365
  • 2
  • 28
  • 33
  • when you say add list to n what are the steps involved in it. and what will be the return value please explain.I'm always confused with what to return especially in recursion.I will be happiest if you give example especially the one i mentioned in this question – Imposter Sep 06 '12 at 09:32
  • @Elemental this algo needs to have global variables which should point to current node. In java we need static variable. thanks. – Trying Aug 06 '13 at 16:03
  • @Trying - actually NO. This can (and should) be run completely without static or global references. – Elemental Aug 07 '13 at 09:44
1

Expanding on Elemental's answer

Traverse(Node N)
  Traverse(N.left);
  Add N to the linked list
  Traverse(N.right);

To add N to the linked list,

your linked list class or data structure should have an append() method or similar.

Use your imagination, but something like this:

def append(N):
    new_tail = node(val=N, prev=self.tail, next=None)
    self.tail.next = new_tail
    self.tail = new_tail

of course you also need to add self.head = self.tail the first time you append.

Rusty Rob
  • 16,489
  • 8
  • 100
  • 116