二叉搜索树没有匹配的函数调用 我尝试用C++编程一个二进制搜索树,其中一个构造函数将空数组插入到主节点中,并打印结果,至少是意图。我发现了这两个错误,如果可能的话,我需要按照规范纠正它们。需要做的是创建一个node_t*构造函数,并将数组插入到二进制搜索树中。 错误:对“BST::insert(BST&,std::array::value\u type&)”的调用没有匹配的函数 错误:调用“BST::print\u BST(BST&)”时没有匹配的函数 #包括 #包括 #包括 使用名称空间std; /* *定义双链接列表的节点结构。 */ BST级 { 私人: 类型定义结构节点{ int-val; 结构节点*左; 结构节点*右; }节点t; 节点树; 公众: BST(){tree=NULL;} node_t*newNode(int val); node_t*insert(node_t*cur_root,int val); node_t*find_node(int val,node_t*root); node_t*find_val(int val,node_t*root); node_t*delete_node(int val,node_t*root); void delete_bst(节点*root); 无效打印(节点*根); node_t*find_max(node_t*root); }; //从给定值创建一个新节点,分配堆内存 //为了它。 BST::node\u t*BST::newNode(int val) { node_t*newNode=新节点; newNode->val=val; newNode->left=NULL; newNode->right=NULL; 返回newNode; } //在给定的二叉搜索树中插入新值, //为它分配堆内存。 BST::node_t*BST::insert(BST::node_t*cur_root,int val) { 如果(cur_root==NULL){return newNode(val);} if(val) cur_root->left=插入(cur_root->left,val); 否则如果(val>cur\u root->val) cur_root->right=插入(cur_root->right,val); 返回cur_根; } BST::node_t*BST::find_node(int val,BST::node_t*root){ 如果(root==NULL | | root->val==val){return root;} else if(root->val left); } else{返回find_节点(val,root->right);} 返回根; } BST::node\u t*BST::find\u max(BST::node\u t*root) { if(root==NULL) 返回NULL; while(root->right!=NULL) { 根=根->右; } 返回根; } //删除节点并重新排序bst BST::node\u t*BST::delete\u node(int val,BST::node\u t*root) { if(root==NULL)返回root; 否则如果(val) root->left=删除_节点(val,root->left); 否则如果(val>root->val) root->right=删除_节点(val,root->right); 其他的 { //没有孩子 if(root->right==NULL&&root->left==NULL) { 删除根; root=NULL; } //独生子女 else if(root->right==NULL) { 节点温度=根; 根=根->左; 删除临时文件; } else if(root->left==NULL) { 节点温度=根; 根=根->右; 删除临时文件; } //两个孩子 其他的 { node_t*temp=find_max(根->左); 根->值=温度->值; root->left=删除_节点(temp->val,root->left); } } 返回根; } //给定一个指向根的指针,释放与 //整棵树。 void BST::delete_BST(BST::node_t*root){ if(root!=NULL) { 删除(根->左); 删除(根->右); 删除(根); 如果(根->左!=NULL) 根->左=空; 如果(根->右!=NULL) root->right=NULL; root=NULL; } } /*给定一个指向根的指针,打印所有 *树中的值。 */ void BST::print\u BST(BST::node\u t*root) { if(root!=NULL){ printf(“%d”,根->值); 打印(根->左); 打印(根->右); } } int main() { 英国理工学院; 数组ai={17,9,23,5,11,21,27,20,22}; 对于(size_t i=0;ival=val; newNode->left=NULL; newNode->right=NULL; 返回newNode; } //在给定的二叉搜索树中插入新值, //为它分配堆内存。 BST::node\u t*BST::insert(BST::node\u t*&cur\u root,int val) { 如果(cur_root==NULL){ cur_root=newNode(val); } 否则如果(val) cur_root->left=插入(cur_root->left,val); 否则如果(val>cur\u root->val) cur_root->right=插入(cur_root->right,val); 返回cur_根; } void BST::print\u BST(BST::node\u t*root) { if(root!=NULL){ printf(“%d”,根->值); 打印(根->左); 打印(根->右); } } int main() { 英国理工学院; 数组ai={17,9,23,5,11,21,27,20,22}; 对于(size_t i=0;i
好,我修复了插入和打印功能二叉搜索树没有匹配的函数调用 我尝试用C++编程一个二进制搜索树,其中一个构造函数将空数组插入到主节点中,并打印结果,至少是意图。我发现了这两个错误,如果可能的话,我需要按照规范纠正它们。需要做的是创建一个node_t*构造函数,并将数组插入到二进制搜索树中。 错误:对“BST::insert(BST&,std::array::value\u type&)”的调用没有匹配的函数 错误:调用“BST::print\u BST(BST&)”时没有匹配的函数 #包括 #包括 #包括 使用名称空间std; /* *定义双链接列表的节点结构。 */ BST级 { 私人: 类型定义结构节点{ int-val; 结构节点*左; 结构节点*右; }节点t; 节点树; 公众: BST(){tree=NULL;} node_t*newNode(int val); node_t*insert(node_t*cur_root,int val); node_t*find_node(int val,node_t*root); node_t*find_val(int val,node_t*root); node_t*delete_node(int val,node_t*root); void delete_bst(节点*root); 无效打印(节点*根); node_t*find_max(node_t*root); }; //从给定值创建一个新节点,分配堆内存 //为了它。 BST::node\u t*BST::newNode(int val) { node_t*newNode=新节点; newNode->val=val; newNode->left=NULL; newNode->right=NULL; 返回newNode; } //在给定的二叉搜索树中插入新值, //为它分配堆内存。 BST::node_t*BST::insert(BST::node_t*cur_root,int val) { 如果(cur_root==NULL){return newNode(val);} if(val) cur_root->left=插入(cur_root->left,val); 否则如果(val>cur\u root->val) cur_root->right=插入(cur_root->right,val); 返回cur_根; } BST::node_t*BST::find_node(int val,BST::node_t*root){ 如果(root==NULL | | root->val==val){return root;} else if(root->val left); } else{返回find_节点(val,root->right);} 返回根; } BST::node\u t*BST::find\u max(BST::node\u t*root) { if(root==NULL) 返回NULL; while(root->right!=NULL) { 根=根->右; } 返回根; } //删除节点并重新排序bst BST::node\u t*BST::delete\u node(int val,BST::node\u t*root) { if(root==NULL)返回root; 否则如果(val) root->left=删除_节点(val,root->left); 否则如果(val>root->val) root->right=删除_节点(val,root->right); 其他的 { //没有孩子 if(root->right==NULL&&root->left==NULL) { 删除根; root=NULL; } //独生子女 else if(root->right==NULL) { 节点温度=根; 根=根->左; 删除临时文件; } else if(root->left==NULL) { 节点温度=根; 根=根->右; 删除临时文件; } //两个孩子 其他的 { node_t*temp=find_max(根->左); 根->值=温度->值; root->left=删除_节点(temp->val,root->left); } } 返回根; } //给定一个指向根的指针,释放与 //整棵树。 void BST::delete_BST(BST::node_t*root){ if(root!=NULL) { 删除(根->左); 删除(根->右); 删除(根); 如果(根->左!=NULL) 根->左=空; 如果(根->右!=NULL) root->right=NULL; root=NULL; } } /*给定一个指向根的指针,打印所有 *树中的值。 */ void BST::print\u BST(BST::node\u t*root) { if(root!=NULL){ printf(“%d”,根->值); 打印(根->左); 打印(根->右); } } int main() { 英国理工学院; 数组ai={17,9,23,5,11,21,27,20,22}; 对于(size_t i=0;ival=val; newNode->left=NULL; newNode->right=NULL; 返回newNode; } //在给定的二叉搜索树中插入新值, //为它分配堆内存。 BST::node\u t*BST::insert(BST::node\u t*&cur\u root,int val) { 如果(cur_root==NULL){ cur_root=newNode(val); } 否则如果(val) cur_root->left=插入(cur_root->left,val); 否则如果(val>cur\u root->val) cur_root->right=插入(cur_root->right,val); 返回cur_根; } void BST::print\u BST(BST::node\u t*root) { if(root!=NULL){ printf(“%d”,根->值); 打印(根->左); 打印(根->右); } } int main() { 英国理工学院; 数组ai={17,9,23,5,11,21,27,20,22}; 对于(size_t i=0;i,c++,C++,好,我修复了插入和打印功能 #include <cstdio> #include <cstdlib> #include <array> using namespace std; /* * Define a node structure for double linked list. */ class BST { private: typedef struct node { int val; struct node
#include <cstdio>
#include <cstdlib>
#include <array>
using namespace std;
/*
* Define a node structure for double linked list.
*/
class BST
{
private:
typedef struct node {
int val;
struct node* left;
struct node* right;
} node_t;
node_t* tree;
public:
BST() { tree = NULL; }
node_t* newNode(int val);
node_t* insert(node_t *&cur_root, int val);
node_t* find_node(int val, node_t* root);
node_t* find_val(int val, node_t* root);
node_t* delete_node(int val, node_t* root);
void delete_bst(node_t* root);
void print_bst(node_t* root);
node_t* find_max(node_t* root);
node_t *bstroot;
};
// Creates a new node from a given value, allocating heap memory
// for it.
BST::node_t* BST::newNode(int val)
{
node_t* newNode = new node;
newNode->val = val;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// Inserts a new value into a given binary search tree,
// allocating heap memory for it.
BST::node_t* BST::insert(BST::node_t *&cur_root, int val)
{
if(cur_root == NULL) {
cur_root = newNode(val);
}
else if( val <= cur_root->val )
cur_root->left = insert(cur_root->left, val);
else if( val > cur_root->val)
cur_root->right = insert(cur_root->right, val);
return cur_root;
}
void BST::print_bst(BST::node_t* root)
{
if (root != NULL) {
printf("%d ", root->val);
print_bst(root->left);
print_bst(root->right);
}
}
int main()
{
BST bst;
array<int, 9> ai = {17, 9, 23, 5, 11, 21, 27, 20, 22};
for( size_t i = 0; i < ai.size(); ++i)
{
bst.insert(bst.bstroot, ai[i]);
}
bst.print_bst(bst.bstroot);
}
#包括
#包括
#包括
使用名称空间std;
/*
*定义双链接列表的节点结构。
*/
BST级
{
私人:
类型定义结构节点{
int-val;
结构节点*左;
结构节点*右;
}节点t;
节点树;
公众:
BST(){tree=NULL;}
node_t*newNode(int val);
节点*插入(节点*和当前根,int-val);
node_t*find_node(int val,node_t*root);
node_t*find_val(int val,node_t*root);
node_t*delete_node(int val,node_t*root);
void delete_bst(节点*root);
无效打印(节点*根);
node_t*find_max(node_t*root);
节点*bstroot;
};
//从给定值创建一个新节点,分配堆内存
//为了它。
BST::node\u t*BST::newNode(int val)
{
node_t*newNode=新节点;
newNode->val=val;
newNode->left=NULL;
newNode->right=NULL;
返回newNode;
}
//在给定的二叉搜索树中插入新值,
//为它分配堆内存。
BST::node\u t*BST::insert(BST::node\u t*&cur\u root,int val)
{
如果(cur_root==NULL){
cur_root=newNode(val);
}
否则如果(val)
cur_root->left=插入(cur_root->left,val);
否则如果(val>cur\u root->val)
cur_root->right=插入(cur_root->right,val);
返回cur_根;
}
void BST::print\u BST(BST::node\u t*root)
{
if(root!=NULL){
printf(“%d”,根->值);
打印(根->左);
打印(根->右);
}
}
int main()
{
英国理工学院;
数组ai={17,9,23,5,11,21,27,20,22};
对于(size_t i=0;i
好,我修复了插入和打印功能
#include <cstdio>
#include <cstdlib>
#include <array>
using namespace std;
/*
* Define a node structure for double linked list.
*/
class BST
{
private:
typedef struct node {
int val;
struct node* left;
struct node* right;
} node_t;
node_t* tree;
public:
BST() { tree = NULL; }
node_t* newNode(int val);
node_t* insert(node_t *&cur_root, int val);
node_t* find_node(int val, node_t* root);
node_t* find_val(int val, node_t* root);
node_t* delete_node(int val, node_t* root);
void delete_bst(node_t* root);
void print_bst(node_t* root);
node_t* find_max(node_t* root);
node_t *bstroot;
};
// Creates a new node from a given value, allocating heap memory
// for it.
BST::node_t* BST::newNode(int val)
{
node_t* newNode = new node;
newNode->val = val;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// Inserts a new value into a given binary search tree,
// allocating heap memory for it.
BST::node_t* BST::insert(BST::node_t *&cur_root, int val)
{
if(cur_root == NULL) {
cur_root = newNode(val);
}
else if( val <= cur_root->val )
cur_root->left = insert(cur_root->left, val);
else if( val > cur_root->val)
cur_root->right = insert(cur_root->right, val);
return cur_root;
}
void BST::print_bst(BST::node_t* root)
{
if (root != NULL) {
printf("%d ", root->val);
print_bst(root->left);
print_bst(root->right);
}
}
int main()
{
BST bst;
array<int, 9> ai = {17, 9, 23, 5, 11, 21, 27, 20, 22};
for( size_t i = 0; i < ai.size(); ++i)
{
bst.insert(bst.bstroot, ai[i]);
}
bst.print_bst(bst.bstroot);
}
#包括
#包括
#包括
使用名称空间std;
/*
*定义双链接列表的节点结构。
*/
error: no matching function for call to 'BST::insert(BST&, std::array::value_type&)'
error: no matching function for call to 'BST::print_bst(BST&)'
for( size_t i = 0; i < ai.size(); ++i)
{
bst.insert(bst, ai[i]);
}
BST::print_bst(bst);
public:
...
node_t *get_root (void) { return tree; } /* tree accessor */
for (size_t i = 0; i < ai.size(); i++)
bst.insert (bst.get_root(), ai[i]);
bst.print_bst (bst.get_root());
putchar ('\n'); /* make your program POSIX compliant with final '\n' */
}
BST::node_t* BST::newNode(int val)
{
node_t* newNode = new node;
public:
...
node_t* newNode(int val);
/* Inserts a new value into a given binary search tree,
* allocating heap memory for it.
*/
BST::node_t *BST::insert (BST::node_t *cur_root, int val)
{
if (tree == NULL) /* you must assign 1st node to tree */
return (tree = newNode(val));
if (cur_root == NULL)
return (cur_root = newNode(val));
if (val <= cur_root->val )
cur_root->left = insert (cur_root->left, val);
else if (val > cur_root->val)
cur_root->right = insert (cur_root->right, val);
return cur_root;
}
node_t* insert(node_t* cur_root, int val);
node_t* find_node(int val, node_t* root);
// Given a pointer to the root, frees the memory associated with
// an entire tree.
void BST::delete_bst(BST::node_t* root) {
if(root != NULL)
{
delete_bst( root->left );
delete_bst( root->right );
delete(root);
if( root->left != NULL)
root->left = NULL;
if( root->right != NULL)
root-> right = NULL;
root = NULL;
}
}
/* Given a pointer to the root, frees the memory associated with
* an entire tree.
*/
void BST::delete_bst (BST::node_t *root)
{
if (root != NULL) {
delete_bst (root->left);
delete_bst (root->right);
delete(root);
}
tree = NULL;
}
public:
...
void print_tree (void) { print_bst (tree); putchar ('\n'); };
#include <cstdio>
#include <cstdlib>
#include <array>
using namespace std;
/*
* Define a node structure for a binary tree list.
*/
class BST
{
private:
typedef struct node {
int val;
struct node* left;
struct node* right;
} node_t;
node_t *tree;
public:
BST() { tree = NULL; }
~BST() { delete_bst (tree); } /* if you define BST(), define ~BST() */
node_t *get_root (void) { return tree; } /* tree accessor */
node_t *newNode (int val);
node_t *insert (node_t* cur_root, int val);
node_t *find_node (node_t* root, int val);
node_t *find_val (node_t* root, int val);
node_t *delete_node (node_t* root, int val);
void delete_bst (node_t* root);
void print_bst (node_t* root);
node_t *find_max (node_t* root);
/* make program output POSIX compliant with final '\n'
* (just create a wrapper for print_bst, or add putchar('\n') in main )
*/
void print_tree (void) { print_bst (tree); putchar ('\n'); };
};
/* Creates a new node from a given value, allocating heap memory for it. */
BST::node_t* BST::newNode (int val)
{
node_t* newnode = new node_t;
newnode->val = val;
newnode->left = NULL;
newnode->right = NULL;
return newnode;
}
/* Inserts a new value into a given binary search tree,
* allocating heap memory for it.
*/
BST::node_t *BST::insert (BST::node_t *cur_root, int val)
{
if (tree == NULL) /* you must assign 1st node to tree */
return (tree = newNode(val));
if (cur_root == NULL)
return (cur_root = newNode(val));
if (val <= cur_root->val )
cur_root->left = insert (cur_root->left, val);
else if (val > cur_root->val)
cur_root->right = insert (cur_root->right, val);
return cur_root;
}
/* determine whether node with value exists in tree
* (don't swap parameter order -- it's confusing)
*/
BST::node_t *BST::find_node (BST::node_t *root, int val)
{
if (root == NULL || root->val == val)
return root;
else if (root->val <= val) {
return find_node (root->left, val);
}
else
return find_node (root->right, val);
return root;
}
/* determine maximum value in the tree */
BST::node_t* BST::find_max (BST::node_t* root)
{
if (root == NULL)
return NULL;
while (root->right != NULL)
root = root->right;
return root;
}
/* Deletes node and reorders bst */
BST::node_t* BST::delete_node (BST::node_t* root, int val)
{
if (root == NULL)
return root;
else if (val <= root->val)
root->left = delete_node (root->left, val);
else if (val > root->val)
root->right = delete_node (root->right, val);
else {
// No child
if (root->right == NULL && root->left == NULL) {
delete root;
root = NULL;
}
// One child
else if (root->right == NULL) {
node_t *temp = root;
root = root->left;
delete temp;
}
else if (root->left == NULL) {
node_t *temp = root;
root = root->right;
delete temp;
}
// Two child
else {
node_t *temp = find_max (root->left);
root->val = temp->val;
root->left = delete_node (root->left, temp->val);
}
}
return root;
}
/* Given a pointer to the root, frees the memory associated with
* an entire tree.
*/
void BST::delete_bst (BST::node_t *root)
{
if (root != NULL) {
delete_bst (root->left);
delete_bst (root->right);
delete(root);
}
tree = NULL;
}
/* Given a pointer to the root, prints all of
* the values in a tree.
*/
void BST::print_bst (BST::node_t *root)
{
if (root != NULL) {
printf ("%d ", root->val);
print_bst (root->left);
print_bst (root->right);
}
}
int main (void) {
BST bst;
array<int, 9> ai = {17, 9, 23, 5, 11, 21, 27, 20, 22};
for (size_t i = 0; i < ai.size(); i++)
bst.insert (bst.get_root(), ai[i]);
bst.print_tree(); /* POSIX comliant output w/final '\n'
* use wrapper function, or just putchar('\n') here
*/
}
$ ./bin/arr_bst
17 9 5 11 23 21 20 22 27
$ valgrind ./bin/arr_bst
==8123== Memcheck, a memory error detector
==8123== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==8123== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==8123== Command: ./bin/arr_bst
==8123==
17 9 5 11 23 21 20 22 27
==8123==
==8123== HEAP SUMMARY:
==8123== in use at exit: 0 bytes in 0 blocks
==8123== total heap usage: 10 allocs, 10 frees, 72,920 bytes allocated
==8123==
==8123== All heap blocks were freed -- no leaks are possible
==8123==
==8123== For counts of detected and suppressed errors, rerun with: -v
==8123== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)