diff --git a/iterator/README.md b/iterator/README.md index a98010c5a..71ad807cd 100644 --- a/iterator/README.md +++ b/iterator/README.md @@ -9,29 +9,34 @@ tags: --- ## Also known as + Cursor ## Intent -Provide a way to access the elements of an aggregate object -sequentially without exposing its underlying representation. +Provide a way to access the elements of an aggregate object sequentially without exposing its +underlying representation. ## Explanation Real world example -> Treasure chest contains a set of magical items. There multiple types of items such as rings, potions and weapons. The items can be browsed by type using an iterator the treasure chest provides. +> Treasure chest contains a set of magical items. There multiple types of items such as rings, +> potions and weapons. The items can be browsed by type using an iterator the treasure chest +> provides. In plain words -> Containers can provide a representation agnostic iterator interface to provide access to the elements. +> Containers can provide a representation agnostic iterator interface to provide access to the +> elements. Wikipedia says -> In object-oriented programming, the iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements. +> In object-oriented programming, the iterator pattern is a design pattern in which an iterator is +> used to traverse a container and access the container's elements. **Programmatic Example** -The main class in our example is the treasure chest that contains items. +The main class in our example is the `TreasureChest` that contains items. ```java public class TreasureChest { @@ -60,7 +65,11 @@ public class TreasureChest { return new ArrayList<>(items); } } +``` +Here's the `Item` class: + +```java public class Item { private ItemType type; @@ -92,7 +101,7 @@ public enum ItemType { } ``` -The iterator interface is extremely simple. +The `Iterator` interface is extremely simple. ```java public interface Iterator { @@ -110,19 +119,26 @@ var itemIterator = TREASURE_CHEST.iterator(ItemType.RING); while (itemIterator.hasNext()) { LOGGER.info(itemIterator.next().toString()); } -// Ring of shadows -// Ring of armor +``` + +Program output: + +```java +Ring of shadows +Ring of armor ``` ## Class diagram + ![alt text](./etc/iterator_1.png "Iterator") ## Applicability + Use the Iterator pattern -* to access an aggregate object's contents without exposing its internal representation -* to support multiple traversals of aggregate objects -* to provide a uniform interface for traversing different aggregate structures +* To access an aggregate object's contents without exposing its internal representation. +* To support multiple traversals of aggregate objects. +* To provide a uniform interface for traversing different aggregate structures. ## Real world examples diff --git a/iterator/src/main/java/com/iluwatar/iterator/App.java b/iterator/src/main/java/com/iluwatar/iterator/App.java index b81e765be..053b5c4fb 100644 --- a/iterator/src/main/java/com/iluwatar/iterator/App.java +++ b/iterator/src/main/java/com/iluwatar/iterator/App.java @@ -62,7 +62,7 @@ public class App { LOGGER.info("------------------------"); LOGGER.info("BST Iterator: "); var root = buildIntegerBst(); - var bstIterator = new BstIterator(root); + var bstIterator = new BstIterator<>(root); while (bstIterator.hasNext()) { LOGGER.info("Next node: " + bstIterator.next().getVal()); } diff --git a/iterator/src/main/java/com/iluwatar/iterator/bst/README.md b/iterator/src/main/java/com/iluwatar/iterator/bst/README.md index 816d1a4fe..07bcbf84f 100644 --- a/iterator/src/main/java/com/iluwatar/iterator/bst/README.md +++ b/iterator/src/main/java/com/iluwatar/iterator/bst/README.md @@ -1,8 +1,11 @@ # BSTIterator -An implementation of the Iterator design pattern, for the Binary Search Tree -data structure. A great explanation of BSTs can be found in this [video tutorial](https://www.youtube.com/watch?v=i_Q0v_Ct5lY). -### What it Does +An implementation of the Iterator design pattern, for the Binary Search Tree data structure. A great +explanation of BSTs can be found in this +[video tutorial](https://www.youtube.com/watch?v=i_Q0v_Ct5lY). + +### What It Does + This iterator assumes that the given binary search tree inserts nodes of smaller value to the left, and nodes of larger value to the right of current node. Accordingly, this iterator will return nodes according to "In Order" binary tree traversal. @@ -12,6 +15,7 @@ return values in order: 1, 3, 4, 6, 7, 8, 10, 13, 14. ![BST](../../../../../../../etc/bst.jpg "Binary Search Tree") ### How It's Done + **The trivial solution** to a binary search tree iterator would be to construct a List (or similar linear data structure) when you construct the BSTIterator. This would require traversing the entire BST, adding each node value to your list as you go. The downside to the trivial solution is twofold. @@ -80,7 +84,4 @@ In Big O terms, here are the costs for our improved solution, where h is the hei * Extra Space: O(h) As you can see, this solution more evenly distributes the work. It yields the same amortized -runtime for `next()`, reduces the run time of the constructor, and uses less extra space. - - - \ No newline at end of file +runtime for `next()`, reduces the run time of the constructor, and uses less extra space.