Title: Connecting with Computer Science, 2e
1Connecting with Computer Science, 2e
- Chapter 8
- Data Structures
2Objectives
- In this chapter you will
- Learn what a data structure is and how its used
- Learn about single-dimensional and
multidimensional arrays and how they work - Learn what a pointer is and how its used in data
structures - Learn that a linked list allows you to work with
dynamic information
3Objectives (contd.)
- In this chapter you will (contd.)
- Understand that a stack is a linked list and how
its used - Learn that a queue is another form of a linked
list and how its used - Learn that a binary tree is a data structure that
stores information in a hierarchical order - See an overview of several sorting routines
4Why You Need to Know AboutData Structures
- Data structures
- Organize the data in a computer
- Efficiently access and process data
- All programs use some form of data structure
- There are many occasions for using data structures
5Data Structures
- Defined as a way of organizing data
- Types of data structures in memory
- Arrays, lists, stacks, queues, trees
- File structures organize storage media data
- Computer memory is organized into cells
- Memory cell has a memory address and content
- Memory addresses are organized consecutively
- Data structures hide memory implementation details
6Arrays
- Set of contiguous memory cells
- Used for storing the same type of data
- Simplest memory data structure
- Consists of a set of contiguous memory cells
- Memory cells store the same type of data
- Usefulness
- Storing similar kinds of information in memory
- Sorted or left as entered
- One array name for a number of similar items
7Arrays (contd.)
Figure 8-2, Arrays make program logic easier to
understand and use
8How an Array Works
- Java example
- int aGrades new int5
- int indicates array will hold integers
- aGrades identifies the array name
- new keyword specifies new array being created
- int5 reserves five memory locations
- sign assigns aGrades as manager of the
array - (semicolon) indicates end of statement
reached - Hungarian notation standard is used to name
aGrades
9How an Array Works (contd.)
Figure 8-3, Five contiguous memory cells managed
by aGrades in a single-dimensional array
10How an Array Works (contd.)
- Element memory cell in an array
- Dimension levels created to hold array elements
- aGrades single-dimensional array (row of
mailboxes) - Offset specifies distance between memory
locations - Arrays first position referenced as position 0
- Next array position referenced as position 1
- Found by using starting memory location plus one
offset - Third array position referenced as position 2
- Found by using starting memory location plus two
offsets
11How an Array Works (contd.)
Figure 8-5, Arrays start at position 0 and use an
offset to know where the next element is located
in memory
12How an Array Works (contd.)
- Index (subscript)
- Indicates memory cell to access in the array
- Looks at elements position
- Placed between square brackets ( ) after
array name - Integer placed in for access
- Example aGrades0 50
- Position 0 first position
- Array with positions 0 to 4
- Five memory cells or addresses
- Upper bound highest array position
- Lower bound lowest array position
13How an Array Works (contd.)
Figure 8-6, The array with all elements stored
14Multidimensional Arrays
- Multidimensional arrays
- Consists of two or more single-dimensional arrays
- Multiple rows stacked on top of each other
- Apartment building mailboxes and tic-tac-toe
boards - Creating the tic-tac-toe board
- char aTicTacToe new char33
- Assignment aTicTacToe11 X
- Place X in second row of the second column
- Arrays beyond three dimensions are difficult to
manage
15Multidimensional Arrays (contd.)
Figure 8-7, A multidimensional array is like
apartment mailboxes stacked on top of each other
Figure 8-8, Tic-tac-toe board
16Multidimensional Arrays (contd.)
Figure 8-9, First row of the tic-tac-toe board
Figure 8-10, Second and third rows of the
tic-tac-toe board
17Multidimensional Arrays (contd.)
Figure 8-11, Storing a value in an array location
18Multidimensional Arrays (contd.)
Figure 8-12, Three-dimensional array
19Uses of Arrays
- Advantages
- Allow sequential access of memory cells
- Retrieve and store data with element array name
and data type - Easy to create
- Useful for people who write computer programs
- Disadvantages
- Require a lot of overhead for insertions
- Memory cell data is only accessed sequentially
20Lists
- Hold dynamic lists of data
- Lists vary in size
- Examples class enrollment, cars being repaired,
e-mail in-boxes - Appropriate whenever amount of data is unknown or
can change - Three basic list forms
- Linked lists
- Queues
- Stacks
21Linked Lists
- Use noncontiguous memory locations to store data
- Each element points to the next element in line
- Does not have to be contiguous with previous
element - Used when exact number of items is unknown
- Store data noncontiguously
- Maintain data and address of next linked cell
- Examples names of students visiting a professor,
points scored in a video game, list of spammers - Basic constructs for more advanced data
structures - Queues and stacks pointer based
22Linked Lists (contd.)
- Pointers memory variable containing the address
of a memory cell as its data - Illustration linked list game
- Students sit in a circle with piece of paper
- Paper has box in the upper left corner and center
- Upper left box indicates a student number
- Center box divided into two parts
- Students indicate favorite color in left part of
center - Professor has a piece of paper with a number only
23Linked Lists (contd.)
Figure 8-14, Structure of a linked list
24Linked Lists (contd.)
- Piece of paper represents a two-part node
- Data (the first part, the color)
- Pointer (the student ID number)
- Professors piece head pointer with no data
- Last student pointers value is NULL
- Inserting new elements
- No resizing needed
- Create new piece of paper with dual node
structure - Realign pointers to accommodate new node (paper)
25Linked Lists (contd.)
Figure 8-15, Inserting an element into a linked
list
26Linked Lists (contd.)
- Similar procedure for deleting items
- Modify pointer of element preceding target item
- Students deleted from list without moving
elements - Use dynamic memory allocation
- More efficient than arrays
- Memory cells need not be contiguous
27Linked Lists (contd.)
Figure 8-16, Deleting an element from a linked
list
28Stacks
- List in which the next item to be removed is the
item most recently stored - Push items on to the list to store new items
- Pop items off the list to retrieve current
items - Examples
- Restaurant spring-loaded plate holder or text
editor - Peeking
- Looking at the stacks top item without removing
it - LIFO data structure
- Last or most recent item pushed (put) onto the
stack - Becomes first item popped (removed) from the stack
29Stacks (contd.)
Figure 8-17, The stack concept
30Uses of a Stack
- Processes lines of program source code
- Source code is logically organized into
procedures - Keep track of procedure calls with a stack
- Address of procedure popped off stack
31Back to Pointers
- Stack pointer
- Keeps track of where to remove or add an item in
a data structure - Check stack before applying pop or push
operations - Stacks
- Memory locations organized into logical
structures - Facilitates reading from them and writing to them
32Back to Pointers (contd.)
Figure 8-18, Stack pointer is decremented when
the item is popped off
33Queues
- Another type of linked list
- Implements first in, first out (FIFO) storage
system - Insertions made at the end
- Deletions made at the beginning
- Similar to that of a waiting line
34Uses of a Queue
- Printer example
- First item printed
- Document waiting longest
- Current item deleted from queue
- Next item printed
- New documents
- Placed at the end of the queue
- Insertions of new data occur at the rear of the
queue - Removal of data occurs at the front of the queue
35Pointers Again
- Head pointer tracks beginning of queue
- Tail pointer tracks end of queue
- Queue containing no items
- Both the head and tail pointer point to same
location - Dequeue operation
- Remove item (oldest entry) from the queue
- Head pointer changed to point to the next item in
list - Enqueue operation
- Item placed at list end
- Tail pointer updated
36Pointers Again (contd.)
Figure 8-19, A queue uses a FIFO structure
37Pointers Again (contd.)
Figure 8-20, Removing an item from the queue
Figure 8-21, Inserting an item into the queue
38Trees
- Hierarchical data structure similar to
organizational or genealogy charts - Node or vertex position in the tree
Figure 8-22, Tree data structure
39Trees (contd.)
- Binary tree
- Each node has at most two child nodes
- Node can have zero, one, or two child nodes
- Left child child node to the left of the parent
node - Right child child node to the right of the
parent node - Root node that begins the tree
- Leaf node node that has no child nodes
- Depth (level) distance from root node
- Height longest path length in the tree
40Trees (contd.)
Figure 8-24, The level and height of a binary
tree
Figure 8-23, Tree nodes
41Uses of Binary Trees
- Binary search tree a type of binary tree
- Data value of left child node is less than the
value of parent node - Data value of right child node is greater than
the value of parent node - Useful for searching through stored data
- Storing information in a hierarchical
representation
42Uses of Binary Trees (contd.)
Figure 8-25, A file system structure can be
stored as a binary search tree
43Searching a Binary Tree
- Three components in a binary search tree node
- Left child pointer, right child pointer, data
- Root pointer contains root nodes address
- Provides initial access to the tree
- If left or right child pointers contain a null
value - Node is not a parent to other nodes down that
specific path - If both left and right pointers contain null
values - Node is not a parent down either path
- Binary tree must be defined properly to be
searchable
44Searching a Binary Tree (contd.)
Figure 8-26, A node in a binary search tree
45Searching a Binary Tree (contd.)
- Search routine
- Start at the root position
- Determine if path moves to left child or right
- Move in direction of data (left or right)
- If left pointer NULL
- No node to traverse down the left side
- If left pointer does have a value
- Path continues down that side
- If value looking for is found
- Stop at that node
46Searching a Binary Tree (contd.)
Figure 8-27, Searching a binary tree for the
value 8
47Searching a Binary Tree (contd.)
Figure 8-28, Searching a binary tree for the
value 1
48Sorting Algorithms
- Sorting leverages data structures to organize
data - Example of data being sorted
- Words in a dictionary
- Files in a directory
- Index of a book
- Course offerings at a university
- Many algorithms for sorting
- Each has advantages and disadvantages
- Focus selection and bubble sorts
49Selection Sort
- Selection sort mimics manual sorting
- Starts at first value in the list
- Processes each element looking for smallest value
- After smallest value found, it is placed in first
position - Moves first position value to location originally
containing smallest value - Sort moves on looking for next smallest value
- Continues to swap places
- Simple to use and implement
- Inefficient for large lists
50Selection Sort (contd.)
Figure 8-29, A selection sort
51Bubble Sort
- Bubble older and slower sort method
- Start with the last element in the list
- Compare its value to that of the item just above
- If smaller, change positions and continue up list
- Continue comparison until smaller item found
- If not smaller, next item compared to item above
- Check until smallest value bubbles to the top
- Process repeated for list less first item
- Simple to implement
- Inefficient for large lists
52Bubble Sort (contd.)
Figure 8-30, A bubble sort
53Bubble Sort (contd.)
Figure 8-31, The bubble sort continues
54Other Types of Sorts
- Quicksort incorporates divide and conquer
logic - Two small lists are easier to sort than one large
list - Uses recursion to break down problem
- All sorted sub-lists are combined into single
sorted set - Fast but difficult to comprehend
55Other Type of Sorts (contd.)
- Merge sort similar to the quicksort
- Continuously halves data sets using recursion
- Sorted halves are merged back into one list
- Time efficient, but not as space efficient as
quicksort - Insertion sort simulates manual sorting of cards
- Requires two lists
- Not complex, but inefficient for list size fewer
than 1000 - Shell sort uses insertion sort against expanding
data set
56One Last Thought
- Algorithms
- Used everywhere in the computer industry
- Knowing how to work with data structures and
sorting algorithms is necessary to begin writing
computer programs - Many algorithms are written and available for use
- Knowing the tools available and which sort
routine will perform best for a situation saves
time
57Summary
- Data structures organize data
- Basic data structures
- Arrays, lists, queues, stacks, trees
- Arrays
- Store data contiguously
- May have one or more dimensions
- Linked lists
- Store data in dynamic containers
- Use pointers for noncontiguous storage
- Pointer contains memory cell address as its data
58Summary (contd.)
- Stack
- Linked list structured as LIFO container
- Queue
- Linked list structured as FIFO container
- Tree
- Hierarchical structure consisting of nodes
- Binary tree nodes have at most two children
- Binary search tree efficient for searching for
information
59Summary (contd.)
- Sorting algorithms
- Organize data within structure
- Examples selection sort, bubble sort, quicksort,
merge sort, insertion sort, shell sort - Sorting routines
- Analyzed by code, space, and time complexities