virtual tree_ptr finalize_tree(tree_ptr tree) const { std::queue<node_ptr> node_queue; node_queue.push(tree->get_root()); while (!node_queue.empty()) { auto current_node = node_queue.front(); node_queue.pop(); if (current_node->is_leaf()) { // Check nodes such as {E, F}, which can be broken without any more info if (current_node->get_size() == 2) { auto broken_color = current_node->get_data().break_into_parts(); auto left = broken_color[0]; auto right = broken_color[1]; current_node->set_left_child(std::make_shared<node_t>(left)); current_node->get_left_child()->set_parent(current_node.get()); current_node->get_left_child()->set_name( cfg::get().mcolor_to_name(current_node->get_left_child()->get_data())); current_node->set_right_child(std::make_shared<node_t>(right)); current_node->get_right_child()->set_parent(current_node.get()); current_node->get_right_child()->set_name( cfg::get().mcolor_to_name(current_node->get_right_child()->get_data())); } } else { node_queue.push(current_node->get_left_child()); node_queue.push(current_node->get_right_child()); } } return tree; }
void insert_encoded_node(huff_node *root, huff_node *node){ int length = strlen(node->encoding); char *encoding = node->encoding; huff_node *current = root; for(int i = 0; i < length; i++){ if(encoding[i] == '0'){ if(current->left_child == 0){ if(i == length-1){ current->left_child = node; return; } else{ huff_node *left_child = create_huff_node(-1, -1,create_string_from_cat(current->encoding, "0")); set_left_child(current, left_child); current = left_child; continue; } } else{ current = current->left_child; } } //next character is a 1 else{ if(current->right_child == 0){ if(i == length-1){ current->right_child = node; return; } else{ huff_node *right_child = create_huff_node(-1, -1,create_string_from_cat(current->encoding, "1")); set_right_child(current, right_child); current = right_child; continue; } } else{ current = current->right_child; } } } }