Skip to main content

Command Palette

Search for a command to run...

Iterator Design Pattern

Updated
β€’1 min read
Iterator Design Pattern
P

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

27 views

More from this blog

Blogs by Paras

52 posts

Hello, I'm Paras Kaushik! πŸ‘‹ I'm a dedicated software engineer based in India, specializing in C++ and proficient in the MERN stack. πŸ’Ό Open to Collaboration