이진 트리의 최대 및 최소 요소를 찾는 함수를 구현했습니다. 그러나 나는 그것을 잘못 산출하고있다.이진 트리의 최소 요소
이진 트리의 최대 값을 찾는 함수.
int FindMax(struct TreeNode *bt)
{
//get the maximum value of the binary tree...
int max;
//get the maximum of the left sub-tree.
int left;
//get the maximum of the right sub-tree.
int right;
//get the root of the current node.
int root;
if(bt!=NULL)
{
root=bt->data;
//Call the left tree recursively....
left=FindMax(bt->leftChild);
//Call the right tree recursively...
right=FindMax(bt->rightChild);
if(left > right)
{
max=left;
}
else
{
max=right;
}
if(max < root)
{
max=root;
}
}
return max;
}
이진 트리의 최소값을 찾는 함수.
int FindMin(struct TreeNode *bt)
{
//get the minimum value of the binary tree...
int min;
//get the minimum of the left sub-tree.
int left;
//get the minimum of the right sub-tree.
int right;
//get the root of the current node.
int root;
if(bt!=NULL)
{
root=bt->data;
//Call the left tree recursively....
left=FindMin(bt->leftChild);
//Call the right tree recursively...
right=FindMin(bt->rightChild);
if(left < right)
{
min=left;
}
else
{
min=right;
}
if(min > root)
{
min=root;
}
}
return min;
}
출력 : 나무 32767
트리 0
나무가 균형을 잡지 못합니다. 그러나 수표를 제공하고 분 초기화에 도움을 주신 것에 감사드립니다. –
트리가 불균형 일지라도 이것은 여전히 최악의 경우 (트리가 퇴보 한 역방향 목록 인 곳)에서 개선되거나 적어도 동등하며 보너스는 작동해야한다는 것입니다. – paddy
올바르지 않습니다. 이것은 BINARY TREE가 아닌 BINARY SEARCH TREE에 해당합니다. 이진 검색 트리는 가장 왼쪽 노드로 이동하여 분을 가져올 수있는 방식으로 구성됩니다. 그것은 이진 트리에서 반드시 필요한 것은 아닙니다. – ohbrobig