0% found this document useful (0 votes)
10 views64 pages

5.algorithm Design and Data Structures

Uploaded by

Tanatswa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views64 pages

5.algorithm Design and Data Structures

Uploaded by

Tanatswa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 64

Data Structures and

Algorithms
A Basic Understanding
Objectives
 Discuss the concept of Algorithms
 Provide a brief background of the concept of

data structures
 Show how Data Structures can be classified
 Discuss the following Data Structures:

◦ Arrays
◦ Binary Trees
Algorithm
 Step by step procedure designed to perform an
operation, and which (like a map or flowchart) will lead
to the sought result if followed correctly.
 Algorithms have a definite beginning and a definite
end, and a finite number of steps.
 An algorithm produces the same output information
given the same input information, and several short
algorithms can be combined to perform complex tasks
such as writing a computer program.
 A cookbook recipe, a diagnosis, a problem solving
routine, are some common examples of simple
algorithms.
Algorithm Continued
 Algorithms are commonly specified using
pseudocode
 It’s simply an implementation of an algorithm

in the form of annotations and informative text


written in plain English.
 It has no syntax like any of the programming

language and thus can’t be compiled or


interpreted by the computer.
 Its subjective and non-standard (but it is

common practice to incorporate programming


syntax)
Algorithm Continued
 To write pseudocode all you do is write down the
required steps in a human understandable
language (English)
 e.g. Produce pseudocode for a program accepts
two numbers, calculate their sum and display the
result:
◦ Start
◦ Accept first number
◦ Accept second number
◦ Add the two numbers
◦ Display the result
◦ Stop
Pseudocode Constructs
 Although pseudocode is independent of any
programming language, it adopts the
traditional programming control structures
 Selection and Iteration
 These constructs are however represented in

simple English
 The syntax of the control structures is as

follows:
Pseudocode :Selection
 It uses the if..else statement and the syntax is as
follows:
◦ If condition action
else
action

◦ The statements can be nested:


 If condition
action
else
if condition
action
Pseudocode :Iteration
 Uses loops

◦ while condition
<statement(s)>
◦ e.g
start
initialize count to 0
while count is less than 0
add 1 to count
display count
end while
stop
Pseudocode :Iteration
 Uses loops

◦ Do <statement(s)>
while condition

◦ e.g
start
initialize count to 0
Do
add 1 to count
display count
while count is less than 0
stop
Algorithm Continued
 An algorithm has the following characteristics
◦ It is a sequence of instructions
◦ Each instruction is clearly specified (precise)
◦ The instructions are logical in terms of the order of
instruction
◦ It must finite
◦ It has a beginning and ending
◦ IT is independent of any programming language
Algorithm Continued
 Other structure constructs can also be used
e.g.
◦ For loop
◦ Repeat until
◦ Case (multiway selection)
What is a Data Structure
 a particular way of organizing data in a computer so that
it can be used efficiently.

 a specialized format for organizing and storing data


designed to organize data to suit a specific purpose so that
it can be accessed and worked with in appropriate ways.

 a way organizing data in such a way that we can perform


operations on these data in an effective way.

 a systematic way to organize data in order to use it


efficiently
What is a Data Structure
 Data Structure does not represent only data in
memory but it also represents the relationship
among the data that is stored in memory.

 Data Structures is about rendering data elements in


terms of some relationship, for better organization
and storage.

 It represents the knowledge of data to be


organized in memory, designed and implemented
in such a way that it reduces the complexity and
increases the efficiency.
Representing a Data Structure
 Abstract Data Type
◦ represents the set of operations that a data
structure supports.
◦ only provides the list of supported operations, type
of parameters they can accept and return type of
these operations.

 Implementation
◦ provides the internal representation of a data
structure. Implementation also provides the
definition of the algorithms used in the operations
of the data structure.
Operations on a Data Structure
 Data Structure allows us to manipulate data by
specifying a set of values, set of operations that can be
performed on set of values and set of rules that needs
to be followed while performing operations.

 The following are some of the operations that can be


carried out on Data Structures:
 Traversal
 Insertion
 Deletion
 Searching
 Sorting
 Merging
Characteristics of a Data Structure
 Every Data Structure is characterized by the
following:
 Type of data that it is going to store.
 Operations that can be carried out on the data
stored.
 The rules to be observed while carrying out the
operations
Classification of Data Structures
 Data Structures can be classified according to
a number of criteria of interest.
 One common classification is whether the

structure is created by the user or provided


for in the language specification standard of
the implementation language
Classification of Data Structures
Classification of Data Structures
Characterstic Description

Linear Data items are arranged in a linear sequence. Example: Array

Non-Linear Data items are not in sequence. Example: Tree, Graph

Homogeneous All the elements are of same type. Example: Array

Non-Homogeneous The elements may or may not be of the same type.


Example: Structures

Static Static data structures are those whose sizes and structures
associated memory locations are fixed, at compile time.
Example: Array

Dynamic Dynamic structures are those which expands or shrinks depending


upon the program need and its execution. Also, their associated
memory locations changes. Example: Linked List created using
pointers
Examples of Data Structures
Built in Data Structures
DATA TYPES
 Data type is a way to classify various types of data such as
integer, string, etc. which determines the values that can
be used with the corresponding type of data, the type of
operations that can be performed on the corresponding
type of data.

 There are two data types :−


 Built-in Data Type
 Derived Data Type
BUILT IN DATA TYPES
 Integer
◦ Integer Data Structure is used to represent numbers without
decimal points.
For Example: 23, -45, 11 etc… Using this Data Structure we can
represent positive as well as negative numbers without decimal
point.

 Real/Float
◦ Float Data Structure is used to represent numbers having decimal
point.
For Example: 3.2, 4.56 etc…

 Character
◦ Character is used to represent single character enclosed between
single inverted comma.
It can store letters (a-z, A-Z), digit (0-9) and special symbols.
BUILT IN DATA TYPES
 String
◦ A string is used to represent numbers a group of
characters
For Example: John , two, num….. etc

 Boolean
◦ can only represent one of two states, either
true or false.
Examples of Data Structures
User Defined Data Structures
Arrays
 Array is a container which can hold a fix number of items
and these items should be of the same type.

 Array is a collection of variables of same data type that


share a common name.

 Array is an ordered set which consist of fixed number of


elements.

 Array is an example of linier data structure.


In array memory is allocated sequentially so it is also
known as sequential list.

Arrays
 Following are the important terms to understand
the concept of Array.
 Element − Each item stored in an array is called
an element.
 Index − Each location of an element in an array
has a numerical index, which is used to identify
the element.
Arrays: A conceptual View
Arrays: Declaration
 Depends on the language of implementation
 E.g VB.NET
 Dim MyNumbers(4) As Integer

 NB:
You might be thinking that the array was set to
the number 4 but always remember that an
array starts counting at zero, and the first
position in your array will be zero.
Arrays: Traversal
 The method of processing each element in
the array exactly once is known as Traversal.
 Array Traversal always starts from first

element in the array an ends at the last


element of the array.
Arrays: Insert an Element
 The Insertion operation is used to insert a new element at
specific position in to one dimensional array.

 Specify the position by way of the index and use an


assignment operator to the position of interest

 In order to insert a new element into one dimensional


array we have to create space for new element.

 Suppose there are N elements in an array and we want to


insert a new element between first and second element.
We have to move last N-1 elements down in order to
create space for the new element.
Arrays: Sorting
 The process of organizing or arranging the
array elements into some predetermined
order.
 The order can be ascending in which case the

following is true
◦ A<B, b<C, C<D …
 The order can be descending in which case
the following is true
◦ A>B, B>C, C>D …
Arrays: Sorting
 The process can be achieved in a number of
ways:
◦ Bubble Sort
◦ Quick Sort
Bubble Sort
 Repeatedly compares each pair of adjacent
elements in the array and swap them if they
are out of order (Depending on the sort
criteria)
 i.e 1st and 2nd are compared and swapped if

necessary
 2nd and 3rd are compared and swapped if

necessary
 Process continues until the last two elements

are compared. (Pass is complete)


Bubble Sort
 When the first pass is complete, the algorithm
returns to the 2nd element and repeats the whole
process again.
 Several passes have to be made through the array
before it is finally sorted.
 The process only stops when the entire array is
examined and no swaps are necessary (sorted)
 After the first pass the 1st element will be in its
proper place
 If the process is repeated n-1 times (where n is the
number of elements to be sorted) then the array
will be sorted.
Bubble Sort:Example
84 69 76 86 94 91

84 76 86 94 91 69

84 86 94 91 76 69

86 94 91 84 76 69

94 91 86 84 76 69

94 91 86 84 76 69
Bubble Sort:Example
84 69 76 86 94 91

84 76
69 86
76 94
86 91
94 69
91

84
84 86
69 94
76 91
86 76
94 69
91

86 94 91 84 76 69

94 91 86 84 76 69

94 91 86 84 76 91
Arrays: Searching
 The process of analyzing the contents of an array to
determine whether an element of interest is present or not.

 Locating a particular element in a given array.

 The item being searched for is typically referred to as a


search key.

 A search typically answers with a true or false as to whether


the element is present or not

 Can be modified to give the index of the location of the


value in the array.
Arrays: Searching
 The process can be achieved in a number of
ways:
◦ Sequential Search
◦ Binary Search
Sequential Searching
 The simplest method of searching
 Compares the search key to each and every

element in the array from the beginning until


either a match is found or until the array
elements are exhausted.
 E.g

0 4 3 2 1 8

 Searh key = 1
Sequential Searching: The Algorithm
Begin
Initialize a Boolean variable to false
Compare the first element to the search key
If there is no match
get the next key and compare again
Otherwise
change the Boolean variable to true
If the Boolean variable is true
return “found”
Otherwise
return “not found”
End
Binary Searching
 A more efficient searching technique
 Only works on an already sorted array
 Uses the divide and conquer approach
 The idea is to compare the search key to the

middle element of the sorted array. If there is no


match and search key is smaller then the left half
of the sorted array will be searched otherwise the
right half will be sorted (Using the same
strategy).
 NB: The middle element is obtained by adding

the index of the first element to the index of the


last element in the array and dividing the result
by two:
Binary Searching: The Algorithm
 Begin
 Assign the index of the first element as the lower

limit
 Assign the index of the last element as the upper

limit
 Calculate the index of the middle element in the

array using the formular: (lower+upper)/2


 While not found and lower is less than upper
 Examine/check the middle element
 If the middle element = the search key
element has been found
Binary Searching: The Algorithm
 Otherwise if search key is > middle element
Search the right side
 Otherwise search the left side
End

e.g

3 4 8 9 11 12 13

Search key =12


Binary Searching:Example
 lower=0
 Upper=6
 Middle=(0+6)/2=3
0 1 2 3 4 5 6

L M U
 Middle element (9) is less than search key (12)
 Lower=middle+1(3+1)=4
 Upper =6
 New middle =(4+6)/2=5
Binary Searching:Example
 lower=4
 Upper=6
 Middle=(4+6)/2=5

0 1 2 3 4 5 6

L M U
 Middle element (12) = search key, therefore

element has been found!!!


Binary Searching: Practice
6 9 14 15 16 18 20

 i) Seach key=9
 ii)Search key=20
 iii) Search key =21
Trees
ATree is defined as a finite set of one
or more nodes such that:
◦ There is a special node called root
node having no predecessor.
◦ All the nodes in a tree except root node
having only one predecessor.
◦ All the nodes in a tree having 0 or
more successors.
Consider Following Figure:
Trees
Binary Trees
 A binary tree is a finite set of elements that is either
empty or is partitioned into three disjoint subsets. The
first subset contains a single element called root of the
tree.
 The other two subsets are themselves binary trees, called
left and right sub trees of the original tree. A left or right
sub tree can be empty and each element of the binary
tree is called as the node.
 A tree whose elements have at most 2 children nodes.
 Since each element in a binary tree can have only 2
children, we typically name them the left and right child.
 One common implementation of the Binary Tree concept
is the Binary Search Tree.
Binary Trees
 A Binary Search Tree is a tree in which nodes are
arranged such that all the nodes in a left sub tree
having values less then root node and all the nodes
in a right sub tree having values greater than root
node.

 Thus we can say that Binary Search Tree is an


ordered binary tree.
The following Figure represents Binary Search Tree:
Binary Search Trees
Binary Search Trees
Properties

 All the nodes in a left sub tree having values less


then root node.

 All the nodes in a right sub tree having values


greater then root node.
Binary Search Trees Operations
SEARCHING

 Inorder to search a node in a binary


search tree we have to follow the
steps given below:
◦ Step 1: First we have to check weather
binary search tree is empty or not. If
binary search tree is empty then search is
unsuccessful.
Step 2: If binary search tree is not empty then we
compare the value of a node to be searched with
root node of binary search tree. If both values
are equal then search is successful otherwise we
have two possibilities.
(A) If value of the node to be searched is
less than the value of root node then we
have to search node in left sub tree of root
node.
(B) If value of the node to be searched is
greater than the value of root node then we
have to search node in right sub tree of
root node.
Step 3: Step is repeated recursively until node to be
searched is found or all the nodes in a binary search tree
are compared with the node to be searched.
Binary Search Trees Operations
 INSERT A NODE
To insert a node in a tree you compare to the root, if its
less you insert the node to the left and if its more you
insert to the right

Suppose we want to construct binary search tree for


following set of data:

45 68 35 42 15 64 78
Binary Search Trees Operations
Binary Search Trees
 Existing tree
Binary Search Trees
Existing tree
NB
◦ Start at the root and recursively go down the tree
searching for a location in a BST to insert a new
node.

◦ If the element to be inserted is already in the tree,


we are done (we do not insert duplicates).

◦ The new node will always replace a NULL reference.


Binary Tree Traversal
Tree traversal refers to means of walking through the tress structure
such that each node is visited once. Traversal of trees is a recursive
function-they call themselves. Common traversal methods are: Pre-
Order, In-order and Post-Order Traversals.

A. Pre-Order traversal
The order of traversal is:
- Visit the Node
- Traverse the Left sub-tree
- Traverse the Right sub-tree.
This is generally given as NLR
For the diagram above, the pre-order traversal will be as follows:
20, 5, 2, 7, 6, 17, 30, 58, 41.
The algorithm for pre-order traversal is as
follows:
1. Print current node
2. For the current node, check if there is left sub-tree
3. If there is left sub-tree, go to the root node of this
sub-tree and print it
4. For the current node, check if there is right sub-
tree
5. If right sub-tree is present, then go to 6, else go to
7
6. Repeat 1
7. End
B. In-Order Traversal
The order of traversal is:
- Traverse the Left sub-tree
- Visit the Node
- Traverse the Right sub-tree.
This is generally given as LNR
For the diagram above, the pre-order traversal will be as
follows:
2, 5, 6, 7, 17, 20, 30, 41, 58.

NB: In-order traversal prints items in ascending order


or in alphabetical order if they are alphabetic items.
In-order traversal algorithm:
1. For the current node, check if there is left-sb-tree.
If it exists, go to the root node of this sub-tree and
then go to 2. If it doesn’t exist, go to 3.
2. Repeat 1
3. Print the current node
4. For the current node. Check whether it has a right
sub-tree. If it has, go to 5. Else go to 6
5. Repeat 1
6. End
C. Post-Order Traversal
The order of traversal is:
- Traverse the Left sub-tree
- Traverse the Right sub-tree.
- Visit the Node

This is generally given as LRN


For the diagram above, the pre-order traversal will
be as follows:
2, 6, 17, 7, 5, 41, 58, 30, 20.

You might also like