Update README.md
This commit is contained in:
parent
6606d6cd08
commit
bcca9beb4d
@ -9,29 +9,34 @@ tags:
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Also known as
|
## Also known as
|
||||||
|
|
||||||
Cursor
|
Cursor
|
||||||
|
|
||||||
## Intent
|
## Intent
|
||||||
Provide a way to access the elements of an aggregate object
|
Provide a way to access the elements of an aggregate object sequentially without exposing its
|
||||||
sequentially without exposing its underlying representation.
|
underlying representation.
|
||||||
|
|
||||||
## Explanation
|
## Explanation
|
||||||
|
|
||||||
Real world example
|
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
|
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
|
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**
|
**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
|
```java
|
||||||
public class TreasureChest {
|
public class TreasureChest {
|
||||||
@ -60,7 +65,11 @@ public class TreasureChest {
|
|||||||
return new ArrayList<>(items);
|
return new ArrayList<>(items);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Here's the `Item` class:
|
||||||
|
|
||||||
|
```java
|
||||||
public class Item {
|
public class Item {
|
||||||
|
|
||||||
private ItemType type;
|
private ItemType type;
|
||||||
@ -92,7 +101,7 @@ public enum ItemType {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
The iterator interface is extremely simple.
|
The `Iterator` interface is extremely simple.
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public interface Iterator<T> {
|
public interface Iterator<T> {
|
||||||
@ -110,19 +119,26 @@ var itemIterator = TREASURE_CHEST.iterator(ItemType.RING);
|
|||||||
while (itemIterator.hasNext()) {
|
while (itemIterator.hasNext()) {
|
||||||
LOGGER.info(itemIterator.next().toString());
|
LOGGER.info(itemIterator.next().toString());
|
||||||
}
|
}
|
||||||
// Ring of shadows
|
```
|
||||||
// Ring of armor
|
|
||||||
|
Program output:
|
||||||
|
|
||||||
|
```java
|
||||||
|
Ring of shadows
|
||||||
|
Ring of armor
|
||||||
```
|
```
|
||||||
|
|
||||||
## Class diagram
|
## Class diagram
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
## Applicability
|
## Applicability
|
||||||
|
|
||||||
Use the Iterator pattern
|
Use the Iterator pattern
|
||||||
|
|
||||||
* to access an aggregate object's contents without exposing its internal representation
|
* To access an aggregate object's contents without exposing its internal representation.
|
||||||
* to support multiple traversals of aggregate objects
|
* To support multiple traversals of aggregate objects.
|
||||||
* to provide a uniform interface for traversing different aggregate structures
|
* To provide a uniform interface for traversing different aggregate structures.
|
||||||
|
|
||||||
## Real world examples
|
## Real world examples
|
||||||
|
|
||||||
|
@ -62,7 +62,7 @@ public class App {
|
|||||||
LOGGER.info("------------------------");
|
LOGGER.info("------------------------");
|
||||||
LOGGER.info("BST Iterator: ");
|
LOGGER.info("BST Iterator: ");
|
||||||
var root = buildIntegerBst();
|
var root = buildIntegerBst();
|
||||||
var bstIterator = new BstIterator<Integer>(root);
|
var bstIterator = new BstIterator<>(root);
|
||||||
while (bstIterator.hasNext()) {
|
while (bstIterator.hasNext()) {
|
||||||
LOGGER.info("Next node: " + bstIterator.next().getVal());
|
LOGGER.info("Next node: " + bstIterator.next().getVal());
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
# BSTIterator
|
# 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
|
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,
|
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.
|
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
|
### How It's Done
|
||||||
|
|
||||||
**The trivial solution** to a binary search tree iterator would be to construct a List (or similar
|
**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
|
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.
|
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)
|
* Extra Space: O(h)
|
||||||
|
|
||||||
As you can see, this solution more evenly distributes the work. It yields the same amortized
|
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.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user