Serialize ternary tree in c

296 Views Asked by At

I am writing ternary tree for string lookup in c.

So far I am able to store strings and check whether string exists.

Example insert

Node *node_link(Node *this, char *line) {
 26   if(!this) {
 27     this = node_create(*line);
 28   }
 29   if(*line < this->ch) {
 30     this->left = node_link(this->left, line);
 31   } else if(*line == this->ch) {
 32     if(*(++line) == 0) {
 33       this->there = TRUE;
 34       return this;
 35     } else {
 36       this->mid = node_link(this->mid, line);
 37     }
 38   } else {
 39     this->right = node_link(this->right, line);
 40   }
 41   return this;
 42 }

I need to get back the strings that I inserted. For example if I inserted

hat
cat
catpost
tent
tents
square
squarish

I should be able to extract the list with same strings. I do not get it if I do tree traversal (DFS). How can I get the list of stored items?

Example traversal

void node_walk_link(Node *this, char *line) {
 44   if(this) {
 45     node_walk_link(this->left, line);
 46     node_walk_link(this->right, line);
 47     node_walk_link(this->mid, line_push(line, this->ch, 0));
 48   }
 49 }

Also how can find union of two ternary trees? That have items from both trees without duplicates.

0

There are 0 best solutions below