Java Reference
In-Depth Information
22
// remove objects from queue
23
try
24
{
25
int removedItem;
26
27
while ( true )
28
{
29
removedItem = queue.dequeue(); // use dequeue method
30
System.out.printf( "%n%d dequeued%n" , removedItem);
31
queue.print();
32
}
33
}
34
catch (EmptyListException emptyListException)
35
{
36
emptyListException.printStackTrace();
37
}
38
}
39
} // end class QueueTest
The queue is: -1
The queue is: -1 0
The queue is: -1 0 1
The queue is: -1 0 1 5
-1 dequeued
The queue is: 0 1 5
0 dequeued
The queue is: 1 5
1 dequeued
The queue is: 5
5 dequeued
Empty queue
com.deitel.datastructures.EmptyListException: queue is empty
at com.deitel.datastructures.List.removeFromFront(List.java:81)
at com.deitel.datastructures.Queue.dequeue(Queue.java:24)
at QueueTest.main(QueueTest.java:29)
Fig. 21.14 | Queue processing program. (Part 2 of 2.)
21.7 Trees
Lists, stacks and queues are linear data structures (i.e., sequences ). A tree is a nonlinear, two-
dimensional data structure with special properties. Tree nodes contain two or more links.
This section discusses binary trees (Fig. 21.15)—trees whose nodes each contain two links
(one or both of which may be null ). The root node is the first node in a tree. Each link in
the root node refers to a child . The left child is the first node in the left subtree (also known
as the root node of the left subtree), and the right child is the first node in the right subtree
(also known as the root node of the right subtree) . The children of a specific node are called
siblings . A node with no children is called a leaf node . Computer scientists normally draw
trees from the root node down—the opposite of the way most trees grow in nature.
 
 
Search WWH ::




Custom Search