9
Chapter: Data structures and data manipulation Topic: Static and dynamic data structures, Arrays, Linked list, Binary tree Static data structure A static data structure is one whose size does not change in size during the execution of the program. A typical example is an array. Dynamic data structure A dynamic data structure is one whose size may change in size during the execution of the program. A typical example is a linked list. Advantages Disadvantages Static data structur e It is relatively easy to write code that sets up an array. It allows random (direct) access to data. The compiler can allocate space during compilation. The programmer has to estimate the maximum amount of space that will be needed for a particular problem. The estimated size is not always the correct one. If the array has no data in it, it will still be taking up space in memory. Therefore, it does not make efficient use of the memory. Dynamic data structur e The memory is used efficiently because only the required space of the actual data in the structure is used. Storage no longer needed in the structure can be returned to the system for other It is difficult to program. It allows only serial access to data. R.Sevanandee 2013 Page 1

Array and Linked List

Embed Size (px)

Citation preview

Page 1: Array and Linked List

Chapter: Data structures and data manipulation

Topic: Static and dynamic data structures, Arrays, Linked list, Binary tree

Static data structureA static data structure is one whose size does not change in size during the execution of the program. A typical example is an array.

Dynamic data structureA dynamic data structure is one whose size may change in size during the execution of the program. A typical example is a linked list.

Advantages DisadvantagesStatic data structure

It is relatively easy to write code that sets up an array.

It allows random (direct) access to data.

The compiler can allocate space during compilation.

The programmer has to estimate the maximum amount of space that will be needed for a particular problem. The estimated size is not always the correct one.

If the array has no data in it, it will still be taking up space in memory. Therefore, it does not make efficient use of the memory.

Dynamic data structure

The memory is used efficiently because only the required space of the actual data in the structure is used.

Storage no longer needed in the structure can be returned to the system for other uses.

It is difficult to program. It allows only serial access to

data.

Revision of array from paper1

Arrays: An array stores data in contiguous memory locations.

Initialising an array consists of: Give a name for the array Declare how many items of data are going to be stored in. (size) Declare the data type of the data to be stored.

E.g in Visual BasicDim Myarray (1 to 10) As Integer

Q1. Describe an algorithm to fill the array with data.Q2. Describe an algorithm to search an item in the arrayQ3. Describe an algorithm to add all numbers in the array and display the sum.

R.Sevanandee 2013 Page 1

Page 2: Array and Linked List

Linked list

A linked list is a dynamic data structure used to hold a sequence of items in a particular order, e.g ascending order.Characteristics of a linked list

1. The items are not necessarily held in contiguous data locations2. Each item in the list is called a node and contains a data field and a next address field called a

pointer.3. The data field holds the actual data associated with the list item, the pointer field contains the address

of the next item in the sequence.4. The pointer field in the last node contains a null pointer, indicating the end of the linked list.5. A head pointer points to the first node.

Algorithm to add an item to the list (E.g inserting Hill in the above list)1. Obtain a free node from free list and call it as node NEW

2. Copy data of Hill in node NEW

3. Search the list sequentially until we find a node that should be immediately before the new node, NEW, after insertion. We call the node PREVIOUS

4. Make node NEW points to the node, that is being pointed by node PREVIOUS.

5. Make node PREVIOUS points to node NEW.

R.Sevanandee 2013 Page 2

Page 3: Array and Linked List

Deleting a node from the list. (E.g Fred)

1. Go to the correct Head pointer

2. Search the node to be deleted and called it node FREE

3. Called the node preceding it as node PREVIOUS

4. Make node PREVIOUS points to the node that is pointed by node FREE.

5. Make node FREE points to null

6. Send node FREE to Free list.

Q3. Write an algorithm to search a data item in a linked list.

R.Sevanandee 2013 Page 3

Page 4: Array and Linked List

Binary Tree

A binary tree starts with a root that point to the first node in the tree. Any node may point to two other nodes, one to the left and one to the right.

A. Consider the following binary tree

B. Now, we want to add 17 to the binary tree.

C. So, we start with the root node and called it Current node.

R.Sevanandee 2013 Page 4

Node A

Node CNode B

23

15 28

12 18 24 30

23

15 28

12 18 24 30

23

15 28

12 18 24 30

Page 5: Array and Linked List

D. 17 is less than of the current node (23). We move to the left sub-tree to the next node. So, the current node will be 15.

E. 17 is greater than that of current node (15), we move to the right sub-tree to the next node and make 18 be the current node.

F. 17 is less than that of the current node (18), we move to the left sub-tree.As there is no further node, we create a new node there and insert the data 17.

Q3. Write the algorithm to add an element to a binary tree.

R.Sevanandee 2013 Page 5

23

15 28

12 18 24 30

23

15 28

12 18 24 30

23

15 28

12 18 24 30

17

Page 6: Array and Linked List

Deleting a nodeWhen deleting a node, care must be taken to not lose other pieces of data, mainly below the deleted one.Example: consider the following binary tree

We want to delete data 15.

We have two solutions.Solution 1:

1. We make the node 15 to be deleted to be the root temporarily.2. We traverse the tree and store all the values, except the root, in a different data structures. E.g a stack.

3. We delete the node 15.4. Insert the values in the stack back to the tree.

Solution 2:1. Search the node 152. Set the node as deleted, without removing it completely from the tree.3. When traversing the tree, we ignore nodes which are set as deleted.

Q4. Describe an algorithm to search an item in the binary tree.Q5. Describe an algorithm for using the tree to read the data items in numerical order.

R.Sevanandee 2013 Page 6

23

15 28

12 18 24 30

1812

23

18 28

12 24 30

Page 7: Array and Linked List

Searching method Serial search Binary search

Serial search

Serial search involves looking at the first record or value and see if it is the one being searched. If it is not the one being searched, we check the next records (or values), in turn, until either we find it or we get to the end of the file. When the value is found, we note its position.

This method can be very slow, particularly if there are a large number of values. The least number of comparisons is one; this occurs if the first item is the one you want. However, if there are n values in the list, you will need to make n comparisons if the value you want is the last value. This means that, on average, you look at n/2 values at each search.

Advantages

The list does not need to be sorted. It is very easy to program a serial search. (start at the beginning and compare each record in turn until

you find it)

Disadvantage

There are many comparisons to be done, especially when the list is long. This makes the serial search a very slow method.

Binary search

Binary search is faster searching method than serial search. There are fewer comparisons when the list is long compared to serial search. However, the data should be sorted and it is quite difficult to program.

The principle behind this method is to halve the list each time we do a comparison. Suppose there are 16 values in the list to start with. The successive lists will contain 8, 4, 2 and 1 value. This involves a maximum of 5 comparisons to look for an item.

Q6. The binary search will be applied on the following list of numbers:12,15,16,17,19,23,26,28,29,36,38,39,41,43,46,47,49,50,52,52

(a) List all numbers that will be compared with the search data if the latter is 50.(b) List all numbers that will be compared during the search of number 18.

R.Sevanandee 2013 Page 7