Update README.md
This commit is contained in:
		| @@ -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<T> { | ||||
| @@ -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 | ||||
|  | ||||
|  | ||||
|  | ||||
| ## 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 | ||||
|  | ||||
|   | ||||
| @@ -62,7 +62,7 @@ public class App { | ||||
|     LOGGER.info("------------------------"); | ||||
|     LOGGER.info("BST Iterator: "); | ||||
|     var root = buildIntegerBst(); | ||||
|     var bstIterator = new BstIterator<Integer>(root); | ||||
|     var bstIterator = new BstIterator<>(root); | ||||
|     while (bstIterator.hasNext()) { | ||||
|       LOGGER.info("Next node: " + bstIterator.next().getVal()); | ||||
|     } | ||||
|   | ||||
| @@ -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. | ||||
|    | ||||
|  | ||||
| ### 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.  | ||||
|  | ||||
|  | ||||
|   | ||||
| runtime for `next()`, reduces the run time of the constructor, and uses less extra space. | ||||
|   | ||||
		Reference in New Issue
	
	Block a user