c++ - 将 STL 列表转换为二叉搜索树递归模板

标签 c++ recursion stl

这是我目前所拥有的:

#include <stdio.h>
#include "bintree.h"
#include <list>
#include <iostream>

using namespace std;
using namespace main_savitch_10;

template <class Item>
binary_tree_node<Item>* convert(list<Item> *& list, int start, int end);

template <class Item> 
binary_tree_node<Item>* convert(list<Item> *head, int n);

int main(int argc, char **argv)
{
    list<int> L;
    L.push_front(10);
    L.push_back(20);
    L.push_back(30);
    L.push_back(40);
    L.push_back(50);
    L.push_back(60);
    L.push_back(70);
    list<int>::iterator test;
    for(test = L.begin(); test != L.end(); test++)
    {
    cout<<*test<<" ";
    }

    binary_tree_node<int>* L2 = convert(L, 7);

    print(L2, 3);

}

template <class Item>
binary_tree_node<Item>* convert(list<Item> *& list, int start, int end)
{
    if (start > end) return NULL;
    int mid = start + (end - start) / 2;
    binary_tree_node<Item>* leftChild = convert(list, start, mid-1);
    binary_tree_node<Item>* parent = new binary_tree_node<Item> (list->data());
    parent->left() = leftChild;
    list = list->next();
    parent->right() = convert(list, mid+1, end);
    return parent;
}

template <class Item> 
binary_tree_node<Item>* convert(list<Item> *head, int n) 
{
    return convert(head, 0, n-1);
}

我在行中遇到错误 binary_tree_node<int>* L2 = convert(L, 7);

说调用没有匹配的函数...当我将它们列在主函数的正上方时,这怎么可能?

旁注:“bintree.h”和命名空间 main_savitch_10 来自二叉搜索树的模板实现文件,可以在以下位置找到 http://ksuweb.kennesaw.edu/~dgayler/cs3304/text_examples/chap10/bintree.h http://ksuweb.kennesaw.edu/~dgayler/cs3304/text_examples/chap10/bintree.template

最佳答案

您的函数接受一个指向列表的指针。这意味着您应该将列表的地址传递给它。

关于c++ - 将 STL 列表转换为二叉搜索树递归模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36183037/

相关文章:

c# - 如何使用非托管 C++ 获取 WCF 命名管道的名称?

c++ - 在 Qt4 中使用 Bash 变量

c++ - 什么是容器/适配器? C++

c++ - 如何为集合提供带有迭代器的 const 接口(interface)?

c++ - StackWalk64 不适用于发布版本

javascript - 深拷贝 NAPI::Value 对象

python - python中for循环内的递归调用不会在预期的位置退出

c++ - 容器类型和编译时类型推导

windows - 如何通过bat文件递归地将文件名从大写更改为小写

c - 在 C 中使用递归的数字总和