COMP 110 Introduction to Programming Mr. Joshua Stough

Preview:

Citation preview

COMP 110 Introduction to Programming

Mr. Joshua Stough

What are Data Structures?• Abstract data types

– data that is “more” (quantity and complexity) than a primitive

– dynamic– ADT encapsulates data and related

services– same as a class?

• yes, classes with the special purpose of primarily maintaining data

• Example: String class

Data Structures @ UNC• COMP 410

– prereq: COMP 401 (after 110)

• Research– researchers develop new data structures for

efficiently storing and retrieving data

• Data structures are used in all areas of computer science. Knowledge of efficient ways of storing information is essential.

What Do We Want?Example

• Maintain a collection of addresses (Strings)– first, middle, and last name– telephone number– email address

• Services– new, find, insert, delete, read,

replace, sort• “Infinite” (unspecified) length

Arrays?

Perhaps but…– need to specify the length at some

point• once specified, can’t change• need to create a new array, copy, etc.

– insertion is “impossible” (hard)– deletion is wasteful

Linked Lists

• Three classes (typically) working together– an “item” class

• one atomic unit of the aggregate data• e.g., a “Name” class (item) might have two

instance variablesString first, last;

– a “node” class• one “item” and a reference to the next “node”• the next reference is the “link” in “linked list”

– a “list” class• reference to the first “node”—head of the list

Linked Lists

data

ItemItem

nextNode

Node

List

head

Node

Node

Node

Node

Node

Node

Linked ListsNew

List

head

Node

Node

Node

Node

Node

NodeNode

List

head

Node

Node

Node

Node

Node

Node

Linked ListsFind

List

head

Node

Node

Node

Node

Node

Node

Linked ListsInsert

Node

List

head

Node

Node

Node

Node

Node

Node

Linked ListsDelete

List

head

Node

Node

Node

Node

Node

Node

Node

Stacks

• Like a stack of paper (or cafeteria trays)– “last-in first-out” (LIFO)

• can only add to the top - push• can only remove from the top - pop

• Why?– Often used to keep track of execution in a

program• when returning from a method call, the computer

has to remember where it last was

Stack

Stack

top

Node

Node

Node

Node

Node

Node

Only have access to the top of the stack

May be implemented as a linked list

StacksExample

10 public shuffle() {11 int ind1 = nextInt(NUM_CARDS); }

1 public static void main (String[] args) {2 deck = new Deck();3 deck.shuffle();4 System.out.println (deck); }

20 public int nextInt (int num) {21 return 0; }

Call Stack

3

3

11top

Stack Applet

http://www.cs.hope.edu/~alganim/jvall/applet/stack.html

Queues

• Standing in line– “first-in first-out” (FIFO)

• add to the “tail” of the list (back of line) - enqueue• remove from the “head” (head of line) - dequeue

• Why?– often used to keep things in the order that

they arrived– processes to be scheduled on the CPU– packets arriving to a router

Queue

Queue

head

Node

Node

Node

Node

Node

Node

Only have access to the head and tail of the queue

May be implemented as a linked list

tail

Queue Applet

http://courses.cs.vt.edu/csonline/DataStructures/Lessons/QueuesImplementationView/applet.html

Trees

• Nodes can have more than one link• Links are bi-directional

– forward link points to children– backward link points to parent

• Why?– keeping items in sorted order

• easy to add things in sorted order

– fast search– also often used to parse arithmetic

expressions (order of operations)

Binary Trees

• Every node has a parent, except head

• Every node has at most two children

• root - has no parent• leaf - has no

children

root

leaves

Binary TreesAnd Expressions

(3 + 2) * 5

*

5+

3 2

Called a parse tree

3 + 2 * 5

+

*3

2 5

Evaluate deepest expressions first.

Binary Search Tree

• The first item added is the root

• Items less than the root go on the left branch

• Items greater than the root go on the right branch

• Makes searches very efficient

3

51

4 7

possible order added: 3 1 5 4 73 1 5 7 43 5 1 7 43 5 1 4 7

Binary Search Tree Applet

http://www.cs.jhu.edu/~goodrich/dsa/trees/btree.html

What To Expect in COMP 401

• Learn more about object-oriented programming– inheritance, interfaces

• Learn to use 2D arrays• Learn to program sorting

algorithms• Learn to program data structures

Recommended