Skip to content

Commit bcca9be

Browse files
committed
Update README.md
1 parent 6606d6c commit bcca9be

File tree

3 files changed

+37
-20
lines changed

3 files changed

+37
-20
lines changed

iterator/README.md

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,34 @@ tags:
99
---
1010

1111
## Also known as
12+
1213
Cursor
1314

1415
## Intent
15-
Provide a way to access the elements of an aggregate object
16-
sequentially without exposing its underlying representation.
16+
Provide a way to access the elements of an aggregate object sequentially without exposing its
17+
underlying representation.
1718

1819
## Explanation
1920

2021
Real world example
2122

22-
> 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.
23+
> Treasure chest contains a set of magical items. There multiple types of items such as rings,
24+
> potions and weapons. The items can be browsed by type using an iterator the treasure chest
25+
> provides.
2326
2427
In plain words
2528

26-
> Containers can provide a representation agnostic iterator interface to provide access to the elements.
29+
> Containers can provide a representation agnostic iterator interface to provide access to the
30+
> elements.
2731
2832
Wikipedia says
2933

30-
> 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.
34+
> In object-oriented programming, the iterator pattern is a design pattern in which an iterator is
35+
> used to traverse a container and access the container's elements.
3136
3237
**Programmatic Example**
3338

34-
The main class in our example is the treasure chest that contains items.
39+
The main class in our example is the `TreasureChest` that contains items.
3540

3641
```java
3742
public class TreasureChest {
@@ -60,7 +65,11 @@ public class TreasureChest {
6065
return new ArrayList<>(items);
6166
}
6267
}
68+
```
69+
70+
Here's the `Item` class:
6371

72+
```java
6473
public class Item {
6574

6675
private ItemType type;
@@ -92,7 +101,7 @@ public enum ItemType {
92101
}
93102
```
94103

95-
The iterator interface is extremely simple.
104+
The `Iterator` interface is extremely simple.
96105

97106
```java
98107
public interface Iterator<T> {
@@ -110,19 +119,26 @@ var itemIterator = TREASURE_CHEST.iterator(ItemType.RING);
110119
while (itemIterator.hasNext()) {
111120
LOGGER.info(itemIterator.next().toString());
112121
}
113-
// Ring of shadows
114-
// Ring of armor
122+
```
123+
124+
Program output:
125+
126+
```java
127+
Ring of shadows
128+
Ring of armor
115129
```
116130

117131
## Class diagram
132+
118133
![alt text](./etc/iterator_1.png "Iterator")
119134

120135
## Applicability
136+
121137
Use the Iterator pattern
122138

123-
* to access an aggregate object's contents without exposing its internal representation
124-
* to support multiple traversals of aggregate objects
125-
* to provide a uniform interface for traversing different aggregate structures
139+
* To access an aggregate object's contents without exposing its internal representation.
140+
* To support multiple traversals of aggregate objects.
141+
* To provide a uniform interface for traversing different aggregate structures.
126142

127143
## Real world examples
128144

iterator/src/main/java/com/iluwatar/iterator/App.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private static void demonstrateBstIterator() {
6262
LOGGER.info("------------------------");
6363
LOGGER.info("BST Iterator: ");
6464
var root = buildIntegerBst();
65-
var bstIterator = new BstIterator<Integer>(root);
65+
var bstIterator = new BstIterator<>(root);
6666
while (bstIterator.hasNext()) {
6767
LOGGER.info("Next node: " + bstIterator.next().getVal());
6868
}

iterator/src/main/java/com/iluwatar/iterator/bst/README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# BSTIterator
2-
An implementation of the Iterator design pattern, for the Binary Search Tree
3-
data structure. A great explanation of BSTs can be found in this [video tutorial](https://www.youtube.com/watch?v=i_Q0v_Ct5lY).
42

5-
### What it Does
3+
An implementation of the Iterator design pattern, for the Binary Search Tree data structure. A great
4+
explanation of BSTs can be found in this
5+
[video tutorial](https://www.youtube.com/watch?v=i_Q0v_Ct5lY).
6+
7+
### What It Does
8+
69
This iterator assumes that the given binary search tree inserts nodes of smaller
710
value to the left, and nodes of larger value to the right of current node. Accordingly,
811
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.
1215
![BST](../../../../../../../etc/bst.jpg "Binary Search Tree")
1316

1417
### How It's Done
18+
1519
**The trivial solution** to a binary search tree iterator would be to construct a List (or similar
1620
linear data structure) when you construct the BSTIterator. This would require traversing the entire
1721
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
8084
* Extra Space: O(h)
8185

8286
As you can see, this solution more evenly distributes the work. It yields the same amortized
83-
runtime for `next()`, reduces the run time of the constructor, and uses less extra space.
84-
85-
86-
87+
runtime for `next()`, reduces the run time of the constructor, and uses less extra space.

0 commit comments

Comments
 (0)