Given a Binary search tree, the task is to implement backward iterator on it with the following functions.

**curr():**returns the pointer to current element.**prev():**iterates to the previous largest element in the Binary Search Tree.**isEnd():**returns true if there’s no node left to traverse else false.

Iterator traverses the BST in decreasing order. We will implement the iterator using a stack data structure.

**Initialisation:**

We will create a stack named “q” to store the nodes of BST. Create a variable “curr” and initialise it with pointer to root. While “curr” is not NULL

- Push “curr” in the stack ‘q’.
- Set curr = curr -> right

**curr()**

Returns the value at the top of the stack ‘q’.

Note:It might throw segmentation fault if the stack is empty.

**Time Complexity:** O(1)

**prev()**

Declare pointer varaible “curr” which points to node. Set curr = q.top()->left. Pop top most element of stack. While “curr” is not NULL

- Push “curr” in the stack ‘q’.
- Set curr = curr -> right.

**Time Complexity:** O(1) on average of all calls. Can be O(h) for a single call in the worst case.

**isEnd()**

Returns true if stack “q” is empty else returns false.

**Time Complexity:** O(1)

Worst Case space complexity for this implementation of iterators is O(h). It should be noticed that

iterator points to the top-most element of the stack.

Below is the implementation of the above approach:

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Node of the binary tree ` `struct` `node { ` ` ` `int` `data; ` ` ` `node* left; ` ` ` `node* right; ` ` ` `node(` `int` `data) ` ` ` `{ ` ` ` `this` `->data = data; ` ` ` `left = NULL; ` ` ` `right = NULL; ` ` ` `} ` `}; ` ` ` `// Iterator for BST ` `class` `bstit { ` `private` `: ` ` ` `// Stack to store the nodes ` ` ` `// of BST ` ` ` `stack<node*> q; ` ` ` `public` `: ` ` ` `// Constructor for the class ` ` ` `bstit(node* root) ` ` ` `{ ` ` ` `// Initializing stack ` ` ` `node* curr = root; ` ` ` `while` `(curr != NULL) ` ` ` `q.push(curr), curr = curr->right; ` ` ` `} ` ` ` ` ` `// Function to return ` ` ` `// current element iterator ` ` ` `// is pointing to ` ` ` `node* curr() ` ` ` `{ ` ` ` `return` `q.top(); ` ` ` `} ` ` ` ` ` `// Function to iterate to previous ` ` ` `// element of BST ` ` ` `void` `prev() ` ` ` `{ ` ` ` `node* curr = q.top()->left; ` ` ` `q.pop(); ` ` ` `while` `(curr != NULL) ` ` ` `q.push(curr), curr = curr->right; ` ` ` `} ` ` ` ` ` `// Function to check if ` ` ` `// stack is empty ` ` ` `bool` `isEnd() ` ` ` `{ ` ` ` `return` `!(q.size()); ` ` ` `} ` `}; ` ` ` `// Function to test the iterator ` `void` `iterate(bstit it) ` `{ ` ` ` `while` `(!it.isEnd()) ` ` ` `cout << it.curr()->data << ` `" "` `, it.prev(); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `node* root = ` `new` `node(5); ` ` ` `root->left = ` `new` `node(3); ` ` ` `root->right = ` `new` `node(7); ` ` ` `root->left->left = ` `new` `node(2); ` ` ` `root->left->right = ` `new` `node(4); ` ` ` `root->right->left = ` `new` `node(6); ` ` ` `root->right->right = ` `new` `node(8); ` ` ` ` ` `// Iterator to BST ` ` ` `bstit it(root); ` ` ` ` ` `// Function call to test the iterator ` ` ` `iterate(it); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

8 7 6 5 4 3 2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Implementing Forward Iterator in BST
- Implementing a BST where every node stores the maximum number of nodes in the path till any leaf
- K'th Largest Element in BST when modification to BST is not allowed
- Convert a normal BST to Balanced BST
- Two nodes of a BST are swapped, correct the BST | Set-2
- Implementing a Linked List in Java using Class
- Implementing Generic Graph in Java
- Implementing upper_bound() and lower_bound() for Ordered Set in C++
- A program to check if a binary tree is BST or not
- Find the largest BST subtree in a given Binary Tree | Set 1
- Print BST keys in the given range
- Sorted Array to Balanced BST
- Construct BST from given preorder traversal | Set 2
- Add all greater values to every node in a given BST
- Inorder predecessor and successor for a given key in BST
- Count BST nodes that lie in a given range
- Overview of Data Structures | Set 2 (Binary Tree, BST, Heap and Hash)
- Check whether BST contains Dead End or not
- Largest BST in a Binary Tree | Set 2
- Largest number in BST which is less than or equal to N

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.