예전 자유 게시판

[re] 자료구조와 알고리즘

(자료구조와 알고리즘에 대한 답글입니다.)

류광 2004-06-14 21:06

CD의 binarytree.h에 있는 구현은 좀 이상합니다. 잘못된 버전이 CD에 수록되었거나 또는 제대로 검증을 하지 않고 집어 넣은 것 같습니다.

지적하신 것처럼 재귀 호출 시에 p_process를 넘겨줘야 합니다. 즉:

template <class DataType>
void Preorder( BinaryTree<DataType>* p_node,
               void (*p_process)(BinaryTree<DataType>*) )
{
    // if the node exists
    if( p_node )
    {
        // process the current node
        p_process( p_node );

        // process the left child
        if( p_node->m_left )
            Preorder( p_node->m_left, p_process );

        // process the right node
        if( p_node->m_right )
            Preorder( p_node->m_right, p_process );
    }
}
Inorder, Postorder 역시 마찬가지입니다. 게다가 그 함수들은 Preorder를 호출하고 있는데 역시 오류입니다. 각각 자기 자신을 호출해야 합니다.

그래픽 데모 2는 제대로 작동하는데, 그 데모는 binarytree.h의 운행 함수들이 아니라 독자적인 함수들을 사용합니다. CD \html\demonstrations\ch12\Demo02 - Binary Tree Traversal\GD12-02.cpp 의 In, Post, Pre 함수들입니다.