-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME
37 lines (26 loc) · 1.54 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Sparse_Array
This project aims to implement the sparse array in C.
Sparse array is defined as a data structure that is sparsely filled. We want to be able to access the array via indices. And the complexity of indexing should be constant.
Design:
Trie is used. Assume the index is by a unsigned int array (32-bit). Therein, each 8 bits are used for selecting the nodes in next level. e.g. bits 0-7 are used to select among the 256 children of root, bits 8-15 are used to select among the children of next level.
The data structure is designed as follow:
struct Trie{
union Content {
struct Trie **children_;
int value_;
}content_;
};
There will be 5 functions designed (pseudocode) as follows:
construct_trie_( ) : constructor function
=== create the root node of the trie and initialized the pointers to NULL.
destruct_trie_( struct Trie * ) : destruct all dynamically created variables
=== post order traversal and free the dynamically allocated memory.
insert_( struct Trie *, index, value ) : insert or update the value at the provided index
=== check the passed in parameters
=== check from the root (level 0) to the corresponding leaf. if any node is not
=== created, create it (malloc). if the node has no children, create 256 children
=== and assign the pointer to the pointer (trie ** children).
get_( struct Trie *, index ) : return the value at the provided index
=== check the exsitance of element, if existed, return the value, otherwise, return -1
iterate( struct Trie * ) : iterate through the sparse array and print the elements
============