Iterator Design Pattern

Hello, I'm Paras Kaushik! π I'm a dedicated software engineer based in India, specializing in C++ and proficient in the MERN stack.
π€ Interested in collaborating on innovative projects that require my technical expertise.
π¬ Passionate about participating in discussions related to software architecture and best practices.
π§ Feel free to reach out to me via email: [paraskaushik12@gmail.com]
π Connect with me on LinkedIn: [https://www.linkedin.com/in/the-paras-kaushik/]
An iterator is an object that facilitates the traversal of a data structure
It keeps a ref to the current element
Knows how to move to a different element


Ex. Above are inbuilt C++ forward and reverse iterators. You can also get constant iterators using crbegin()

In C++ the range-based for-loop has no reverse iterator
Building an iterator for a Binary Tree
Let us define a BinaryTree Node class of generalised key value

Similarly, let's define a class BinaryTree which maintains our Binary Tree

Now we we will build a Pre-order iterator for this tree (Node->Left->Right)

We can also set this as the default iterator for our BinaryTree class

Now to finally create a tree, we can do something like the below





