UMBC CMSC202, Computer Science II, Spring 1998,
Sections 0101, 0102, 0103, 0104 and Honors
Thursday April 16, 1998
Assigned Reading:
- A Book on C: 10.8 - 10.9
- Programming Abstractions in C: 13.1 - 13.2
Handouts (available on-line):
Trees (optional)
Topics Covered:
- Queues: A queue is a First-In-First-Out data structure
(imagine a line of customers waiting for service at a bank). When you use
a queue, new items are inserted at the end of the queue and old items
are removed from the front of the queue. As with a stack, you are not
allowed to insert or remove items in the middle of the queue (that would
be cutting in line!).
- Again, we can derive the queue data structure from our generic
linked list ADT. See header file and
implementation of the queue ADT.
-
The operations we support in the queue ADT are:
- Enqueue() adds an item at the end of the queue.
- Dequeue() removes an item from the front of the queue.
- Front() returns the item at the front of the queue
without deleting it from the queue.
- IsEmpty() determines whether the queue is empty.
- Length() returns the number of items in the queue.
- Print() prints out the contents of the queue.
- The way that Print() was implemented is noteworthy. In
a private derivation, the public member functions of the base class are
usually hidden. We can expose a public member of the base class, by
including it in the public section of the derived class. For example,
to expose the Print() function from the List base class, we
include the following declaration in the public section of the Queue
class:
List::Print ;
Note that there is no return type and that parentheses do not appear after the
symbol Print. Compare this method with the implementation of
Print() in the Stack class.
- For a program that
uses a queue, we again use the "To Do" list example. This time
jobs that arrive first are finished first. See
sample run.
- The next topic is trees. We discussed terminology of general trees as
well as binary trees. The definitions appear in your textbooks, and in the
optional on-line reading on
trees. We considered the UNIX file system as an example of a tree.
The terminology for trees differ from textbook to textbook. You should
become familiar with the terminology rather than attempt to memorize the
exact terminology. One important thing to note is that in a full binary
tree, the number of nodes at level i is 2i --- i.e., the number
of nodes increases exponentially. Also, a full binary tree of height i has
2i+1-1 nodes and 2i (or roughly half) of these are
leaves. Thus, full binary trees are very much bottom heavy. This
becomes important when we discuss binary search trees.
- There are three standard methods for visiting every node in a binary
tree: inorder traversal, preorder traversal and postorder traversal.
(Actually, there are more than three ways to visit every node, but these
are the three we will discuss for now.) These traversals are defined
recursively:
- Inorder traversal: recursively use inorder traversal on
the left subtree, visit the current node, and recursively use
inorder traversal on the right subtree.
- Preorder traversal: First visit the current node. Then,
recursively use preorder traversal on the left subtree and the right
subtree (in that order).
- Postorder traversal: First, recursively use postorder traversal
on the left and right subtrees. Finally, visit the current node.
- We use a very simple implementation of a Tree class to demonstrate the
traversals. See the header file and implementation for the Tree class. As we did
for the List class, the Tree class uses an item.h file to define the data type
stored in each node of the tree. In this case, we use string data defined
in the stringitem.h header
file (q.v. the implementation
file). The main program builds a
management hierarchy for a fictitious company using the Tree class. The
sample run shows the inorder, preorder
and postorder traversals of the tree.
Last Modified:
22 Jul 2024 11:27:44 EDT
by
Richard Chang
Back up
to Spring 1998 CMSC 202 Section Homepage