Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

_2.Data_structures_and_algorithms

The document outlines various data structures and memory allocation methods, including contiguous, linked, and indexed allocations, with examples such as arrays and linked lists. It also discusses other data structures like trees and graphs, as well as the efficiency of operations on these structures. Finally, it emphasizes the importance of understanding algorithm run times for implementing queries and operations effectively.

Uploaded by

15pwcse1348
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

_2.Data_structures_and_algorithms

The document outlines various data structures and memory allocation methods, including contiguous, linked, and indexed allocations, with examples such as arrays and linked lists. It also discusses other data structures like trees and graphs, as well as the efficiency of operations on these structures. Finally, it emphasizes the importance of understanding algorithm run times for implementing queries and operations effectively.

Uploaded by

15pwcse1348
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Data Structures

and Algorithms
Data Structures and Algorithms
2
2.2 Outline

This topic will describe:


– The concrete data structures that can be used to store information
– The basic forms of memory allocation
• Contiguous
• Linked
• Indexed
– The prototypical examples of these: arrays and linked lists
– Other data structures:
• Trees
• Hybrids
• Higher-dimensional arrays
– Finally, we will discuss the run-time of queries and operations on arrays
and linked lists
Data Structures and Algorithms
3
2.2.1 Memory Allocation

Memory allocation can be classified as either


– Contiguous
– Linked
– Indexed

Prototypical examples:
– Contiguous allocation: arrays
– Linked allocation: linked lists
Data Structures and Algorithms
4
2.2.1.1 Memory Allocation

Contiguous, adj.
Touching or connected throughout in an unbroken sequence.
Meriam Webster

Touching, in actual contact, next in space; meeting at a common


boundary, bordering, adjoining.
www.oed.com
Data Structures and Algorithms
5
2.2.1.1
2.2.1 Contiguous Allocation

An array stores n objects in a single contiguous space of memory


Unfortunately, if more memory is required, a request for new
memory usually requires copying all information into the new
memory
– In general, you cannot request for the operating
system to allocate to you the next n memory
locations
Data Structures and Algorithms
6
2.2.1.2 Linked Allocation

Linked storage such as a linked list associates two pieces of data


with each item being stored:
– The object itself, and
– A reference to the next item
• In C++ that reference is the address of the next node
Data Structures and Algorithms
7
2.2.1.2 Linked Allocation

This is a class describing such a node

template <typename Type>


class Node {
private:
Type element;
Node *next_node;
public:
// ...
};
Data Structures and Algorithms
8
2.2.1.2 Linked Allocation

The operations on this node must include:


– Constructing a new node
– Accessing (retrieving) the value
– Accessing the next pointer

Node( const Type& = Type(), Node* = nullptr );


Type retrieve() const;
Node *next() const;
Data Structures and Algorithms
9
2.2.1.2 Linked Allocation

For a linked list, however, we also require an object which links to


the first object

The actual linked list class must store two pointers


– A head and tail:
Node *head;
Node *tail;
Optionally, we can also keep a count
int count;
The next_node of the last node is assigned nullptr
Data Structures and Algorithms
10
2.2.1.2 Linked Allocation

The class structure would be:

template <typename Type>


class List {
private:
Node<Type> *head;
Node<Type> *tail;
int count;
public:
// constructor(s)...
// accessor(s)...
// mutator(s)...
};
Data Structures and Algorithms
11
2.2.1.3 Indexed Allocation

With indexed allocation, an array of pointers


(possibly NULL) link to a sequence of allocated
memory locations
Data Structures and Algorithms
12
2.2.1.3 Indexed Allocation

Matrices can be implemented using indexed allocation:

 1 2 3
 
 4 5 6 
Data Structures and Algorithms
13
2.2.1.3 Indexed Allocation

Matrices can be implemented using indexed allocation


– Most implementations of matrices (or higher-dimensional arrays) use
indices pointing into a single contiguous block of memory

Row-major order Column-major order


 1 2 3
 
 4 5 6 
Data Structures and Algorithms
14
2.2.2 Other Allocation Formats

We will look at some variations or hybrids of these memory


allocations including:
– Trees
– Graphs
– Deques (linked arrays)
– inodes
Data Structures and Algorithms
15
2.2.2.2 Trees

The linked list can be used to store linearly ordered data


– What if we have multiple next pointers?

A rooted tree is similar


to a linked list but with multiple next
pointers
Data Structures and Algorithms
16
2.2.2.2 Trees

A tree is a variation on a linked list:


– Each node points to an arbitrary number of subsequent nodes
– Useful for storing hierarchical data
– We will see that it is also useful for storing sorted data
– Usually we will restrict ourselves to trees where each node points to at
most two other nodes
Data Structures and Algorithms
17
2.2.2.2 Graphs

Suppose we allow arbitrary relations between any two objects in a


container
– Given n objects, there are n2 – n possible relations
n2  n
• If we allow symmetry, this reduces to
2

– For example, consider the network


Data Structures and Algorithms
18
2.2.2.2 Arrays

Suppose we allow arbitrary relations between any two objects in a


container
– We could represent this using a two-dimensional array
– In this case, the matrix is A B C D E F G H I J K L
symmetric A × × ×
B × × × × ×
C × × × × × ×
D × × ×
E × ×
F × ×
G × × ×
H × × ×
I × ×
J × × ×
K × × ×
L × × ×
Data Structures and Algorithms
19
2.2.2.2 Array of Linked Lists

Suppose we allow arbitrary relations between any two objects in a


container
– Alternatively, we could use a hybrid: an array of linked lists

A
B
C
D
E
F
G
H
I
J
K
L
Data Structures and Algorithms
20
2.2.2.3 Linked Arrays

Other hybrids are linked lists of arrays

For example, the alphabet could be stored either as:


– An array of 26 entries, or
– A linked list of arrays of 8 entries
Data Structures and Algorithms
21
2.2.3 Algorithm run times

Once we have chosen a data structure to store both the objects and
the relationships, we must implement the queries or operations as
algorithms
– The Abstract Data Type will be implemented as a class
– The data structure will be defined by the member variables
– The member functions will implement the algorithms

The question is, how do we determine the efficiency of the


algorithms?
Data Structures and Algorithms
22
2.2.3 Operations

We will us the following matrix to describe operations at the


locations within the structure

Arbitrary
Front/1st Back/nth
Location
Find ? ? ?
Insert ? ? ?
Erase ? ? ?
Data Structures and Algorithms
23
2.2.3.1 Operations on Sorted Lists

Given an sorted array, we have the following run times:

Arbitrary
Front/1st Back/nth
Location
Find Good Okay Good
Insert Bad Bad Good* Bad
Erase Bad Bad Good

* only if the array is not full


Data Structures and Algorithms
24
2.2.3.2 Operations on Lists

If the array is not sorted, only one operations changes:

Arbitrary
Front/1st Back/nth
Location
Find Good Bad Good
Insert Bad Bad Good* Bad
Erase Bad Bad Good

* only if the array is not full


Data Structures and Algorithms
25
2.2.3.3 Operations on Lists

However, for a singly linked list where we a head and tail pointer, we
have:

Arbitrary
Front/1st Back/nth
Location
Find Good Bad Good
Insert Good Bad Good
Erase Good Bad Bad
Data Structures and Algorithms
26
2.2.3.3 Operations on Lists

If we have a pointer to the kth entry, we can insert or erase at that


location quite easily

Arbitrary
Front/1st Back/nth
Location
Find Good Bad Good
Insert Good Good Good
Erase Good Good Bad
Data Structures and Algorithms
27
2.2.3.4 Operations on Lists

For a doubly linked list, one operation becomes more efficient:

Arbitrary
Front/1st Back/nth
Location
Find Good Bad Good
Insert Good Good Good
Erase Good Good Good

You might also like