Chapter 12: Data Structures
Chapter 12: Data Structures
Presentation slides are copyright 2002 by John Lewis and William Loftus. All rights reserved.
Instructors using the textbook may use and modify these slides for pedagogical purposes.
Data Structures
Now we explore some convenient techniques for organizing
and managing information
2
Collections
A collection is an object that serves as a repository for other
objects
4
Abstraction
Our data structures should be abstractions
7
Object References
Recall that an object reference is a variable that stores the
address of an object
student
John Smith
40725
3.58
8
References as Links
Object references can be used to create links between
objects
10
Intermediate Nodes
The objects being stored should not be concerned with the
details of the data structure in which they may be stored
11
Magazine Collection
Let’s explore an example of a collection of Magazine objects
info
next
insert this new node
Fig. 12.3 - deleting a node from a list
header node
list count: 4
front
list with front and rear references
rear
If the linked list is doubly linked, the first node in the list also
points to the last node in the list
enqueue dequeue
21
Queues
A queue can be represented by a singly-linked list.
Operationrs: enqueue – add an item to rear
dequeue – remove an item from front
empty – returns true if queue is empty
Is it more efficient if the references point from front to the rear?
rear front
Two info4 info3 info2 info1 queue
representations
of same queue
null next next next
0 1 2 3 4 5
Stacks
A stack ADT is also linear, like a list or a queue
Items are added and removed from only one end of a stack
Analogies:
• a stack of plates in a cupboard
• a stack of bills to be paid
• a stack of hay bales in a barn
24
Fig. 12.7 – stack data structure
Stacks often are drawn vertically:
push pop
25
Stacks
Some stack operations:
• push - add an item to the top of the stack
• pop - remove an item from the top of the stack
• peek (or top) - retrieves the top item without removing it
• empty - returns true if the stack is empty
reverse a string
push pop
1
5 S
t r a m S4 m Smart
2
3 a 3
2 r
1 4
t 5
Fig 12.8 - Trees
A tree is a non-linear data structure that consists of a root node
and potentially many levels of additional nodes that form a
hierarchy
Nodes that have no children are called leaf nodes
Non-root and non-leaf nodes are called internal nodes
root node
imagine an upside
down tree
internal
nodes
VP VP VP VP
mgr
leaf nodes
Binary Trees
A binary tree is defined recursively. Either it is empty (the
base case) or it consists of a root and two subtrees, each of
which is a binary tree
leaf nodes
Fig. 12.9 - graph
A graph is a non-linear structure (also called a network)
Unlike a tree or binary tree, a graph does not have a root – no
primary entry point.
Any node can be connected to any other node by an edge
Can have any number of edges and nodes
C F P
E J
D L
B
a directed graph Y N
A X R
W S
airline routes represented via digraph
What else could be provided?
Graphs and Digraphs
Both graphs and digraphs can be represented using
dynamic links or using arrays.