Operation | Array | List |
---|---|---|
size, isEmpty | O(1) | O(1) |
atRank, rankOf, elemAtRank | O(1) | O(n) |
first, last, before, after | O(1) | O(1) |
replaceElement, swapElements | O(1) | O(1) |
replaceAtRank | O(1) | O(n) |
insertAtRank, removeAtRank | O(n) | O(n) |
insertFirst, insertLast | O(1) | O(1) |
insertAfter, insertBefore | O(n) | O(1) |
remove | O(n) | O(1) |
Define a sequence ADT that includes all the methods of both vector and list ADTs as well as
Sequences can be implemented as either circular arrays, as we did
for vectors, or doubly linked lists, as we did for lists. Neither
clearly dominates the other. Instead it depends on the relative
frequency of the various operations. Circular arrays are faster for
some and doubly liked lists are faster for others as the table to the
right illustrates.
An ADT for looping through a sequence one element at a time. It has two methods.
When you create the iterator it has all the elements of the sequence. So a typical usage pattern would be
create iterator I for sequence S while I.hasNext process I.nextObject
The tree ADT stores elements hierarchically. There is a distinguished root node. All other nodes have a parent of which they are a child. We use nodes and positions interchangeably for trees.
The definition above precludes an empty tree. This is a matter of taste some authors permit empty trees, others do not.
Some more definitions.
We order the children of a binary tree so that the left child comes before the right child.
There are many examples of trees. You learned or will learn tree-structured file systems in 202. However, despite what the book says, for Unix/Linux at least, the file system does not form a tree (due to hard and symbolic links).
These notes can be thought of as a tree with nodes corresponding to the chapters, sections, subsections, etc.
Games like chess are analyzed in terms of trees. The root is the current position. For each node its children are the positions resulting from the possible moves. Chess playing programs often limit the depth so that the number of examined moves is not too large.
The leaves are constants or variables and the internal nodes are binary arithmetic operations (+,-,*,/). The tree is a proper ordered binary tree (since we are considering binary operators). The value of a leaf is the value of the constant or variable. The value of an internal node is obtained by applying the operator to the values of the children (in order).
Evaluate an arithmetic expression tree on the board.
Homework: R-2.2, but made easier by replacing 21 by 10. If you wish you can do the problem in the book instead (I think it is harder).
We have three accessor methods (i.e., methods that permit us to access the nodes of the tree.
We have four query methods that test status.
Finally generic methods that are useful but not related to the tree structure.