cs180 recitation - web.ics.purdue.educs180/fall2009web/recitation_slides/... · circular queue •...
TRANSCRIPT
CS180 Recitation
Week 10: Abstract Data Types
Announcement
• Project 4 is due next Wednesday at 9pm.
• Project 5 will be posted on November 5.
– Milestone is due on Thursday (Nov. 12)
– Two week project– Two week project
– Final submission on Nov. 18
Data structures discussed in lectures
• Linked List
• Stack
• Queue
• Doubly Linked List• Doubly Linked List
• Double Ended Queue
More ADTs
• Double Ended Queue
• Circular Queue
• Tree
Abstract Data Types
• ADTs can be implemented using different
basic data structures
– Dynamic arrays
– Linked List– Linked List
• The external behavior of an ADT remains the
same regardless of the internal
representation.
Double Ended Queue
• Use doubly linked list for internal
representation
• Similar to doubly linked list
– Doubly linked list– Doubly linked list
• Can access arbitrary nodes
– Double Ended Queue
• Insert or remove nodes at either head or tail only.
Doubly Linked List
next
next
prev
content
head
next
prev
content
prev
contentnull
null
tail
Double Ended QueueaddToHead() deleteFromHead()
addToTail() deleteFromTail()
next
prev
content
null null
head tail
next
prev
content
next
prev
content
next
prev
content
Order Matters
class DoubleQ{
private Node2 head, tail;
public void addToHead(Object c){
Node2 n = new Node2(c);
n.setPrev(null);n.setPrev(null);
n.setNext(head);
head.setPrev(n);
head = n;
}
}
What would happen if we
exchange these two lines?
Circular Queue
• A variant of the single ended queue
• The tail points to the first node, not NULL.
Circular Linked List
next
next
content
head
next
content
next
contenttail
Circular Queue
next
contentnext
content
next
content next
content
head
next
content
next
content
next
content
next
content
tail
Circular Queue Implementation
Class CircularQueue{
private CircularNode head,tail;
public CircularQueue{
head = tail = null;
}}
public addHead(int value){
CircularNode n = new CircularNode(value);
n.setNext( head );
tail.setNext( n );
head = n;
}
}
Another Circular Queue
Implementation
• The head is redundant
• Why?
– head is always pointed by tail!
Another Circular Queue
ImplementationClass CircularQueue{
private CircularNode tail;
public CircularQueue{
tail = null;
}
public addHead(int value){
tail is sufficient
public addHead(int value){
CircularNode n = new CircularNode(value);
if( tail == null )
n.setNext( n );
else{
n.setNext( tail.next );
tail.setNext( n );
}
}
}
tail.next = head
Tree
• A hierarchical structure.
• Each element, or “node”, has several links to
other nodes.
– A node can have 3,4,5 or even more links– A node can have 3,4,5 or even more links
– Commonly a node has two links
• Binary Tree
• Each node can be linked by at most one node.
Binary Tree
left
right
value
TreeNode
left
TreeNode
left
TreeNode
root
valueleft
right
value
left
right
value
TreeNode
left
right
value
TreeNode
left
right
value
TreeNode
left
right
value
TreeNode
left
right
value
public class TreeNode {
String value ;
TreeNode left = null , right = null ;
TreeNode ( String value ) {
this.value = value ;
}
void print() {Print the left subtree first
void print() {
if ( left != null )
left.print();
System.out.format ("%s\n", value );
if ( right != null )
right.print();
}
}
Print the left subtree first
Finally, print the right subtree.
Print the value of this node
void insert ( String value ) {
if ( value.compareTo(this.value ) <= 0)
if ( left == null )
left = new TreeNode( value );
elseelse
left.insert ( value );
else
if ( right == null )
right = new TreeNode ( value );
else
right.insert ( value );
}
Store value to a sorted binary treeclass ReadAndSortStrings {
public static void main ( String [] args ) {
TreeNode root = null ;
Scanner in = new Scanner ( System.in);
while (in.hasNextLine ())
if ( root == null )
root = new TreeNode (in.nextLine ());
else
root.insert (in.nextLine());
if ( root == null )
System.out.format (" tree is empty \n");
else
root.print();
}
}
QUIZ
• How to create a circular linked list such that
we can traverse in two directions?