Size of structure
determined when writing
program.
Size of data structure has to be
estimated during the creation of
the program
Space can be
wasted.
Dynamic data structure
Size of structure updates
based on the information
that is stored in it.
Only uses the space
needed at any time
More difficult to
program.
Stacks
Last in first out data structure
Insertion
1: Check to see if
stack is full
2: If stack is full report
an error and stop
3: Increment stack pointer
4: Insert item into cell pointed
to by the stack pointer and stop
Deletion / Reading
1: Check to see
if stack is empty
2: If stack is empty
report an error and stop
3: Copy data item at cell pointed
at by the stack pointer
The data item copied can be either
discarded or read depending on the
operation that is being performed
4: Decrement the
stack pointer and stop
Queues
First in first out data structure
Insertion
1: Check to see
if queue is full
2: If queue is full report
an error and stop
3: Insert item to the cell that is
pointed to by the head pointer.
4: Increment head
pointer and stop
Deletion / Reading
1: Check to see if
queue is empty
2: If queue is empty
report an error and stop
3: Copy data item pointed
to by the tail pointer
4: Increment the tail
pointer and stop
Binary
Tree
An example of
a binary tree
Dynamic data structures have to be stored as static data structures
in order to be processed by the computer. Binary trees, for
example, are stored in the form of an array (a static data
structure). Above is the binary tree on the left stored as an array.
Insertion
1: If tree is empty enter
data item at root and stop
2: Current node = root
3: Repeat steps 4 & 5
until current node = null
4: If data item is less than the
value at the current node
then go left else go right
5: Current node = node
reached (null if no node).
6: Create new node
and enter data
Deletion
Option 1: Leave the structure of the tree unchanged by
labeling the deleted node as deleted, keeping it in the tree
but making sure it is not read when the tree is searched.
Option 2: Start from the node to be deleted and gather all
of the values from the nodes below the node to be deleted
and store in some form of data structure (other than a
tree). Then remove the node to be deleted and starting
from the node above the node that was deleted add the
nodes saved in the separate file back into the tree.
In reality both methods are used. Option 1 because it is quicker and,
after a certain number of nodes have been deleted this way,Option 2
is used in order to clear space in memory (as marking the node as
deleted means that the node stays in memory.)
Searching
Serial Searching
Requires data to be in consecutive order
locations (meaning require data to be in
a data structure such as an array)
Does not require data to be in a order
To find the position of a value you have to
iterate through the list comparing each
value with the value you are looking for.
The smallest amount of searches that is required is 1
when the item is at the top of the list and n where n =
number of items, when item is at the bottom of the
list, therefore the speed of this algorithm is n / 2
Binary Searching
Like serial searching requires
data to be in consecutive
order locations
The data needs to be
sorted into order first
which can take some time.
To find the position of a value the midway of the list
is looked at and the item is compared against this
item to see if it is smaller or bigger than the item.
The same is then done for the items above or below
it depending on whether the item is less or more
than the item that is midway until one item is left.
If there are two items in
the middle the smaller
value item is used.
Speed of serial searching as
opposed to binary searching
Merging lists
Compare the first two items in two sorted lists. The
smaller one is put in a new list and is removed from
it's list. The lists are again compared until all of the
items have been put in the new list.