-1

这似乎应该很容易,但我已经有很长一段时间了。正如标题所说,我只是想在二叉树(不是 BST!)中找到具有最小值的节点并返回它。我可以很容易地编写一个递归 void 函数,它至少可以分配函数中的最小值,但是一旦我到达一个 NULL 指针,我就会陷入如何回溯到以前的节点的问题。

我有一个节点类,它有一个指向左右子节点的指针,每个子节点都有自己的值。到目前为止,这是我的(失败的)尝试:

int preOrder(Node *node, int value, int count, int sizeOfTree)
{
  count++; //keeps track of whether or not we have traversed the whole tree

  if(value < node->getValue())
    value = node->getValue(); 

  if(count == sizeOfTree);
    return value;

  if(node == NULL)
    //Want to return to the previous function call
    //How do I do this for a non void function? 
    //for a void function, you could jsut type "return;" and the function
    //back tracks to your previous place in the tree
    //but since I'm returning a value, How would I go about doing this?

  //these 2 calls are incorrect but the idea is that I first traverse the left subtree
  //followed by a traversal of the right subtree.
  preOrder(node->getLeft(), value);

  preOrder(node->getRight(), value);

}

如果可能的话,我想尝试在不跟踪“计数”的情况下执行此操作,以使代码更清晰。让我知道是否需要进一步澄清。

4

3 回答 3

6

我真的不明白为什么在您的原始代码中,您需要跟踪遍历的元素数量。这是我的解决方案:

int find_min(Node* node)
{
  int value = node->getValue()

  Node* left_node = node->getLeft();
  if (left_node != NULL)
  {
    int left_value = find_min(left_node);
    if (left_value < value)
      value = left_value;
  }

  Node* right_node = node->getRight();
  if (right_node != NULL)
  {
    int right_value = find_min(right_node);
    if (right_value < value)
      value = right_value;
  }

  return value;
}
于 2012-07-30T19:31:48.320 回答
1

Basically what you need to do is just visit every node and keep track of the smallest value you've seen. This can actually be done fairly simply:

#include <algorithm>
#include <limits>

int preOrder(Node *node)
{
  if(node == NULL) return std::numeric_limits<int>::max();
  // this should never affect the calculation of the minimum
  // (What could possibly be bigger than INT_MAX? At worst it's equal)

  int value = std::min(
    node->getValue(),
    preOrder(node->getLeft())
    );

  value = std::min(
    value,
    preOrder(node->getRight())
    );

  return value;

}
于 2012-07-30T19:34:13.780 回答
1

好的,所以你有一个无序的二叉树,你试图找到其中的最低元素。

由于树是无序的,最低的元素可以在树中的任何位置,所以你必须搜索整棵树。

搜索的特点如下:

  • 彻底(搜索整棵树)
  • 递归(而不是迭代,这真的很恶心)
  • 基本情况:节点为 NULL
  • 基本结果:保持当前值

那就写吧:

#include <algorithm>
using namespace std;

int searchLowest(Node * node, int value = INT_MAX)
{
    if (node == NULL) // base case
        return value; // base outcome

    // at this point, node must not be NULL

    value = min(value, preOrder(node->getRight(), value)); // thorough, always recurse
    value = min(value, preOrder(node->getLeft (), value)); // and check children
    value = min(value, node->getValue());
    return value;
}

编辑彻底、公正和 OOness:

// Node.h
#include <algorithm>
using namespace std;

template <typename T>
class Node
{
public:
    Node(T item)
    {
        data = item;
    }

    T lowest()
    {
        T value = data;

        if (right != NULL)
            value = min(value, right->lowest());
        if (left  != NULL)
            value = min(value, left->lowest());

        return value;
    }

    Node<T> * getRight()
    {
        return right;
    }

    Node<T> * getLeft()
    {
        return left;
    }

private:
    T data;

    Node<T> * right;
    Node<T> * left;
};

// main.cpp
#include <iostream>
#include "Node.h"
using namespace std;

int main(int c, char * v[])
{
    Node<int> * tree = sycamore(); // makes a nice big tree

    cout << tree->lowest();
}

见吉米跑

于 2012-07-30T19:46:10.007 回答