Chapter 8-Data Structures and Algorithms
Chapter 8-Data Structures and Algorithms
Page 1 of 22
Advantages of Dynamic Data Structures
Only uses the space that is needed at any time
Makes efficient use of the memory, no spaces lie idle at any given
point
Storage no-longer required can be returned to the system for
other uses.
Does not allow overflow
There is effective use of resources as resources are allocated at
run-time, as they are required.
The last option modifies the starting value and the ending value of the
Page 2 of 22
indices, but the number of elements still remains the same.
In the above declaration, the array index starts from 0 to 4 and are
integer values. The memory locations will be as follows:
The five individual locations are Names (0), Names (1), Names (2),
Names (3) and Names (4).
Each data item is called an element of the array. To reference a
particular element one must use the appropriate index.
NB: However, most programming languages differ with Microsoft Visual basic
in handling arrays, especially on the amount of memory allocated. For
example, using Java, the following declaration:
Int [4 ]Names;
This array declaration creates exactly 4 memory spaces for the array Names.
The indices of the array range from 0 to 3 which are
Names[0], Names[1], Names[2] and Names[3]
Page 3 of 22
- Size of array is calculated
- Location of array is decided according to data type and size
- Locations are reserved for the array
- Size of array is stored in a table
- Lower bound of the array is stored in a table
- Upper bound of array is stored in a table
- Data type is stored in a table
- Address of first element is stored in a table
One-dimensional arrays
A one-dimensional array is a data structure in which the array is
declared using a single index and can be visually represented as a
list.
0 Theresa
1 Lameck
2 Johanne
3 Laurence
4 Fadzai
Page 4 of 22
Two-dimensional arrays
A two-dimensional array is a data structure in which the array is
declared using two indices and can be visually represented as a
table.
Indices 0 1 2 3
0 Makombe Tinashe M 4A
1 Vheremu Alex M 4B
2 Mununi Mary F 3C
3 Chirongera Salpicio M 2C
4 Mutero Violet F 4C
Initialising an array
Initialising an array is a procedure in which every value in the array
is set with starting values – this starting value would typically be “”
for a string array, or 0 for a numeric array.
Page 6 of 22
Sorting Array Elements
Dim y As Integer
Dim p As Integer
Dim z As Integer
For y = LBound(Names) To UBound(Names)
For p = LBound(Names) To UBound(Names) - 1
If Names(y) < Names(p) Then
z = Names(y)
Names(y) = Names(p)
Names(p) = z
End If
Next p
Next y
The above algorithm will shift elements of the array up (or left),
removing the first element and then completely removing the last
element in the array.
Page 7 of 22
The following is an alternative method of deleting an array element:
The algorithm first searches the element to delete, and then remove it
from the array.
NB:
If the item is string, it replaces with empty spaces. However, if it is
numeric, it replaces with a 0.
Deleting form an array is often difficult as elements need to be
shifted positions after deletion. It is an effective method for
deleting elements.
Page 8 of 22
This allows the user to create the array when he/she actually needs it,
using a ReDim statement: Dynamic arrays can be re-created at will, each
time with a different number of items. When you re-create a dynamic
array, its contents are reset to 0 (or to an empty string) and you lose the
data it contains. If you want to resize an array without losing its
contents, use the ReDim Preserve command:
-
The data items are held in nodes.
The possible routes are called paths/branches. They are lines
connecting the nodes.
Each node has two possible paths.
The nodes are arranged in layers.
The first node is called the root, or root node. Each tree has only
one root node. However, each branch can have its branch root.
Node created by another one is called child node (children)
Each child node has only one parent node
Each parent node has at most two children
Page 9 of 22
The last node is called the leaf node/terminal node (has no
children)
Nodes that share common parent are called siblings
For example, given the following numbers: 20, 30, 5, 2, 7, 6, 17, 58, 41
Placing them in the binary tree is as follows:
- The first element becomes the root node, i.e. 20
- For other numbers, the bigger number goes to the right and the
smaller one to the right of a node. Every time start from the root
node, until you get to an empty space to place the new node.
- For example, 30, is bigger than 20, therefore is placed to the right
hand side of 20. There is nothing on this side and therefore a new
node is created and 30 placed inside.
- Next is 5, which is smaller than 20 (root node) and therefore goes
to the left. There is an empty space therefore a new node is
created and 5 is placed inside.
- Then 2 is smaller than 20 (root node) and therefore goes to the
left. On the left there is 5. 2 is smaller than 5, therefore we go to
the left and place 2 there.
- Next is 7, which is smaller than 20, we go to the left where there is
5. Seven (7) is bigger than 5, therefore we place it to the right of 5.
- ……….finish on your own!!!!!!!!!!!
Page 10 of 22
Follow left pointer
Endif
Until pointer =0
Place item at this node
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.
B. In-Order Traversal
The order of traversal is:
- Traverse the Left sub-tree
- Visit the Node
Page 11 of 22
- 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.
C. Post-Order Traversal
The order of traversal is:
- Traverse the Left sub-tree
- Traverse the Right sub-tree.
- Visit the Node
Page 12 of 22
If Tree(p).Right <> 0 Then
traversefrom(Right)
endif
Print (data);
endProcedure
Page 13 of 22
The value at the node is not only data, it is part of the structure of
the tree
If the node is simply deleted then the sub-tree leading from it is
not navigable
*NB: the algorithm to delete a leaf node is straightforward as deleting a
leaf node does not change the structure of the tree
To remove the value from the tree, either:
It remains in the tree structure
Mark value as deleted so that it cannot be output but acts as the
root for its sub-tree
OR:
The entire sub-tree without its root is read to a list
The sub-tree is deleted
The values in the list are read back into the tree (element which
was originally on the left will replace the deleted element
(becomes root of that branch))
Page 14 of 22
IMPLEMENTATION OF BINARY TREES USING ARRAYS
Binary trees can be implemented using left and right pointers for each
node. Each node will have the following:
- Left pointer
- Right pointer
- Data item
Let’s take the binary tree below:
Page 15 of 22
These can be illustrated as follows:
Left Data Right
Tree [1] 2 Long 5
Tree [2] 0 Charlesworth 3
Tree [3] 4 Illman 7
Tree [4] 0 Hawthorne 0
Tree [5] 8 Todd 6
Tree [6] 0 Youngman 0
Tree [7] 0 Jones 0
Tree [8] 0 Ravage 0
- The pointer value 0 indicates a ‘nil’ pointer, thus a node will be
pointing to nothing.
- Tree[1].Left = 2
- Tree[Tree[1].Left].Right = 3
- Tree[6].Data = “Youngman”
Infix Expressions
Normal mathematical expressions are written as follows:
A+B
This method is called Infix Notation, because the operator is found
between the operands to be acted upon. Infix notation involves the use
of brackets and observes operator precedence, e.g. BODMAS/BOMDAS.
For example, the expression below is an Infix:
(A+B)*C+(D-A)
If there are no brackets, the expression will give a different answer. It is
not easy for the computer to evaluate infix expressions.
Prefix Expressions
The Polish Notation is also called the prefix. In Prefix (Polish) notation,
the operator precedes the operands. For example, the infix expression
A + B is given as: +AB
This has an advantage that it removes ambiguity and avoids use of
brackets
Page 16 of 22
Postfix Expressions
Reverse Polish Notation (Postfix) is a way of writing mathematical
expressions without using parenthesis and brackets, and in which the
operands precede the operator. Reverse Polish Notation is also called
Postfix. For example, the Infix expression A + B is written as follows in
Postfix:
AB+
Likewise, the infix expression (A+B)*C+(D-A) is written as:
AB+C*DA-+
The first procedure is to put elements into a binary tree, with each
operator or operand being a node on its own.
To do this, we look at operator precedence. The operator with the
lowest / weakest precedence becomes the root node. When the
weakest operator is identified, it is placed as the root node.
Expressions on the left hand side of this weakest operator makes up the
left sub tree, while those on the right hand side makes the right sub-
tree. This procedure is applied as we move down the tree.
Page 17 of 22
- There are no more items to the left branch of the root node. We
therefore go to the right side.
- The weakest operator to the right of = is +, therefore it becomes
the first node to the left of the root.
- To the left of the + sign, the weakest sign is the *, which we place
as the node to the left of +
- To the left of * there is A, which becomes a node.
- To the right of * there is B, which becomes a node.
- We are now done with the left branch of the + node, lets move to
the right side.
- The weakest sign is /, it becomes a node there.
- To its left there is C, which becomes a node.
- To the right of / there is D, which also becomes a node.
- Thus all the items are now in a binary tree , which appears as
follows:
Page 18 of 22
Given the following infix notation: (a+b) – c*(d-e)
This can be represented using a tree as follows:
Using the above tree, the reverse polish notation will be as follows;
ab+cde-*-
Page 19 of 22
First a (5) is pushed into the stack
b (3) is pushed into the stack
an operator is encountered, therefore, b (3) is popped and placed
on the right
a (5) is popped and placed on the left
the operator (+) is placed in between and effected
the result (8)is pushed back into the stack
c (2) is pushed into the stack
d (6) is pushed into the stack
e (4) is pushed into the stack
- sign is encountered. Thus e (4) is popped and placed on the right
e (6) is popped and placed on the left, to give (d-e)
Operator (-) is effected
The result (2) is pushed back into the stack.
Another operator is encountered(*)
Result (2) is popped
C (2) is popped and operator effected
The result (4) is pushed back to the stack.
Operator (-) again is encountered
Result(4) is popped
Another result (8) is popped and operator effected
Result (4) is pushed into the stack
There are no more operators. The answer is displayed.
Questions
1. (a) State the difference between dynamic and static data structures giving an example of
each. (3)
b) Show how a binary tree can be used to store the data items Feddi, Eda, Joh, Sean, Dav,
Gali in alphabetic order. (4)
c) Explain why problems may arise if Joh is deleted from the tree and how such problems
may be overcome.
2. An array is to be used to store information. State three parameters that need to be given
about the array before it can be used, explaining the reason why each is necessary.
Page 20 of 22
3. (a) Explain the difference between static and dynamic data structures. [2]
(b) Give an example of a
(i) static,
(ii) dynamic
data structure, giving an advantage of each. [4]
(c) The details of a car part are stored in a binary tree according to this algorithm
READ VALUE NEW_PART
START AT ROOT NODE
WHILE NODE NOT EMPTY, DO
IF NEW_PART < VALUE AT NODE
THEN FOLLOW LEFT SUBTREE
ELSE FOLLOW RIGHT SUBTREE
ENDIF
ENDWHILE
INSERT NEW_PART AT NODE
END
(i) Show the binary tree after the following values have been input
Radio Visor Brakes Tyres Alternator Windscreen [3]
(ii) Explain how Clutch is added to the tree in (i). [5]
(iii) Describe an algorithm that can be applied to the binary tree of car parts, so that the
tree is read in alphabetic order.
4.
The following binary tree diagram contains a number of integers. In each case the right
pointer indicates the condition “higher number” and the left pointer indicates the
condition “lower or equal number”.
(ii) Write down the order in which the nodes would be accessed to find the integer 2528.
[1]
(iii) Copy the tree and show where a new node containing the integer 3106 would be
added. [1]
Page 21 of 22
(iv) The integer 2550 is not in the diagram. Explain what would happen if a search was
made for this code.
Page 22 of 22