Finding the $j^{th}$ Smallest Element in an AVL Tree

In summary, the conversation discussed an algorithm for finding the $j^{th}$ smallest element in an AVL tree in $O(\log m)$ time, where $m$ is the number of nodes in the tree. The algorithm involves checking the number of nodes in the left subtree of each node and navigating through the tree until the desired element is found. The conversation also included a C++ implementation of the algorithm and a discussion on the reasoning behind the second argument in one of the function calls.
  • #1
evinda
Gold Member
MHB
3,836
0
Hello! (Wave)

We consider an $\text{ AVL }$ tree $T$. At each node $v$ of the tree the number of nodes of the left subtree of $v$ is stored.
I want to write an algorithm that returns the $j^{th}$ smallest element of the tree in time $O(\log m)$ where $m$ is the number of nodes of the tree.

How could we do this? Could you give me a hint? (Thinking)
 
Technology news on Phys.org
  • #2
An $\text{ AVL } tree$ is a sorted binary tree. So the smallest element will be at the last level of the left subtree of the root, right? (Thinking)
 
  • #3
We know that at each node $v$ of the tree, the number of nodes of the left subtree of $v$ is stored.
Does this mean that the number of nodes of the left subtree is the key or are there two fields? (Thinking)
 
  • #4
I think that we could do it like that:

We check if the number of nodes of the left subtree of the root is smaller that $j-1$ and if this holds we know that the $j^{th}$ smallest element is at the left subtree of the root.
If the number of nodes of the left subtree of the root is equal to $j-1$, then we know that the root is the $j^{th}$ smallest element.
Otherwise, we know that we can find the desired element at the right subtree of the root.So if the root is not the $j^{th}$ smallest element , we will look at the nodes of the left/right subtree, till we find a node,of which the left subtree has $j-1$ elements.Am I right? (Thinking)
 
  • #5
You haven't precisely specified an algorithm. Here's an implementation in C++:

Code:
class Node {
public:
  int key;
  int leftCount;
  Node* left;
  Node* right;
  Node();
};

Node* findKthSmallest(Node* p, int k) {
  int n = p->leftCount;
  if (n == k - 1) {
    return (p);
  }
  if (k <= n) {
    return (findKthSmallest(p->left, k));
  }
  return (findKthSmallest(p->right, k - n - 1));
}
 
  • #6
johng said:
You haven't precisely specified an algorithm. Here's an implementation in C++:

Code:
class Node {
public:
  int key;
  int leftCount;
  Node* left;
  Node* right;
  Node();
};

Node* findKthSmallest(Node* p, int k) {
  int n = p->leftCount;
  if (n == k - 1) {
    return (p);
  }
  if (k <= n) {
    return (findKthSmallest(p->left, k));
  }
  return (findKthSmallest(p->right, k - n - 1));
}

Could you maybe explain me why at this command: [m] return (findKthSmallest(p->right, k - n - 1)); [/m] the second argument is $k-n-1$?
Why are we looking for the $(k-n-1)^{th}$ smallest element of the right subtree? (Thinking)
 
  • #7
Think of the keys in the tree as being 1,2, etc. Of course, this need not be the case but it's easier to think this way. As you originally pointed out, the kth smallest element in a list is characterized by there being k-1 elements in the list that are smaller. Now if the function gets to
return(findKthSmallest(p->right,k-n-1), it is true that there are n elements in the left subtree smaller than k;also k is not n+1. So in the right subtree, there are then k-n-1 elements smaller than k. That is in the right subtree we want the (k-n-1)th smallest element.
Maybe it would help if you traced the function for the following AVL tree (the 1st value in each node is the key: the 2nd value is leftCount):

641dop.png
 
Last edited:
  • #8
johng said:
Think of the keys in the tree as being 1,2, etc. Of course, this need not be the case but it's easier to think this way. As you originally pointed out, the kth smallest element in a list is characterized by there being k-1 elements in the list that are smaller. Now if the function gets to
return(findKthSmallest(p->right,k-n-1), it is true that there are n elements in the left subtree smaller than k;also k is not n+1. So in the right subtree, there are then k-n-1 elements smaller than k. That is in the right subtree we want the (k-n-1)th smallest element.
Maybe it would help if you traced the function for the following AVL tree (the 1st value in each node is the key: the 2nd value is leftCount):

Great! (Happy) Thanks a lot!
 

Related to Finding the $j^{th}$ Smallest Element in an AVL Tree

What is an AVL Tree?

An AVL tree is a self-balancing binary search tree that maintains a height difference of at most one between its left and right subtrees. This ensures efficient insertion, deletion, and search operations with a time complexity of O(log n).

What is the $j^{th}$ smallest element in an AVL Tree?

The $j^{th}$ smallest element in an AVL tree is the element that is located at the $j^{th}$ position when the tree is sorted in ascending order. In other words, it is the $j^{th}$ element when the tree is traversed in an in-order manner.

How can I find the $j^{th}$ smallest element in an AVL Tree?

To find the $j^{th}$ smallest element in an AVL tree, you can use the in-order traversal method. Start from the root node and traverse the tree in an in-order manner, keeping track of the number of nodes you have visited. Once you have visited $j-1$ nodes, the next node you encounter will be the $j^{th}$ smallest element.

What is the time complexity of finding the $j^{th}$ smallest element in an AVL Tree?

The time complexity of finding the $j^{th}$ smallest element in an AVL tree is O(log n). This is because the tree is self-balancing, and the in-order traversal method will only visit a logarithmic number of nodes.

Can the $j^{th}$ smallest element be found in constant time in an AVL Tree?

No, the $j^{th}$ smallest element cannot be found in constant time in an AVL tree. This is because the tree needs to be traversed in order to determine the position of the $j^{th}$ smallest element, which has a time complexity of O(log n).

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
15
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
14
Views
3K
Replies
9
Views
1K
Back
Top