Download EE 461_Data Structures

Document related concepts

Lattice model (finance) wikipedia , lookup

Quadtree wikipedia , lookup

Red–black tree wikipedia , lookup

B-tree wikipedia , lookup

Interval tree wikipedia , lookup

Linked list wikipedia , lookup

Binary tree wikipedia , lookup

Binary search tree wikipedia , lookup

Transcript
DATA STRUCTURES
• Data structure is concerned with the various
ways that data files can be organized and
assembled.
• The structures of data files will strongly
influence the selection of a computational
strategy for a given data processing job.
DATA STRUCTERS
• 1- A List is a collection of sets of data
elements. LIST and FILE have the same
meaning.
• 2- Nodes : The sets of data contained in a list
are referred to as Node. Each node will
contain several related data items. A node is
equivalent to a record.
DATA STRUCTERS
• The items within a node are required to be
stored in storage locations, though the nodes
themselves need not be adjacent to one
another.
• 3- Field: Each of the items within a given node
is said to occupy a field. A field can contain
either a data item or a link [or pointer].
DEFINITION
• 4- The Link [ Pointer]: Contain the starting
addresses of other nodes, thus creating a
structural interrelationship among the nodes.
4
Novels arranged by title but linked
according to authorship
5
DEFINITION
• The link tell us that the next node in the list
has starting address of xxxx.
• A node need not be restricted to one link,
multilinked nodes are common. Each of the
nodes in a given list should contain the same
number of links.
6
DEFINITION
Linear Lists:
A linear list is a list which the nodes are ordered in one
dimensional arrangement. [ N1, N2, ---, Nr]
Sequential linear list
When Nodes are stored consecutive adjacent to one
another. We refer to the list as sequential linear list
.[M/C language, Computer programs, Magnetic tape
data files are usually stored in the form of sequential
linear list].
7
LINKED LINEAR LIST
• Most searching, sorting, and merging
operations can be carried out quite easily with
sequential linear lists. The simple type of the
linked list is the single linked linear list. Each
node will contain one pointer, which indicates
the starting address of the next node in the
list.
8
The array of Readings stored in
memory starting at address
9
A two-dimensional array with four rows
and five columns stored in row major order
10
Names stored in memory as a
contiguous list
11
A two-dimensional array with four rows
and five columns stored in row major order
12
Names stored in memory as a
contiguous list
13
The structure of a linked list
14
Deleting an entry from a linked list
15
Inserting an entry into a linked list
16
A procedure for printing a linked list
17
MULTIPLE LINKS
• If each node in the list contains several items,
each of which should be ordered in some
particular manner, then a different set of links
is established for each item.
• Each set of links establishes a separate single
linked linear list.
18
STACKS
• A stack is a list in which all insertions and
deletions are performed at the same end of
the structure.
• Stack known as last-in, first-out [LIFO]
structures.
19
BACKTRACKING
• A classic application of a stack occurs when a
program unit requests the execution of a procedure.
If the procedure itself request the execution of
another procedure, and so on .
• Stack is an ideal structure for such a system.
• This process is called Backtracking
20
Nested procedures terminating in the opposite
order to that in which they were requested
21
BACKTRACKING
• Suppose we want to print the names in a
linked list in a reverse order that is last name
first.
• We can access the names only by following
the link structure. Thus we need to use the
stack.
22
Using a
stack to
print a
linked list
in
reverse
order
(continue
d)
23
Using a
stack to
print a
linked list
in reverse
order
24
A procedure (using an auxiliary stack)
for printing a linked list in reverse order
25
A stack in memory
26
Stacks
•
•
•
•
Last-in first-out.
Push and pop.
Using stacks for maintaining procedure calls.
Other applications???
27
QUEUES
• Queue is a list in which all insertions are
performed at one end while all deletions are
made at the other end.
• The end at which entries are removed is called
the Head [ or sometimes the Front] of the
queue. The end of the queue at which new
entries are added is called the Tail[ or Rear].
28
QUEUES
• QUEUE uses two memory cells to use as
pointers instead of just one .
• Head Pointer
• Tail Pointer
29
A queue implemented with head and tail
pointers
30
A queue “crawling” through memory
31
A circular
queue
(a)
containing
the letters F
through O as
actually
stored in
memory
32
A circular queue (b) in its conceptual form in
which the last cell in the block is “adjacent” to
the first cell
33
Queues
• First-in first-out.
• Head and tail.
• Circular queue.
34
TREES
• The last Data structure that we will consider is
the tree, which is the structure reflected by an
organization chart of a typical company.
35
An example of an organization chart
36
TREES
• Tree consists of a collection of nodes which
are interconnected by branches to form a tree
like configuration, as shown below:
37
Tree terminology
38
TREES
• The branches emanate downward from the
nodes, The node is presented by a square. The
top node is called the root of the tree.
• All of the remaining nodes are called either
branch or terminal nodes.
• The branch nodes have branches emanating
from them, the terminal nodes do not.
39
TREES
• The degree of a node is equal to the number
of sub-trees that emanate from that node.
• Terminal nodes are therefore nodes of degree
zero.
• All trees have two or more levels. The root is
the 1st level. The subsequent nodes increase in
level as they branch out from the root.
40
BINARY TREE
• It is tree in which each node has at most two
children. This trees are stored in memory
using a linked structure with two pointer
called:
• Left Child Pointer
• Right Child Pointer
41
The structure of a node in a
binary tree
42
The conceptual and actual organization of a
binary tree using a linked storage system
43
A tree stored without pointers
44
BINARY TREE
• Another alternative storage system
• The location of a node’s parent can be found
by dividing the node’s position in the block by
2 while discarding any remainder [the parent
of the node in position 7 would be the node in
position 3]
45
BINARY TREES
• The location of a nodes sibling can be found
by adding 1 to the location of a node in even
position or subtract 1 from location of a node
in an odd position.
• Node 4 ( 4+1)= 5
• Node 3 ( 3- 1 )= 2
46
A sparse, unbalanced tree shown in its
conceptual form and as it would be stored
without pointers
47
Searching the list
• If the list were stored according to the linked
list model , we would be forced to search the
list in sequential fashion, a process could be
very inefficient if the list should become long.
We will seek an implementation that allows us
to use the binary search algorithm.
48
Searching The List
• To apply this algorithm , our storage system
must allows us to find the middle entry of
successively smaller portion of the list.
• This can be done using linked binary tree.
• Ex. The list of letters [ A, B, C, D, E, F, G, H, I, J,
K, L, and M ]
49
The letters A through M arranged in an ordered
tree
50
The binary search as it would appear if the list
were implemented as a linked binary tree
51
The successively smaller trees considered by
the procedure in when searching for the letter
J
52
Printing a search tree in alphabetical
order
53
A procedure for printing the data in a binary
tree
54
Inserting the entry M into the list
B, E, G, H, J, K, N, P stored as a tree
(continued)
55
Inserting the entry M into the list
B, E, G, H, J, K, N, P stored as a tree
56
A procedure for inserting a new entry in a list
stored as a binary tree
57
Trees
• Trees - an organization chart; e.g., family tree
and company’s organization .
• Root node, leaf nodes, arc, sub trees.
• Parent, children, siblings.
• Depth of a tree.
• Tree implementation.
• Binary tree.
58
A stack of integers implemented
in C++
59
A stack of integers implemented in Java and
C#
60