I am doing my homework which requires me to implement a Trie Tree without using vector. I have a struc
defined as following:
typedef struct{
char _name;
int32_t * _children;
int32_t _children_size;
int32_t _children_capacity;
int32_t * _location;
int32_t _location_size;
int32_t _location_capacity;
} TrieTreeNode;
To reduce the memory use, I store all the pointers of TrieTreeNode
into a global variable TrieTreeNode ** nodes_array
. Then the _children
member of each TrieTreeNode
is just an array whose elements are int32_t
indices to nodes_array
.
For example, say we have TrieTreeNode * parent
. To access its first child, we use nodes_array[parent -> _children[0]]
.
My question is how to delete the whole Trie Tree? I have tried the following approach (tail
is the number of pointers nodes_array
has):
void delete_node(TrieTreeNode *node){
delete [] node -> _children;
delete [] node -> _location;
}
void delete_tree(){
for (int i = 0; i < tail; i++){
delete_node(nodes_array[i]);
}
delete [] nodes_array;
nodes_array = NULL;
}
However, when I used both -ps -l
command and GDB to monitor the memory use of my program before and after deleting a tree, the memory only decreases a little bit. The RRS goes from 13744 to 13156, while it is only 1072 before I build the tree.
Any suggestions will be appreciated!