380 Creating Trees A related problem, dubbed the Traveling Salesman problem, takes this one step further by finding the shortest round-trip route through a graph where the same node is both the starting and ending point. In this case, the shortest route through a graph may not necessarily be the shortest round-trip route to return to the same starting point. Both the Traveling Salesman and the Chinese Postman problem can get more complicated with a weighted or directed graph. A directed graph may restrict movement in one direction, such as traveling through one-way streets in a city, whereas a weighted graph can make one route longer than two shorter routes combined. Adding in directed and weighted graphs can alter the best solution. If you ever looked up directions on a map Web site such as MapQuest, you’ve used a graph to find the most efficient way from one location to another. Connecting nodes in a graph Another use for graphs involves topological graph theory. This problem is highlighted by the Three Cottage problem with three cottages needing to connect to the gas, water, and electricity companies, but their lines can’t cross each other. (It’s impossible, by the way.) Connecting lines in a graph without crossing is a problem that circuit board designers face in the placement of chips. Another example of eliminating intersections involves transportation designs, such as the design of highways or railroad tracks. Creating Trees Graphs typically represent a chaotic arrangement of data with little or no structure. To give graphs some form of organization, computer scientists have created special graphs dubbed trees. Like a graph, a tree consists of nodes and edges, but unlike a graph, a tree organizes data in a hierarchy, as shown in Figure 5-5. A tree arranges a graph in a hierarchy with a single node appearing at the top (or the root node) and additional nodes appearing connected underneath (or internal nodes). If a node has no additional nodes connected underneath, those nodes are leaf nodes, as shown in Figure 5-6.
Creating Trees 381 Figure 5-5: A tree is a hierarchical graph. A graph A tree Root node Figure 5-6: A tree consists of one root node and multiple leaf and internal nodes. A tree Internal node Leaf node Ordered trees A tree can store information at random in its different nodes, which is dubbed an unordered tree. However, the tree is already in the form of a hierarchy, so it only makes sense to take advantage of this built-in structure and create an ordered tree. Book III Chapter 5 Graphs and Trees An ordered tree provides a distinct beginning node (the root node) with additional nodes organized in a hierarchy. Such a hierarchy can store and show relationships of a corporate management team or the spread of a flu epidemic through different cities. As a result, ordered trees are a common data structure used to both model and organize data. One common use for ordered trees involves storing data. Under each root node, you can have 26 internal nodes that each represent a single letter of the alphabet from A to Z. Under each of these letter nodes, you can have multiple nodes that contain the actual data, as shown in Figure 5-7.