|
||||||
Title: Nodes Of BST exchanged Post by TrozanHrorse on Feb 5th, 2009, 8:27am I heard an interview question which goes as follows: Two nodes of the BST are exchanged , and a root pointer is given. We have to correct the BST........ hw this is to be done??? |
||||||
Title: Re: Nodes Of BST exchanged Post by towr on Feb 8th, 2009, 2:56pm I'd start with a depth first traversal, and check whether the child nodes are in the right place for it to be a BST. If all the elements are unique, there shouldn't be a problem finding them in this way. And if one of the nodes isn't the ancestor of the other, then you'll find both. And then you just switch the pointers in the parent nodes. |
||||||
Title: Re: Nodes Of BST exchanged Post by R on Apr 10th, 2009, 4:06pm on 02/08/09 at 14:56:30, towr wrote:
Nodes are exchanged or two nodes' values are exchanged??? if it is the latter, then switching the pointers will change the BST. |
||||||
Title: Re: Nodes Of BST exchanged Post by towr on Apr 11th, 2009, 3:39am on 04/10/09 at 16:06:50, R wrote:
Otherwise you need to find both values that are out of place and switch them. |
||||||
Title: Re: Nodes Of BST exchanged Post by R on Apr 11th, 2009, 4:34am on 04/11/09 at 03:39:37, towr wrote:
so just two nodes are exchanged... their subtree (descendents) are not??? |
||||||
Title: Re: Nodes Of BST exchanged Post by towr on Apr 11th, 2009, 4:38am on 04/11/09 at 04:34:27, R wrote:
|
||||||
Title: Re: Nodes Of BST exchanged Post by R on Apr 12th, 2009, 7:06pm on 04/11/09 at 04:38:26, towr wrote:
well then those two nodes can't have a ancestor-descendant relationship. As one will be in the subtree of other. Moving the ancestor will move the descendant too. |
||||||
Title: Re: Nodes Of BST exchanged Post by TrozanHrorse on Apr 12th, 2009, 10:23pm Don't know the exact language of the question as I have only heard of it(most probably the question is exchange of values of the nodes)...But let's discuss the solution to both of these question..... |
||||||
Title: Re: Nodes Of BST exchanged Post by towr on Apr 13th, 2009, 1:11am on 04/12/09 at 19:06:04, R wrote:
Of course, as it turns out, if one of the exchanged nodes is originally the parent of the other, you won't find any problem in the BST, since the problem is disconnected from it. |
||||||
Title: Re: Nodes Of BST exchanged Post by R on Apr 13th, 2009, 7:07am on 04/13/09 at 01:11:51, towr wrote:
Yes ofcourse two poitners can be exchanged. I was talking in the context of this problem. I meant, if that is the case, then we won't be able to solve the problem(correct the BST). |
||||||
Title: Re: Nodes Of BST exchanged Post by R on Apr 13th, 2009, 7:14am on 04/12/09 at 22:23:52, TrozanHrorse wrote:
Well in case of exchanged-values, we will do an in-order traversal and see which nodes are out of order, and will remember their address. At the end just swap their contents. |
||||||
Title: Re: Nodes Of BST exchanged Post by TrozanHrorse on Apr 13th, 2009, 7:20am It will not be easy to do that as there will be many cases and moreover if any how we do it by that method that will not be an efficient approach |
||||||
Title: Re: Nodes Of BST exchanged Post by towr on Apr 13th, 2009, 7:50am on 04/13/09 at 07:07:58, R wrote:
on 04/13/09 at 07:20:57, TrozanHrorse wrote:
|
||||||
Title: Re: Nodes Of BST exchanged Post by R on Apr 13th, 2009, 10:14am on 04/13/09 at 07:20:57, TrozanHrorse wrote:
on 04/13/09 at 07:20:57, TrozanHrorse wrote:
This will work... Code:
Swap(node1->data, node2->data); |
||||||
Title: Re: Nodes Of BST exchanged Post by R on Apr 13th, 2009, 4:59pm For the case: when the nodes are exchanged (along with their subtrees) not only values, the following approach will work. Time O(n). Assuming that the exchanged nodes were not related by ancestor-descendant relationship. Code:
swap addresses exch_node[0] and exch_node[1]. somthing like Code:
will work. |
||||||
Title: Re: Nodes Of BST exchanged Post by TrozanHrorse on Apr 14th, 2009, 3:05am Thanx R for the code my problem is solved .....and thanx everybody for your responses :) |
||||||
Title: Re: Nodes Of BST exchanged Post by __Debug on Jul 23rd, 2009, 8:50pm i think we do not need to search for both the nodes. Once first 'bad' node is found using inorder traversal (or by using the valid BST property as R suggested) , just look for the right position of that node using binary search. There we can find the second 'bad' node (as bad nodes are merely exchanged) . exchange the two |
||||||
Powered by YaBB 1 Gold - SP 1.4! Forum software copyright © 2000-2004 Yet another Bulletin Board |