如何创建一个返回无序二叉树最小值的函数

时间:2012-07-30 19:23:32

标签: c++ tree binary-tree tree-traversal

这似乎应该很容易,但我已经有很长一段时间没遇到这个问题了。正如标题所说,我只是试图找到具有最小值的二叉树(不是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);

}

如果可能的话,我想尝试在不跟踪“计数”的情况下执行此操作以使代码更清晰。 如果需要澄清,请告诉我。

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:1)

基本上您需要做的就是访问每个节点并跟踪您看到的最小值。这实际上可以相当简单地完成:

#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;

}

答案 2 :(得分: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();
}

SEE JIMMY RUN