GeeksforGeeks » Trees specific questions

Median of BST

(9 posts)

Tags:

  1. Shekhu
    Member
    Posted 2 years ago #

    Given a BST (Binary search Tree), how will you find median in that?
    Constraints:
    -No extra memory.
    - Algorithm should be efficient in terms of complexity.

  2. kartik
    Moderator
    Posted 2 years ago #

    No extra memory is allowed, so we can't use recursive tree traversal.

    1) Use Morris Traversal (http://geeksforgeeks.org/?p=6358) to get the number of nodes in BST. Let number of nodes be n.
    2) Traverse till the n/2the node using Morris traversal again.
    3) Print the middle node.

  3. Anubhav
    guest
    Posted 1 year ago #

    We can find the median by using the rabbit and the turtle pointer. The rabbit moves twice as fast as the turtle in the in-order traversal of the BST. This way when the rabbit reaches the end of traversal, the turtle in at the median of the BST. Please see the full expalaination at http://www.technicallyidle.com/2011/01/05/median-of-a-binary-seach-tree-bst/ .

  4. Niraj Nawanit
    guest
    Posted 1 year ago #

    I have thought of a way to find median in O(1). Tweak your insert function and delete function to always maintain the median. The tweaked inert and tweaked delete functions will still take O(lg n).

  5. Kumar
    Member
    Posted 1 year ago #

    Can anyone give the code for Rabbit & Turtle point method .

  6. Anubhav
    guest
    Posted 1 year ago #

    Updated the post with the code for the rabbit-turtle method. Check it out http://www.technicallyidle.com/2011/01/05/median-of-a-binary-seach-tree-bst/ .

  7. aimless
    guest
    Posted 11 months ago #

    @Anubhav & everyone,
    Though it may seem to work at the first sight, but its not working as expected,
    because morris algorithm changes the internal representation of the tree and hence turtle won’t see the same tree as that of rabbit. having two pointers changing the same tree is messing things up. you can try for the following BST
    preorder – 4 2 1 3 6 5 7
    inorder – 1 2 3 4 5 6 7 (having said BST, mentioning inorder was not necessary yet mentioned, as it helps)

  8. Kamal Tripathi
    guest
    Posted 7 months ago #

    Recursively go thru BST just like you go for in-order traversal. Say BST is perfectly balanced with nodes no. 1- 15. BST will look like:
    8
    4 12
    2 6 10 14
    1 3 5 7 9 11 13 15

    Now going by in-order traversal, fix median at 1 (since it is first entry, the lowest), then

    ==>2, median will still be at 1
    ==>3, median now shifts one place right at 2 now
    ==>4, median will still be at 2
    ==>5, median now shifts one place right at 3 now
    ..
    ..
    ..
    ==>15, median now shifts one place right at 8 now.

    So with a global pointer we can find the median

  9. aaman
    Member
    Posted 7 months ago #

    @kamal....
    but this method will take O(n) as we are traversing full tree,if O(n) is to be used then we can simply find median of inorder result which is always sorted and will take constant time .


Reply

You must log in to post.

RSS feed for this topic