cs180 recitation - web.ics.purdue.educs180/fall2009web/recitation_slides/... · circular queue •...

21
CS180 Recitation Week 10: Abstract Data Types

Upload: hoangkiet

Post on 27-Apr-2019

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS180 Recitation - web.ics.purdue.educs180/Fall2009Web/recitation_slides/... · Circular Queue • A variant of the single ended queue • The tail points to the first node, not NULL

CS180 Recitation

Week 10: Abstract Data Types

Page 2: CS180 Recitation - web.ics.purdue.educs180/Fall2009Web/recitation_slides/... · Circular Queue • A variant of the single ended queue • The tail points to the first node, not NULL

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

Page 3: CS180 Recitation - web.ics.purdue.educs180/Fall2009Web/recitation_slides/... · Circular Queue • A variant of the single ended queue • The tail points to the first node, not NULL

Data structures discussed in lectures

• Linked List

• Stack

• Queue

• Doubly Linked List• Doubly Linked List

• Double Ended Queue

Page 4: CS180 Recitation - web.ics.purdue.educs180/Fall2009Web/recitation_slides/... · Circular Queue • A variant of the single ended queue • The tail points to the first node, not NULL

More ADTs

• Double Ended Queue

• Circular Queue

• Tree

Page 5: CS180 Recitation - web.ics.purdue.educs180/Fall2009Web/recitation_slides/... · Circular Queue • A variant of the single ended queue • The tail points to the first node, not NULL

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.

Page 6: CS180 Recitation - web.ics.purdue.educs180/Fall2009Web/recitation_slides/... · Circular Queue • A variant of the single ended queue • The tail points to the first node, not NULL

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.

Page 7: CS180 Recitation - web.ics.purdue.educs180/Fall2009Web/recitation_slides/... · Circular Queue • A variant of the single ended queue • The tail points to the first node, not NULL

Doubly Linked List

next

next

prev

content

head

next

prev

content

prev

contentnull

null

tail

Page 8: CS180 Recitation - web.ics.purdue.educs180/Fall2009Web/recitation_slides/... · Circular Queue • A variant of the single ended queue • The tail points to the first node, not NULL

Double Ended QueueaddToHead() deleteFromHead()

addToTail() deleteFromTail()

next

prev

content

null null

head tail

next

prev

content

next

prev

content

next

prev

content

Page 9: CS180 Recitation - web.ics.purdue.educs180/Fall2009Web/recitation_slides/... · Circular Queue • A variant of the single ended queue • The tail points to the first node, not NULL

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?

Page 10: CS180 Recitation - web.ics.purdue.educs180/Fall2009Web/recitation_slides/... · Circular Queue • A variant of the single ended queue • The tail points to the first node, not NULL

Circular Queue

• A variant of the single ended queue

• The tail points to the first node, not NULL.

Page 11: CS180 Recitation - web.ics.purdue.educs180/Fall2009Web/recitation_slides/... · 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

Page 12: CS180 Recitation - web.ics.purdue.educs180/Fall2009Web/recitation_slides/... · Circular Queue • A variant of the single ended queue • The tail points to the first node, not NULL

Circular Queue

next

contentnext

content

next

content next

content

head

next

content

next

content

next

content

next

content

tail

Page 13: CS180 Recitation - web.ics.purdue.educs180/Fall2009Web/recitation_slides/... · Circular Queue • A variant of the single ended queue • The tail points to the first node, not NULL

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;

}

}

Page 14: CS180 Recitation - web.ics.purdue.educs180/Fall2009Web/recitation_slides/... · Circular Queue • A variant of the single ended queue • The tail points to the first node, not NULL

Another Circular Queue

Implementation

• The head is redundant

• Why?

– head is always pointed by tail!

Page 15: CS180 Recitation - web.ics.purdue.educs180/Fall2009Web/recitation_slides/... · Circular Queue • A variant of the single ended queue • The tail points to the first node, not NULL

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

Page 16: CS180 Recitation - web.ics.purdue.educs180/Fall2009Web/recitation_slides/... · Circular Queue • A variant of the single ended queue • The tail points to the first node, not NULL

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.

Page 17: CS180 Recitation - web.ics.purdue.educs180/Fall2009Web/recitation_slides/... · Circular Queue • A variant of the single ended queue • The tail points to the first node, not NULL

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

Page 18: CS180 Recitation - web.ics.purdue.educs180/Fall2009Web/recitation_slides/... · Circular Queue • A variant of the single ended queue • The tail points to the first node, not NULL

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

Page 19: CS180 Recitation - web.ics.purdue.educs180/Fall2009Web/recitation_slides/... · Circular Queue • A variant of the single ended queue • The tail points to the first node, not NULL

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 );

}

Page 20: CS180 Recitation - web.ics.purdue.educs180/Fall2009Web/recitation_slides/... · Circular Queue • A variant of the single ended queue • The tail points to the first node, not NULL

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();

}

}

Page 21: CS180 Recitation - web.ics.purdue.educs180/Fall2009Web/recitation_slides/... · Circular Queue • A variant of the single ended queue • The tail points to the first node, not NULL

QUIZ

• How to create a circular linked list such that

we can traverse in two directions?