Data Structures. Lessons 9, 10 , 11, & 12. Overview. Basic Concepts Sorting Techniques Stacks Queues Records Linked Lists Binary Trees. Basic Concepts. Whether using arrays or lists…there has to be some way to: Search the data structure Sort the data structure
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
Data Structures
Lessons 9, 10, 11, & 12
COP1000
COP1000
Whether using arrays or lists…there has to be some way to:
Search the data structure
Sort the data structure
To do this, we use keys…
COP1000
COP1000
COP1000
Sorting
COP1000
COP1000
COP1000
COP1000
Exchanges Two Values
Private Sub Swap(Array(J), Array(J+1))
If Array(J) > Array(J+1) Then
Temp = Array(J)
Array(J) = Array(J+1)
Array(J+1) = Temp
End If
End Sub
Element
Index
COP1000
1
2
3
4
5
6
COP1000
COP1000
Worst Case
Exchanges = 1/2 n(n  1) = 1/2 5(5  1) = 10
Compares = 1/2 n(n  1)= 1/2 5(5  1) = 10
Best CaseExchanges = 0Compares = n  1
COP1000
COP1000
Get Array (or List) Input
For Index = 1 to ListLength
Input Value into Array(Index)
Next Index
‘Then Sort
For I = 1 to ListLength
For J = 1 to ListLength – 1
If Array(J)>Array(J+1) Then
Call Swap (Array(J), Array(J+1))
Next J
Next I
Call to Swap isnow insidethe IF statementrather than the IFStatement being inside the Swap Procedure
COP1000
Exchange Two Values
Private Sub Swap(Array(J), Array(J+1))
Temp = Array(J)
Array(J) = Array(J+1)
Array(J+1) = Temp
End Sub
Use the Call statement to access the Swap Routine.
Swap
COP1000
The Algorithm for a Selection Sort
For Index 1 to ListLength1 do
Find the position of the smallest element in list[1..ListLength].
If List(Index) is not the position of the smallest element then
Exchange the smallest element with the one at position List(Index)
Next Index
COP1000
COP1000
Exchanges = n  1= 5  1 = 4
Compares = 1/2 n(n  1)= 1/2 5(5  1) = 10
Note: 390 is not included as it is the last item in list.
COP1000
COP1000
COP1000
Old List
New List
Exchanges = n  1= 5  1 = 4
Compares = 1/2 n(n  1)= 1/2 5(5  1) = 10
COP1000
COP1000
Searching
?
?
?
?
?
COP1000
COP1000
function ItemSearch (List : ListType;
Item : ComponentType ): Boolean;
var
Index : Integer;
begin
Index := 1;
List.Items[List.Length+1] := Item;
while List.Items[Index] <> Item do
Index := Index + 1;
ItemSearch := Index <> List.Length + 1
end;
Pascal Code
COP1000
function SeqSearch (List : ListType; Item : ComponentType ): Boolean;
varIndex : Integer;Stop : Boolean;
begin
Index := 1; Stop := False;{Initialize}
List.Items[List.Length+1] := Item;
While Not Stop Do
{Item is not in List.Items[1]..List.Items[Index1]}
If Item > List.Items[Index] then
Index := Index + 1
Else Stop := True;{Item is either found or not there}
SeqSearch := (Index <> List.Length + 1) and
(Item = List.Items[Index])
end;
Pascal Code
COP1000
COP1000
[First] [Middle][Last]
Item
COP1000
Compute the subscript of the middle element.
If the Target is the middle value Then
Middle value is target location
Return with success
ElseIf the Target is less than the middle value Then
Search sublist with subscripts First..Middle1
Else
Search sublist with subscripts Middle + 1..Last
End If
COP1000
procedure BinarySearch (var List {Input} : IntArray;
Target {Input} : Integer; First, Last {Input} : Integer;
var Index {output} : Integer; var Found {output} : Boolean);
varMiddle : Integer;
begin
Middle := (First + Last) div 2;
if First > Last then Found := False
else if Target = List[Middle] then
beginFound := True;
Index := Middleend
else if Target < List[Middle] then
BinarySearch (List, Target, First, Middle1, Index, Found)
else
BinarySearch(List, Target, Middle+1, Last, Index, Found)
end;
Pascal’s Integer Division
Found the item
Pascal Code
COP1000
Linked Structures
COP1000
COP1000
COP1000
Node
Node
Node nil
Linked List
Pointer
Pointer
COP1000
key
data
Instance
of
Node
P
COP1000
As silly as it sounds…You always know where your head is…
head
current
nil
current
node
first
node
COP1000
COP1000
COP1000
Dynamic Structures
COP1000
COP1000
head
pointer
head
pointer
4
3
2
1
4
3
2
1
a stack
a popped stack
head
pointer
5
4
3
2
1
a pushed stack
COP1000
COP1000
tail
pointer
tail
pointer
dog
head
pointer
head
pointer
an empty queue
after enqueuing an element
tail
pointer
cat
dog
tail
pointer
cat
head
pointer
head
pointer
after enqueuing
another element
after dequeuing an element
COP1000
COP1000
So…
Why is the treeupside down?
COP1000
COP1000
COP1000
COP1000
COP1000
ancestors to
60 & 95
root
50
edge
descendant
to 50
descendant
to 50
left
child
right
child
25
75
ascendant
to 60 & 95
parent to
10 & 45
children
10
45
60
95
A subtree
to 50
with nodes
75, 60, 95
siblings (or twins) to 25
leaves (or terminal nodes)
COP1000
[First][Middle] [Last] Root M1 M+1
COP1000
COP1000
It’s the order in which we place these statements that determines the type of Traversal.
COP1000
40
25
65
13
35
54
78
This is calledInorder Traversal
There are two others
13 25 35 40 54 65 78
COP1000
4
2
6
1
3
5
7
Private Sub InOrder (P : NodePointer)
If P <> nil Then
InOrder (P^.Left)
Process (P)
InOrder (P^.Right)
End If
End Sub
inorder
COP1000
1
2
5
3
4
6
7
(center, left, right)
Private Sub PreOrder (P : NodePointer)
If P <> nil Then
Process (P)
PreOrder (P^.Left)
PreOrder (P^.Right)
End If
End Sub
preorder
COP1000
7
3
6
1
2
4
5
Private Sub PostOrder (P : NodePointer)
If P <> nil Then
PostOrder (P^.Left)
PostOrder (P^.Right)
Process (P)
End If
End Sub
postorder
COP1000
10, 15, 20, 25, 30, 35, 40
COP1000
Linear List Balanced, Inorder, Binary Search Tree
10
25
15
20
35
15
25
Createdin orderfrom first tolast item inlist
10
20
30
40
30
35
Created from dividing list into twoand then dividing each side into twokeeping keys in order
40
COP1000
COP1000
Used for text manipulation
a
ab
aa
abb
aba
aab
aaa
COP1000

10
[10, 11, 12, 13, … 19]
[0, 1, 2, 3, 4, … 9]
COP1000
Object Oriented
Programming
COP1000