Data Structures Pasco
Data Structures Pasco
Question 1
-----------
128 47 8521 186 774 8975 741 121 100 500 7789 7410
i. Insertion sort(ascending order)
pass 1 = 128 47 8521 186 774 8975 741 121 100 500 7789 7410
pass 2 = 47 128 8521 186 774 8975 741 121 100 500 7789 7410
pass 3 = 47 128 8521 186 774 8975 741 121 100 500 7789 7410
pass 4 = 47 128 186 8521 774 8975 741 121 100 500 7789 7410
pass 5 = 47 128 186 774 8521 8975 741 121 100 500 7789 7410
psss 6 = 47 128 186 774 8521 8975 741 121 100 500 7789 7410
pass 7 = 47 128 186 741 774 8521 8975 121 100 500 7789 7410
pass 8 = 47 121 128 186 741 774 8521 8975 100 500 7789 7410
pass 9 = 47 100 121 128 186 741 774 8521 8975 500 7789 7410
pass 10= 47 100 121 128 186 500 741 774 8521 8975 7789 7410
pass 11= 47 100 121 128 186 500 741 774 7789 8521 8975 7410
pass 12 =47 100 121 128 186 500 741 774 7410 7789 8521 8975
ii.
Radix sort(descending order)
According to one's digit
- 7789 128 47 186 8975 774 8521 741 121 100 500 7410
iii. 3 comparisons
v.
//code for bubble sort
#include <iostream>
int main(){
int n;
cout << "Enter the length of your array ";
cin >> n;
cout << "Enter array values\n";
int numbers_array[n];
for (int i = 0; i < n; i++) {
cin >> numbers_array[i];
}
------------
Question 2
-----------
b.In row major order
A[i][j] = B + W(i*n+j)
Given that = B = 25
i= 3
j = 6
n = 11
W = 2
Therefore
A[3][6] = 25+2(3*11+6)
A[3][6] = 103
d. In the linear linked list, the tail node or the the terminal node points to
nothing
or null whilst in the circular linked list the terminal node points back to
the first
or the head node giving it a ring-like or circular structure.
-------------
Question 3
------------
a. An array is a series of elements of the same
type placed in contiguous memory locations
that can be individually referenced by
adding an index to a unique identifier.
c . i Insertion
With insertion in a linked list data structure, there are 2 cases. Is either
we add the
incoming node to the front the linked list or it is inserted into any other
location.
Case 1
--------
If the incoming
node is supposed to be inserted at the front, the head field of the linked
list class points to the incoming
node(new Node) and then the pointer field of the incoming node points to the
initial head node.
Case 2
---------
But if the it supposed to be inserted at any other position then we go
through the list until the
location reached, we then points the previous node in that location to the
incoming node and then we finally points
the incoming node to the next node of that location.
ii. Deletion
With deletion, there are also 2 cases we need to consider. Is either the node
to be deleted is at the front(head node)
or it can be found at any other location.
Case 1
-------
If the node to be deleted is actually the head node, then all we would do is
to point the head field of the linked list
class to the next or second node
Case 2
-------
If the node is in any other location then we would go through the list until
the node to be deleted is found, if found we then
point the node(previous node) before the one we want it to be delete to the
node which is after the one want to deleted.
d. Insertion in a Queue(Enqueue)
in insertion in a queue, we first check whether the queue is full(thus rear =
max -1 ), if this condition is met, this therefore means
that the queue.
But if this condition is not met then we increase our rear value by one
(since we start our rear value at -1 when the queue is empty),
and then we insert a value at the location using rear as it index
( queue[rear] = data).
We then finally check if the front is -1, if this true we then assign the
front variable to be zero, since the first item has been inserted.
----------
Question 4
-------------
i .a. A leaf in a tree data structure is simply node with no child.
b. A forest is a union of tree data structures where each tree is not connected
to each other
c. Tree traversal is the process of visting(checking or updating) each node in a
tree data structure.
d. Terminal node is node that have its child node to be null(thus ends the
structure) or points to no node.
e. Node is a basic unit of data structures such as linked list, tree data
structure etc which contains a data field, and may or may not have pointer
to the a next node
Level 0 - A
Level 1 - B,C
Level 2 - D,E,F
Level 3 - G,H,I
Level 4 - J
iv. Preorder
A B D E G H C F I J
Post order left right root
D G H E B J I F C A