<｜begin▁of▁sentence｜>### Instruction:
Given a graph whose edge set is [('A', 'B', 1), ('B', 'C', 2), ('A', 'C', 3), ('C', 'D', 4), ('B', 'D', 5)], can you use NetworkX to iterate over all spanning trees of the graph in both increasing and decreasing order of cost?
### Response:
import networkx as nx

# Create a weighted undirected graph
G = nx.Graph()
G.add_weighted_edges_from(
    [('A', 'B', 1), ('B', 'C', 2), ('A', 'C', 3), ('C', 'D', 4), ('B', 'D', 5)]
)

# Iterate over all spanning trees in increasing cost
for tree in nx.algorithms.tree.mst.spanning_tree_iterator(G, weight='weight', minimum=True):
    print(tree.edges(data=True))

# Iterate over all spanning trees in decreasing cost
for tree in nx.algorithms.tree.mst.spanning_tree_iterator(G, weight='weight', minimum=False):
    print(tree.edges(data=True))
<|EOT|>
