+ Reply to Thread
Results 1 to 3 of 3

Thread: Find 4th smallest element of a tree

  1. #1
    TopGun Guest

    Find 4th smallest element of a tree

    given a binary search tree diagram, you need to find 4th smallest element of that tree.

  2. #2
    arun4interviews Guest

    Re: Find 4th smallest element of a tree

    For this specific problem you may do an inorder traversal of the tree.
    We know inorder traversal yields elements of tree in increasing order, keep a counter
    for number of elements traversed and return the fourth element.

    C implementation:-
    struct NODE
    {
    struct NODE *left;//in Java simple references would work as NODE left;
    int value;
    struct NODE *right;
    }

    int inorder(struct NODE *curr)
    {
    static int i=0;
    if(curr->left != NULL) inorder(curr->left); /*step-1 & step-2*/
    printf("%d", curr->value); /*step-3*/
    i++;
    if(i==4)
    return curr->value;
    if(curr->right != NULL) inorder(curr->right); /*step-4*/
    }

  3. #3
    game.iiith Guest
    C'mmon .. if you use globals or static variables . what's the use of this question !!!!

    Do it without static and globals !!!

    Avi
    Avi Dullu's Code Arena

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Find 6th largest element in an array
    By Surfer in forum Adobe
    Replies: 4
    Last Post: 5th December 2010, 01:08
  2. Find level of an element in a tree
    By Surfer in forum Adobe
    Replies: 1
    Last Post: 23rd March 2010, 20:35
  3. Find depth of a tree
    By TopGun in forum Adobe
    Replies: 1
    Last Post: 15th January 2009, 15:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts