Javatpoint Logo
Javatpoint Logo

K'th Largest Element in BST Using Constant Extra Space Using Python

What is a Binary Search Tree?

The Binary Search Tree is a binary data structure containing various nodes having a few properties, including:

  • The left subtree nodes are less than the root node.
  • The right subtree nodes are more than the root node.
  • The sub-nodes of each node of the tree nodes form a Binary Search Tree.

Problem Statement

We need to find the Kth largest element in the existing binary search tree. It means we first arrange the elements of the binary search tree in descending order; then, we will search the kth largest element from the binary search tree.

Let's understand the problem statement with the help of an example.

Example

Input:

Output:

14

Explanation:

We have a binary search tree and need to search the Kth largest element. We have given the input as 4, i.e., k is 4. We need to search for 4th largest element from the binary search tree. We will first arrange the elements of the binary search tree in descending order and then find the 4th largest element.

Approach to Find Kth Largest Element in a BST

There are different approaches to finding the kth element in BST. This includes:

1. Naive Approach

Using the naive approach, we can store the inorder traversal, and then we will find the n - k + 1 element where n is the number of elements and k is the element we need to search. This approach will have O(N) space complexity, which means it will take more space and is inefficient.

Thus, we will use a more efficient approach to find the Kth largest element in the Binary Search Tree.

2. Reverse Morris Traversal

This approach is based on the threaded binary trees. The threaded binary tree is just a normal Binary tree with an extra thread, which helps to traverse the tree easily. It uses NULL pointers, which store the successor and predecessor (previous and next node) used to use the unused memory by these NULL pointers.

Reverse Morris Traversal is only the reversed Morris Traversal. Unlike Morris Traversal, in which we first move to the left subtree and then to the right subtree without using stack or recursion, in the Reverse Morris Traversal, we will first traverse the right subtree and then the left subtree. It has a constant O(1) extra memory consumed. It is the best and most efficient approach to finding the kth largest element in the binary search tree.

The logic for this problem statement is to perform the reverse inorder traversal, which by default gives the list in sorted descending order, along with tracking the number of nodes visited. We will print the element when this count equals the element we are searching for (k).

Implementation to find the kth largest element in the binary search tree

Code:

Output:

Enter the kth element to be searched: 3
The  K-th largest Node in Binary Search Tree is :  11

Explanation:

The binary tree used is:

K'th Largest Element in BST Using Constant Extra Space Using Python

We first initialised the current node as the root node. We initialised a count variable, traversed the binary tree in reverse order, checked whether the kth element matches the current node, and counted the number of nodes. We found that the element 11 is the kth largest element (k = 3).







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA




news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news