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  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. Disadvantages  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.  It is difficult to program.  It allows only serial access to data.

Static data structure

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.

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 Basic Dim 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 array Q3. Describe an algorithm to add all numbers in the array and display the sum. R.Sevanandee 2013 Page 1

The pointer field in the last node contains a null pointer. Algorithm to add an item to the list (E. Characteristics of a linked list 1. R.g ascending order. Make node NEW points to the node. 3. 5. Make node PREVIOUS points to node NEW. Each item in the list is called a node and contains a data field and a next address field called a pointer.Sevanandee 2013 Page 2 . We call the node PREVIOUS 4. that is being pointed by node PREVIOUS. The data field holds the actual data associated with the list item.Linked list A linked list is a dynamic data structure used to hold a sequence of items in a particular order. A head pointer points to the first node. the pointer field contains the address of the next item in the sequence. indicating the end of the linked list. NEW. Obtain a free node from free list and call it as node NEW 2. Search the list sequentially until we find a node that should be immediately before the new node.g inserting Hill in the above list) 1. Copy data of Hill in node NEW 3. The items are not necessarily held in contiguous data locations 2. 4. e. 5. after insertion.

R.Sevanandee 2013 Page 3 . Go to the correct Head pointer 2. Make node PREVIOUS points to the node that is pointed by node FREE. 5.g Fred) 1. Send node FREE to Free list.Deleting a node from the list. (E. Make node FREE points to null 6. Called the node preceding it as node PREVIOUS 4. Q3. Search the node to be deleted and called it node FREE 3. Write an algorithm to search a data item in a linked list.

23 15 28 12 18 24 30 C.Sevanandee 2013 Page 4 . one to the left and one to the right.Binary Tree A binary tree starts with a root that point to the first node in the tree. we want to add 17 to the binary tree. Consider the following binary tree 23 15 28 12 18 24 30 B. 23 15 28 12 18 24 30 R. So. Node A Node B Node C A. Any node may point to two other nodes. we start with the root node and called it Current node. Now.

23 15 28 12 18 24 30 E. 23 15 28 12 18 24 30 17 Q3. Write the algorithm to add an element to a binary tree. 17 is less than that of the current node (18). the current node will be 15.Sevanandee 2013 Page 5 . As there is no further node. We move to the left sub-tree to the next node. R.D. we move to the left sub-tree. 17 is less than of the current node (23). we move to the right sub-tree to the next node and make 18 be the current node. 17 is greater than that of current node (15). we create a new node there and insert the data 17. 23 15 28 12 18 24 30 F. So.

3. except the root. Q4. without removing it completely from the tree. in a different data structures. We have two solutions. mainly below the deleted one. Q5. Insert the values in the stack back to the tree. Search the node 15 2. R. Solution 1: 1.Deleting a node When deleting a node. We traverse the tree and store all the values.Sevanandee 2013 Page 6 . E. Set the node as deleted. care must be taken to not lose other pieces of data. we ignore nodes which are set as deleted. When traversing the tree. 2. Example: consider the following binary tree 23 15 28 12 18 24 30 We want to delete data 15. 23 18 28 12 24 30 Solution 2: 1. 4. Describe an algorithm for using the tree to read the data items in numerical order. 18 12 3. We make the node 15 to be deleted to be the root temporarily. We delete the node 15. Describe an algorithm to search an item in the binary tree.g a stack.

Q6.15.  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.16. If it is not the one being searched.29. It is very easy to program a serial search.52 (a) List all numbers that will be compared with the search data if the latter is 50. However.38.41.28. The least number of comparisons is one.36.Sevanandee 2013 Page 7 . (b) List all numbers that will be compared during the search of number 18. This involves a maximum of 5 comparisons to look for an item. this occurs if the first item is the one you want. The successive lists will contain 8.17.49. The principle behind this method is to halve the list each time we do a comparison. if there are n values in the list. The binary search will be applied on the following list of numbers: 12. the data should be sorted and it is quite difficult to program. you will need to make n comparisons if the value you want is the last value. on average.46. R. we note its position. in turn. This makes the serial search a very slow method. until either we find it or we get to the end of the file. This method can be very slow. When the value is found. Advantages   The list does not need to be sorted. However. especially when the list is long.19. Suppose there are 16 values in the list to start with. (start at the beginning and compare each record in turn until you find it) Disadvantage  There are many comparisons to be done. we check the next records (or values). particularly if there are a large number of values. This means that. Binary search Binary search is faster searching method than serial search. 4.47.52.43. There are fewer comparisons when the list is long compared to serial search.26.50. you look at n/2 values at each search.39. 2 and 1 value.23.