在二叉搜索树中找到具有最小值的节点(打印该节点)

问题描述 投票:-1回答:2

我必须在二叉搜索树中找到具有最小值的节点。我编写了函数,但无法显示具有最小值的节点。这是树]]

struct Node
{
    int key; 
    void *info; 
    Node *left, *right;
};

这是功能

Node* findMin(Node* r)
{
    if (r == 0)
        return 0;
    else if (r->left == 0)
        return r;
    else
        return findMin(r->left);
}

我在主文件中这样调用该函数:

Node *root = makeTree(); // I enter the values and insert them into a binary search tree with an insert function.
root = findMin(root);
cout << root;

它显示地址而不是值。我应该怎么做才能显示最小值的节点?

我必须在二叉搜索树中找到具有最小值的节点。我编写了函数,但无法显示具有最小值的节点。这是树结构Node {int key;无效* ...

c++ data-structures binary-search-tree
2个回答
0
投票

在此声明中


0
投票
int minValue(struct node* node)  
{  
    struct node* current = node;  

    /* loop down to find the leftmost leaf */
    while (current->left != NULL)  
    {  
        current = current->left;  
    }  
    return(current->data);  
}
© www.soinside.com 2019 - 2024. All rights reserved.