簡體   English   中英

查找 BST 的最大值和最小值

[英]Find max and min value of BST

您好 stackoverflowers,我在 C 中的 function 遇到問題,我想創建一個 function 中的最小值和最大值問題是當我使用這個 function 它返回相同的最小值和最大值:

void Find_Min_Max(node *bt,int* maxint,int* minint)
{
    node *tmp = bt;
    if( bt == NULL)
    {
        *maxint = 0;  // Only if the tree contains nothing at all
        *minint = 0;  // Only if the tree contains nothing at all
    }
   if( bt->left)
       return Find_Min_Max(bt->left,&(*maxint),&(*minint));
   *minint = bt->data;
   if( tmp->right)
       return Find_Min_Max(tmp->right,&(*maxint),&(*minint));
   *maxint = tmp->data;
}

但是當我用它給我一個最大/最小的結果時,我刪除了這部分代碼,一切正常:

if( tmp->right)
    return Find_Min_Max(tmp->right,&(*maxint),&(*minint));
*maxint = tmp->data;

知道這將如何工作嗎? 先感謝您。

在同一個函數中同時遞歸計算最大值和最小值並不容易/直觀。 我什至會說這是不可能的,因為這是兩種完全不同的遍歷。

您應該有一個獲取最小值的函數,一個獲取最大值的函數,並在Find_Min_Max調用它們中的每一個。

這將是一種可能的方法:

int find_min(node *n) {
    if (n == NULL) {
        return 0;

    }
    while (n->left != NULL) {
        n = n->left;
    }
    return n->data;
}

find_max類似,但只遍歷正確的鏈接:

int find_max(node *n) {
    if (n == NULL) {
        return 0;
    }
    while (n->right != NULL) {
        n = n->right;
    }
    return n->data;
}

然后, find_min_max()很容易編碼:

void find_min_max(node *bt, int *min, int *right) {
    *min = find_min(bt);
    *max = find_max(bt);
}

find_min()find_max()可以是遞歸的,但迭代方法具有使用常量內存的理想特性(從而避免堆棧溢出)。

要在 BST 中找到最小值,您可以從根開始沿着左孩子鏈直到到達沒有左孩子的節點。 該節點包含最小值(即使它確實有一個右孩子)。

尋找最大值的算法正是鏡像:沿着右孩子鏈直到到達沒有右孩子的節點。 該節點包含最大值。

嘗試同時執行兩種遍歷是沒有意義的,因為它們遵循完全不同的路徑。 如果您希望單個函數同時發現最小值和最大值,那么該函數本身是遞歸的就沒有多大意義。 但是,它可以將調用包裝到兩個單獨的遞歸函數中,一個用於查找最小值,另一個用於查找最大值。

在 BST 中查找最小值和最大值非常容易。 請檢查下面的兩個代碼片段,我解釋了這些代碼的工作原理。

public int minValueInBST(Node node){
    if (node == null) throw new IllegalStateException();
    Node current = node;
    while (current.leftChild != null) {
        current = node.leftChild;
    }
    return current.value;
}

要在 BST 中找到最小值,我們必須找到最左邊的葉節點,因為該節點包含最小值。 所以首先,我們檢查根節點是否為空,如果為空,我們拋出 IllegalStateException 否則我們找到左節點,最后,我們返回左節點值。

public int maxValueInBST(Node node){
    if (node == null) throw new IllegalStateException();
    Node current = node;
    while (current.rightChild != null) {
        current = node.rightChild;
    }
    return current.value;
}

要在 BST 中找到最大值,我們必須找到最右邊的葉節點,因為該節點包含最大值。 所以首先,我們檢查根節點是否為空,如果為空我們拋出 IllegalStateException 否則我們找到正確的節點,最后,我們返回正確的節點值。

// try this
tree_node *min(tree_node *root)
{
    if (!root)
    {
        printf("Tree is empty");
        exit(1);
    }
    tree_node *ret_val;
    if (root->left == NULL)
    {
        ret_val = root;
    }
    else
    {
        ret_val = min(root->left);
    }
    return ret_val;
}
tree_node *max(tree_node *root)
{
    if (!root)
    {
        printf("Tree is empty");
        exit(1);
    }
    tree_node *ret_val;
    if (root->right == NULL)
    {
        ret_val = root;
    }
    else
    {
        ret_val = max(root->right);
    }
    return ret_val;
}

完整代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM