comp 110 introduction to programming mr. joshua stough
Post on 21-Dec-2015
228 views
TRANSCRIPT
![Page 1: COMP 110 Introduction to Programming Mr. Joshua Stough](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649d605503460f94a41687/html5/thumbnails/1.jpg)
COMP 110 Introduction to Programming
Mr. Joshua Stough
![Page 2: COMP 110 Introduction to Programming Mr. Joshua Stough](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649d605503460f94a41687/html5/thumbnails/2.jpg)
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
![Page 3: COMP 110 Introduction to Programming Mr. Joshua Stough](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649d605503460f94a41687/html5/thumbnails/3.jpg)
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.
![Page 4: COMP 110 Introduction to Programming Mr. Joshua Stough](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649d605503460f94a41687/html5/thumbnails/4.jpg)
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
![Page 5: COMP 110 Introduction to Programming Mr. Joshua Stough](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649d605503460f94a41687/html5/thumbnails/5.jpg)
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
![Page 6: COMP 110 Introduction to Programming Mr. Joshua Stough](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649d605503460f94a41687/html5/thumbnails/6.jpg)
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
![Page 7: COMP 110 Introduction to Programming Mr. Joshua Stough](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649d605503460f94a41687/html5/thumbnails/7.jpg)
Linked Lists
data
ItemItem
nextNode
Node
List
head
Node
Node
Node
Node
Node
Node
![Page 8: COMP 110 Introduction to Programming Mr. Joshua Stough](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649d605503460f94a41687/html5/thumbnails/8.jpg)
Linked ListsNew
List
head
Node
Node
Node
Node
Node
NodeNode
List
head
Node
Node
Node
Node
Node
Node
![Page 9: COMP 110 Introduction to Programming Mr. Joshua Stough](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649d605503460f94a41687/html5/thumbnails/9.jpg)
Linked ListsFind
List
head
Node
Node
Node
Node
Node
Node
![Page 10: COMP 110 Introduction to Programming Mr. Joshua Stough](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649d605503460f94a41687/html5/thumbnails/10.jpg)
Linked ListsInsert
Node
List
head
Node
Node
Node
Node
Node
Node
![Page 11: COMP 110 Introduction to Programming Mr. Joshua Stough](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649d605503460f94a41687/html5/thumbnails/11.jpg)
Linked ListsDelete
List
head
Node
Node
Node
Node
Node
Node
Node
![Page 12: COMP 110 Introduction to Programming Mr. Joshua Stough](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649d605503460f94a41687/html5/thumbnails/12.jpg)
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
![Page 13: COMP 110 Introduction to Programming Mr. Joshua Stough](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649d605503460f94a41687/html5/thumbnails/13.jpg)
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
![Page 14: COMP 110 Introduction to Programming Mr. Joshua Stough](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649d605503460f94a41687/html5/thumbnails/14.jpg)
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
![Page 15: COMP 110 Introduction to Programming Mr. Joshua Stough](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649d605503460f94a41687/html5/thumbnails/15.jpg)
Stack Applet
http://www.cs.hope.edu/~alganim/jvall/applet/stack.html
![Page 16: COMP 110 Introduction to Programming Mr. Joshua Stough](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649d605503460f94a41687/html5/thumbnails/16.jpg)
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
![Page 17: COMP 110 Introduction to Programming Mr. Joshua Stough](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649d605503460f94a41687/html5/thumbnails/17.jpg)
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
![Page 18: COMP 110 Introduction to Programming Mr. Joshua Stough](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649d605503460f94a41687/html5/thumbnails/18.jpg)
Queue Applet
http://courses.cs.vt.edu/csonline/DataStructures/Lessons/QueuesImplementationView/applet.html
![Page 19: COMP 110 Introduction to Programming Mr. Joshua Stough](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649d605503460f94a41687/html5/thumbnails/19.jpg)
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)
![Page 20: COMP 110 Introduction to Programming Mr. Joshua Stough](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649d605503460f94a41687/html5/thumbnails/20.jpg)
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
![Page 21: COMP 110 Introduction to Programming Mr. Joshua Stough](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649d605503460f94a41687/html5/thumbnails/21.jpg)
Binary TreesAnd Expressions
(3 + 2) * 5
*
5+
3 2
Called a parse tree
3 + 2 * 5
+
*3
2 5
Evaluate deepest expressions first.
![Page 22: COMP 110 Introduction to Programming Mr. Joshua Stough](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649d605503460f94a41687/html5/thumbnails/22.jpg)
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
![Page 23: COMP 110 Introduction to Programming Mr. Joshua Stough](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649d605503460f94a41687/html5/thumbnails/23.jpg)
Binary Search Tree Applet
http://www.cs.jhu.edu/~goodrich/dsa/trees/btree.html
![Page 24: COMP 110 Introduction to Programming Mr. Joshua Stough](https://reader036.vdocuments.site/reader036/viewer/2022062407/56649d605503460f94a41687/html5/thumbnails/24.jpg)
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