Vous êtes sur la page 1sur 6

Links The linked list is a versatile mechanism suitable for use in many kinds of generalpurpose databases.

It can also replace an array as the basis for other storage structures such as stacks and queues. In fact, you can use a linked list in many cases in which you use an array, unless you need frequent random access to individual items using an index. Linked lists arent the solution to all data storage problems, but they are surprisingly versatile and conceptually simpler than some other popular structures such as trees. linked list is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of a datum and a reference (in other words, a link) to the next node in the sequence; more complex variants add additional links. This structure allows for efficient insertion or removal of elements from any position in the sequence.

A linked list whose nodes contain two fields: an integer value and a link to the next node. The last node is linked to a terminator used to signify the end of the list. In a linked list, each data item is embedded in a link. A link is an object of a class called something like Link. Because there are many similar links in a list, it makes sense to use a separate class for them, distinct from the linked list itself. Each Link object contains a reference (usually called next) to the next link in the list. A field in the list itself contains a reference to the first link. Types of Linked List Singly, doubly, and multiply linked lists Singly linked lists contain nodes which have a data field as well as a next field, which points to the next node in the linked list.

A singly linked list whose nodes contain two fields: an integer value and a link to the next node In a doubly linked list, each node contains, besides the next-node link, a second link field pointing to the previous node in the sequence. The two links may be called forward(s) and backwards, or next and prev(ious).

A doubly linked list whose nodes contain three fields: an integer value, the link forward to the next node, and the link backward to the previous node

Trees Why might you want to use a tree? Usually, because it combines the advantages of two other structures: an ordered array and a linked list. You can search a tree quickly, as you can an ordered array, and you can also insert and delete items quickly, as you can with a linked list. Well be mostly interested in a particular kind of tree called a binary tree, but lets start by discussing trees in general before moving on to the specifics of binary trees. A tree consists of nodes connected by edges. Figure 8.1 shows a tree. In such a picture of a tree (or in our Workshop applet) the nodes are represented as circles, and the edges as lines connecting the circles.

The lines (edges) between the nodes represent the way the nodes are related. Roughly speaking, the lines represent convenience: Its easy (and fast) for a program to get from one node to another if there is a line connecting them. In fact, the only way to get from node to node is to follow a path along the lines. Generally, you are restricted to going in one direction along edges: from the root downward. Typically, there is one node in the top row of a tree, with lines connecting to more nodes on the second row, even more on the third, and so on. Thus, trees are small on the top and large on the bottom. This may seem upside-down compared with real trees, but generally a program starts an operation at the small end of the tree, and its (arguably) more natural to think about going from top to bottom, as in reading text. There are different kinds of trees. The tree shown in Figure 8.1 has more than two children per node. (Well see what children means in a moment.) However, in this chapter well be discussing a specialized form of tree called a binary tree. Each node in a binary tree has a

maximum of two children. More general trees, in which nodes can have more than two children, are called multiway trees.

Path Think of someone walking from node to node along the edges that connect them. The resulting sequence of nodes is called a path. Root The node at the top of the tree is called the root. There is only one root in a tree. For a collection of nodes and edges to be defined as a tree, there must be one (and only one!) path from the root to any other node. Figure 8.3 shows a non-tree. You can see that it violates this rule. Parent Any node (except the root) has exactly one edge running upward to another node. The node above it is called the parent of the node. Child Any node may have one or more lines running downward to other nodes. These nodes below a given node are called its children. Leaf A node that has no children is called a leaf node or simply a leaf. There can be only one root in a tree, but there can be many leaves. Subtree Any node may be considered to be the root of a subtree, which consists of its children, and its childrens children, and so on. If you think in terms of families, a nodes subtree contains all its descendants. Visiting A node is visited when program control arrives at the node, usually for the purpose of carrying out some operation on the node, such as checking the value of one of its data fields or displaying it. Merely passing over a node on the path from one node to another is not considered to be visiting the node. Traversing To traverse a tree means to visit all the nodes in some specified order. For example, you might visit all the nodes in order of ascending key value. There are other ways to traverse a tree, as well see later. Levels The level of a particular node refers to how many generations the node is from the root. If we assume the root is Level 0, then its children will be Level 1, its grandchildren will be Level 2, and so on. Keys Weve seen that one data field in an object is usually designated a key value. This value is used to search for the item or perform other operations on it. In tree diagrams, when a circle represents a node holding a data item, the key value of the item is typically shown in the circle. (Well see many figures later on that show nodes containing keys.)

Binary Trees If every node in a tree can have at most two children, the tree is called a binary tree. The two children of each node in a binary tree are called the left child and the right child, corresponding to their positions when you draw a picture of a tree, as shown in Figure 8.2. A node in a binary tree doesnt necessarily have the maximum of two children; it may have only a left child, or only a right child, or it can have no children at all (in which case its a leaf).

Finding Maximum and Minimum Values Incidentally, we should note how easy it is to find the maximum and minimum values in a binary search tree. In fact, this process is so easy we dont include it as an option in the Workshop applet, nor show code for it in Listing 8.1. Still, understanding how it works is important. For the minimum, go to the left child of the root; then go to the left child of that child, and so on, until you come to a node that has no left child. This node is the minimum, as shown in Figure 8.12.

Heaps
a heap is a specialized tree-based data structure that satisfies the heap property: if B is a child node of A, then key(A) key(B). This implies that an element with the greatest key is always in the root node, and so such a heap is sometimes called a max-heap.

A heap is a binary tree with these characteristics:

Its complete. This means its completely filled in, reading from left to right across each row, although the last row need not be full. Figure 12.1 shows complete and incomplete trees. Its (usually) implemented as an array.
Each node in a heap satisfies the heap condition, which states that every nodes key is larger than (or equal to) the keys of its children

Vous aimerez peut-être aussi