im은 AVL 트리의 임 플리 멘 테이션 작업을하고 있으며, im은 높이 재 계산 기능에 문제가 있습니다. 내가 그것을 트리의 루트와 1의 값을 가진 변수를 통과 할 때 호출하고 그것을 통해 단계별로 한 번 while 루프에 도착하면 예상대로 preform하지만 이후에는 하나만 반환합니다. 당신은 그것을보고 제 잘못을보고 싶습니까? 필요한 경우 더 많은 코드를 게시 하겠지만 함수가 충분한 정보를 제공 할 것이라고 생각합니다. 감사합니다C++ AVL 트리의 높이를 다시 계산합니다.
void BinaryTree::recalculate(Node *leaf, int count)
{
if(leaf == NULL)//if we are at the root
{
return;//exit the function
}
if((leaf->getLeft() != NULL))//if we are not at the end of the subtree
{
recalculate(leaf->getLeft(), count);//advance to the next node and re-enter the function
}
if(leaf->getRight() != NULL)//if we are not at the end of the right subtree
{
recalculate(leaf->getRight(), count);//advance to the next node and re-enter the program
}
else
{
while(leaf->getParent() != NULL)//calculate each subtree until we are at the root
{
leaf = leaf->getParent();//get the parent node
count++;//increment the height
if(leaf->getLeft() != NULL)//if there is an item in the left
{
leaf->getLeft()->setHeight(count-1);//sets the hight of the left child
}
if(leaf->getRight() != NULL)//if there is an item in the right
{
leaf->getRight()->setHeight(count -1);//sets the height of the right child
}
}
return;//exit the function
}
}
count 변수는 각 노드의 높이를 계산할 때 사용하는 것으로, 각 하위 트리에서 가장 낮은 노드를 찾은 다음 거기에서 이동할 수 있기를 원합니다. 하지만 그것은 작동하지 않는 것 같습니다. – compprog254
이미 노드에 저장되어있는 노드의 현재 높이를 다시 계산하지 않아도됩니다. 트리를 회전시켜야 할 경우 노드의 위치가 바뀌고 그 일을하는 방식이 엉망입니다. 메신저 아프다고 생각하고 대신 루프를 사용하십시오. – compprog254
대부분의 시간 동안 전체 트리가 한 노드 주위로 이동하므로 예 트리에서 각 노드의 높이를 계산하려고합니다. – compprog254