【问题标题】:How to change my function into a constructor?如何将我的函数更改为构造函数?
【发布时间】:2018-04-17 23:36:15
【问题描述】:

我需要创建一个参数化的构造函数,在给定排序数组的情况下创建一个完美平衡的二叉搜索树。我知道如何创建一个创建 BST 的函数,但是如何从构造函数中创建 BST? 这是我的功能:

    node * sortedArrayBST(double * arr, int start, int end)
    {
        int mid = (start + end)/2;
        if (start > end)
        {
            return NULL;
        }
        //because the array will be sorted
        //the root of the tree will contain the item in the middle of the array so everything less than the middle will go in the left subtree
        //and everything greater than the middle will go in the right subtree

        node * root = new node(arr[mid]);
        //recursively make left subtree
        root->left = sortedArrayBST(arr, start, end-1);
        //recursively make left subtree
        root->right = sortedArrayBST(arr, mid + 1, end);

        return root;
    }

【问题讨论】:

  • 构造函数没有返回类型,你要如何返回节点*?
  • 当新手寻求帮助时,我希望每次看到投反对票时都能得到一分钱......
  • 构造函数必须是结构/类的成员。你需要一个结构或一个类。
  • 学究气(但我们不都是这样)。 int mid = (start + end)/2; 有一个潜在的问题,如果值足够大,它可能会溢出 int。欲了解更多信息,请参阅here。您不太可能使用那么大但理论上仍然可行的数组。
  • 提示您的努力:在stackoverflow.com/a/45514477/2785528 上查看我的回答。请注意,我有两个协作类:BTree_t 和 Node_t。分离这些问题允许 BTree_t 包含“Node_t* m_root”,即保存一棵树的根的地方。 Btree 方法检查各种树问题(满或空等),但通常调用 m_root->methodX();其中各种方法是节点插入的细节,showTallView, searchR [Recursive], showBR [BreadthFirst, or wide view Recursive], showDFioR [depth first, in-order, Recursive]等(我喜欢递归!)跨度>

标签: c++ function constructor binary-search-tree


【解决方案1】:
class BST
{
public:
      BST (double * arr, int start, int end)
      {
          int mid = (start + end)/2;
          if (start > end)
          {
              std::cerr << "Invalid BST tree."
          }
          //because the array will be sorted
          //the root of the tree will contain the item in the middle of the array so everything less than the middle will go in the left subtree
          //and everything greater than the middle will go in the right subtree

          root = new node(arr[mid]);
          node *temp, *parent;
          for (num = arr[mid/2]; start <= end;)
             if (num <= arr[end]){
                parent = temp;
                temp=temp->left;
                end = (start + end)/2;
             } else {
                parent = temp;
                temp=temp->right;
                start = (start + end)/2;
             }
          }
          //recursively make left subtree
          root->right = sortedArrayBST(arr, mid + 1, end);
      }
private :
      node * root;
}

【讨论】:

  • 构造函数不能返回任何东西。 node * root 是构造函数的局部变量,所以你有内存泄漏,构造函数不起作用。 node root; 不是指针。
  • UPS 我的坏@NeilButterworth,我会马上修复它:)
  • 请添加一个返回节点*的方法,这样数据在类中将永远丢失:)。
  • 在构造函数中直接调用sortedArrayBST不是更好吗?避免代码重复。即root = sortedArrayBST(arr, start, end);
  • 一些架构思想,诸如 sortedArrayBST 之类的方法通常包含在类的私有部分中。如果您只需要一种处理,则结果的进一步处理将在类内。如果没有,那么数据可以作为指针或引用或更好地在外部共享,通过继承数据可以被特定的处理实现访问,访问必须受到保护以使其可见。
猜你喜欢
  • 2016-08-18
  • 2013-06-16
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 2014-03-12
  • 2016-11-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多