繁体   English   中英

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

[英]How to create a function that returns smallest value of an unordered binary tree

看来这确实很容易,但是我已经困扰了很长时间了。 就像标题所说的那样,我只是试图在二叉树(不是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);

}

如果可能的话,我也想尝试这样做而不跟踪“计数”,以使代码更整洁。 让我知道是否需要澄清。

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

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

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

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

}

好的,所以您有一棵无序的二叉树,并且您正在尝试查找其中的最低元素。

由于树是无序的,因此最低的元素可以在树中的任何位置,因此您必须搜索整个树。

搜索的特征如下:

  • 彻底的(搜索整棵树)
  • 递归的(而不是迭代的,这真的很麻烦)
  • 基本情况:节点为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;
}

编辑以求彻底,公正和客观:

// 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();
}

查看JIMMY RUN

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM