You are on page 1of 20

2/15/2021

Abstraction

Computational thinking and


problem-solving

Abstraction – Functions and Procedures

1
2/15/2021

Abstraction – Functions and Procedures ADT – Abstract Data Type

The Record Type / Composite Type The Record Type


• Sometimes variables of different data types are a logical group, such • Using the example above we can declare a Person record type:
as data about a person (name, date of birth, height, number of
siblings, whether they are a full-time student). TYPE PersonType
• Name is a STRING; date of birth is a DATE; height is a REAL; number of Name : STRING
siblings is an INTEGER; DateOfBirth : DATE
• whether they are a full-time student is a BOOLEAN. Height : REAL
NumberOfSiblings : INTEGER
• We can declare a record type to suit our purposes. The record type is IsFullTimeStudent : BOOLEAN
known as a user-defined type, because the programmer can decide ENDTYPE
which variables (fields) to include as a record.

2
2/15/2021

The Record Type ADT – Abstract Data Type


• To declare a variable of this type we write:

DECLARE Person : PersonType


And now we can assign a value to a field of this Person record:

Person.Name ← "Fred"
Person.NumberOfSiblings ← 3
Person.IsFullTimeStudent ← TRUE
To output a field of a record:
OUTPUT Person.Name

ADT – LIST ADT – LIST

3
2/15/2021

ADT – LIST ADT – STACK

ADT – STACK ADT – QUEUE

4
2/15/2021

ADT – QUEUE Queue Using an Array


• We will maintain two pointers - tail and head to represent
a queue. head will always point to the oldest element which was
added and tail will point where the new element is going to be
added.

ADT – TREE

5
2/15/2021

6
2/15/2021

7
2/15/2021

8
2/15/2021

9
2/15/2021

10
2/15/2021

Addressing a linked List

Addressing a linked List Linked list diagram


Storing an alphabetic list of names in a linked list
• A List data structure is to be used to store some names. The list is in
alphabetic sort order.
• The bare list looks like:
Paul Hameed Ali Sajid

Paul Hameed Ali Sajid

Start Null
Pointer Pointers

11
2/15/2021

Addressing a linked List Inserting a node to a linked list


An example sequence of loading the list might be One of the basic algorithms needed to maintain the list is INSERT. This
is explained by carrying on with the example from the previous page.
But basically the INSERT algorithm adjusts the pointer of the relevant
nodes to make sure the list remains in the correct order.
1. List is empty

2. Paul added to the list. Its pointer is null to also mark it as the last node. The start pointer also points to Paul
An example sequence of loading the list might be to INSERT Paul, Ali,
3. Ali is added. Now the start pointer needs to point to Ali since this name comes before Paul alphabetically Hameed and Sajid in that order. Let's examine how the INSERT
4.Sajid is added. So the Paul pointer now points to Sajid and Sajid is given a Null pointer. algorithm is used.
5. Hameed is added. The Ali pointer is changed to point to Hameed and the Hameed pointer now points to Paul.

Inserting a node to a linked list Deleting a node from a linked list


• List is empty
• Paul added to the list. Its pointer is null to mark it as the last node. The • One of the basic algorithms needed to maintain the list is DELETE.
start pointer also points to Paul. This is explained by carrying on with the example from the previous
• Ali is added. Now the INSERT algorithm adjusts the start pointer of the list Slide.
to point to Ali since this name comes before Paul. Paul remains the last • But basically the DELETE algorithm adjusts the pointer of the relevant
node.
nodes to make sure the list remains in the correct order after the
• Sajid is added. Sajid comes after Paul, so the INSERT algorithm adjusts the node is removed
Paul node pointer to Sajid and the Sajid node is given a Null pointer.
• Hameed is added. The INSERT algorithm of the list works out the Hameed
node needs to lie between the Ali and Paul nodes, so the Ali pointer is
changed to point to Hameed and the Hameed pointer now points to Paul.
• The linked list is complete.

12
2/15/2021

Deleting a node from a linked list Deleting a node from a linked list
Starting with the first node pointed to by the start pointer • Case 2:
Is this the node to be removed? The node to be removed is an intermediate one Examine the next node by
using the node pointers to move from node to node until the correct node is
• Case 1: identified
The node to be removed is the first one. Adjust the start pointer to Node found
point to the next node Copy the pointer of the removed node into temporary memory
Remove the original node Remove the node from the list and mark the memory it was using as free
once more
Mark the memory it used as free once more
Update the previous node's pointer with the address held in temporary
Task complete memory
Task complete

Binary Tree
Deleting a node from a linked list
• Case 3:
It is the last node to be removed
• Remove the last node from the list
• Mark the prior node with a null pointer
• Mark the memory the deleted node used as free once more.
• Task complete
• Case 4:
• Node cannot be found
• Return an error message to the calling code
• Task complete

13
2/15/2021

The BINARY SEARCH TREE


Binary Tree Operations
• One of the most powerful uses of the TREE data structure is to sort and
manipulate data items.
• Most databases use the Tree concept as the basis of storing, searching and
sorting its records.
• The Binary search tree holds data items in a sorted order, but with the
addition of a simple rule
Rule: The LEFT node always contain values that come before the root node
and the RIGHT node always contain values that come after the root node.

For numbers, this means the left sub-tree contains numbers less than the
root and the right sub-tree contains numbers greater than the root. For
words, as might be in a sorted dictionary, the order is alphabetic.

14
2/15/2021

Binary Tree: Adding nodes Binary Tree: Adding nodes


• Question: Where would 'larry' and 'tom' be placed in the tree?
Answer: As shown below, the rule is 'larry' alphabetically comes after
the root 'kevin' and so must be in the right sub tree, larry comes before
sam and so must be in the left sub tree below it. Likewise, tom is also in
the right sub-tree but comes after sam, so it must be in a right subtree.
Like this

15
2/15/2021

Binary Tree: Subtracting nodes


• Sam is removed from the tree, re-form the binary search tree.

16
2/15/2021

17
2/15/2021

18
2/15/2021

19
2/15/2021

20

You might also like