compute_code_table
을 재귀 적으로 시작하면 트리를 쉽게 탐색 할 수 있습니다.
둘째, 모든 작업이나 과제가 특정 작업을 수행하는 방법을 (의사 코드로 또는하지 않고) 설명하는 일부 출처를 온라인으로 검색하는 데 도움이됩니다. 이 경우, 이것은 다음과 같은 설명이 산출 :
당신이 공은 왼손 지점을마다하고, 1 마다하고 출력하기, 당신이 원하는 값으로 트리를 탐색 하프 맨 코드를 생성하는 당신 오른 쪽 가지를 가져 가라. (일반적으로 사용자가 원하는 코드에서 트리를 거꾸로 통과하고 이진 호프만 인코딩 문자열을 역방향으로 작성합니다. 첫 번째 비트는 맨 위부터 시작해야하기 때문에).
siggraph.org C에서
,이는 등으로 구현 될 수있다 :
int compute_code_table_for_node(tree_t tree, node_t target_node, node_t current_node, int code_table) {
// Check for target
if (current_node == target_node) {
// Found target
return code_table;
}
// Check if end-node
if (current_node->left == NULL && current_node->right == NULL) {
// Is an end node
return -1;
}
// Try left
int left = compute_code_table_for_node(tree, target_node, current_node->left, code_table << 1 + 0);
// Try right
int right = compute_code_table_for_node(tree, target_node, current_node->right, code_table << 1 + 1);
// Find which path was taken
if (left == -1) {
// Left path didn't find it, so it must be the right path:
return code_table << 1 + 1;
} else {
// Left path found it
return code_table << 1 + 0;
}
}
그런 다음 당신은 단지
tree
의 모든
node
에 대한
compute_code_table_for_node(tree, node, tree->head, 0)
를 호출해야합니다.
이 코드는 특정 사례에 적용되지 않으므로 다시 작성해야합니다.
답장을 보내 주셔서 감사합니다.하지만 이해할 수 있는지 확실하지 않습니다. typedef에 포인터를 숨기는 것은 무엇을 의미합니까? – Bart
'tree_t' 내 생각 엔 (구조체가 될 수도 있습니다!). 우리가 확실히 알 수 있도록'tree_t'의 정의/typedef를 제공 하시겠습니까! –
아, tree_t의 정의가 주어졌습니다. 질문 : – Bart