Nth largest node in Binary Search Tree










0















I'm trying to find the Nth largest node in a Binary Search Tree given a number. All other solutions online find the Nth smallest node such as this one:



/**
* Return the key in the symbol table whose rank is @code k.
* This is the (k+1)st smallest key in the symbol table.
*
* @param k the order statistic
* @return the key in the symbol table of rank @code k
* @throws IllegalArgumentException unless @code k is between 0 and
* <em>n</em>–1
*/
public Key select(int k)
if (k < 0

// Return key of rank k.
private Node select(Node x, int k)
if (x == null) return null;
int t = size(x.left);
if (t > k) return select(x.left, k);
else if (t < k) return select(x.right, k-t-1);
else return x;



Source: https://algs4.cs.princeton.edu/32bst/BST.java.html



How would I convert the select(Node x, int k) method to find the Nth largest node?



For example, in a BST that looks like:



 30
/
20 35
/ /
15 25 31 40


The largest node has a key of 40.



The Ranked BST would look like:



 4
/
6 2
/ /
7 5 3 1









share|improve this question
























  • I haven't tested it, but changing left for right and right for left in the select method should do the trick.

    – raven1981
    Nov 15 '18 at 7:35















0















I'm trying to find the Nth largest node in a Binary Search Tree given a number. All other solutions online find the Nth smallest node such as this one:



/**
* Return the key in the symbol table whose rank is @code k.
* This is the (k+1)st smallest key in the symbol table.
*
* @param k the order statistic
* @return the key in the symbol table of rank @code k
* @throws IllegalArgumentException unless @code k is between 0 and
* <em>n</em>–1
*/
public Key select(int k)
if (k < 0

// Return key of rank k.
private Node select(Node x, int k)
if (x == null) return null;
int t = size(x.left);
if (t > k) return select(x.left, k);
else if (t < k) return select(x.right, k-t-1);
else return x;



Source: https://algs4.cs.princeton.edu/32bst/BST.java.html



How would I convert the select(Node x, int k) method to find the Nth largest node?



For example, in a BST that looks like:



 30
/
20 35
/ /
15 25 31 40


The largest node has a key of 40.



The Ranked BST would look like:



 4
/
6 2
/ /
7 5 3 1









share|improve this question
























  • I haven't tested it, but changing left for right and right for left in the select method should do the trick.

    – raven1981
    Nov 15 '18 at 7:35













0












0








0


1






I'm trying to find the Nth largest node in a Binary Search Tree given a number. All other solutions online find the Nth smallest node such as this one:



/**
* Return the key in the symbol table whose rank is @code k.
* This is the (k+1)st smallest key in the symbol table.
*
* @param k the order statistic
* @return the key in the symbol table of rank @code k
* @throws IllegalArgumentException unless @code k is between 0 and
* <em>n</em>–1
*/
public Key select(int k)
if (k < 0

// Return key of rank k.
private Node select(Node x, int k)
if (x == null) return null;
int t = size(x.left);
if (t > k) return select(x.left, k);
else if (t < k) return select(x.right, k-t-1);
else return x;



Source: https://algs4.cs.princeton.edu/32bst/BST.java.html



How would I convert the select(Node x, int k) method to find the Nth largest node?



For example, in a BST that looks like:



 30
/
20 35
/ /
15 25 31 40


The largest node has a key of 40.



The Ranked BST would look like:



 4
/
6 2
/ /
7 5 3 1









share|improve this question
















I'm trying to find the Nth largest node in a Binary Search Tree given a number. All other solutions online find the Nth smallest node such as this one:



/**
* Return the key in the symbol table whose rank is @code k.
* This is the (k+1)st smallest key in the symbol table.
*
* @param k the order statistic
* @return the key in the symbol table of rank @code k
* @throws IllegalArgumentException unless @code k is between 0 and
* <em>n</em>–1
*/
public Key select(int k)
if (k < 0

// Return key of rank k.
private Node select(Node x, int k)
if (x == null) return null;
int t = size(x.left);
if (t > k) return select(x.left, k);
else if (t < k) return select(x.right, k-t-1);
else return x;



Source: https://algs4.cs.princeton.edu/32bst/BST.java.html



How would I convert the select(Node x, int k) method to find the Nth largest node?



For example, in a BST that looks like:



 30
/
20 35
/ /
15 25 31 40


The largest node has a key of 40.



The Ranked BST would look like:



 4
/
6 2
/ /
7 5 3 1






java binary-search-tree ranking






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 5:30







R. Mo

















asked Nov 15 '18 at 5:18









R. MoR. Mo

127




127












  • I haven't tested it, but changing left for right and right for left in the select method should do the trick.

    – raven1981
    Nov 15 '18 at 7:35

















  • I haven't tested it, but changing left for right and right for left in the select method should do the trick.

    – raven1981
    Nov 15 '18 at 7:35
















I haven't tested it, but changing left for right and right for left in the select method should do the trick.

– raven1981
Nov 15 '18 at 7:35





I haven't tested it, but changing left for right and right for left in the select method should do the trick.

– raven1981
Nov 15 '18 at 7:35












1 Answer
1






active

oldest

votes


















0














One thing to note about this BST is that the rank starts from 0.



A simpler way



For a BST containing X elements numbered 0 to (X-1),



The Nth smallest element is equivalent to the (X-N)th largest element, and vice versa.



If you have no choice but to change the method



What select does in this case is something like a binary search on the rank. So if we adjust it such that it always goes towards the right for smaller ranks (and left for higher ranks), we can make it to return the answer we want.



Invert the x.right and x.left:



private Node select(Node x, int k) 
if (x == null) return null;
int t = size(x.right);
if (t > k) return select(x.right, k);
else if (t < k) return select(x.left, k-t-1);
else return x;






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312895%2fnth-largest-node-in-binary-search-tree%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    One thing to note about this BST is that the rank starts from 0.



    A simpler way



    For a BST containing X elements numbered 0 to (X-1),



    The Nth smallest element is equivalent to the (X-N)th largest element, and vice versa.



    If you have no choice but to change the method



    What select does in this case is something like a binary search on the rank. So if we adjust it such that it always goes towards the right for smaller ranks (and left for higher ranks), we can make it to return the answer we want.



    Invert the x.right and x.left:



    private Node select(Node x, int k) 
    if (x == null) return null;
    int t = size(x.right);
    if (t > k) return select(x.right, k);
    else if (t < k) return select(x.left, k-t-1);
    else return x;






    share|improve this answer



























      0














      One thing to note about this BST is that the rank starts from 0.



      A simpler way



      For a BST containing X elements numbered 0 to (X-1),



      The Nth smallest element is equivalent to the (X-N)th largest element, and vice versa.



      If you have no choice but to change the method



      What select does in this case is something like a binary search on the rank. So if we adjust it such that it always goes towards the right for smaller ranks (and left for higher ranks), we can make it to return the answer we want.



      Invert the x.right and x.left:



      private Node select(Node x, int k) 
      if (x == null) return null;
      int t = size(x.right);
      if (t > k) return select(x.right, k);
      else if (t < k) return select(x.left, k-t-1);
      else return x;






      share|improve this answer

























        0












        0








        0







        One thing to note about this BST is that the rank starts from 0.



        A simpler way



        For a BST containing X elements numbered 0 to (X-1),



        The Nth smallest element is equivalent to the (X-N)th largest element, and vice versa.



        If you have no choice but to change the method



        What select does in this case is something like a binary search on the rank. So if we adjust it such that it always goes towards the right for smaller ranks (and left for higher ranks), we can make it to return the answer we want.



        Invert the x.right and x.left:



        private Node select(Node x, int k) 
        if (x == null) return null;
        int t = size(x.right);
        if (t > k) return select(x.right, k);
        else if (t < k) return select(x.left, k-t-1);
        else return x;






        share|improve this answer













        One thing to note about this BST is that the rank starts from 0.



        A simpler way



        For a BST containing X elements numbered 0 to (X-1),



        The Nth smallest element is equivalent to the (X-N)th largest element, and vice versa.



        If you have no choice but to change the method



        What select does in this case is something like a binary search on the rank. So if we adjust it such that it always goes towards the right for smaller ranks (and left for higher ranks), we can make it to return the answer we want.



        Invert the x.right and x.left:



        private Node select(Node x, int k) 
        if (x == null) return null;
        int t = size(x.right);
        if (t > k) return select(x.right, k);
        else if (t < k) return select(x.left, k-t-1);
        else return x;







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 8:00









        Justin TayJustin Tay

        744




        744





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312895%2fnth-largest-node-in-binary-search-tree%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Barbados

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3