我已经使用循环在BST中插入了一个函数,它工作得很好.现在,当我写使用递归来做它时,我不知道它为什么不能正常工作,但是根据我的说法逻辑是正确的.似乎没有新的节点被添加到BST树中,并且在从插入函数出来之后树的头部再次变为NULL.
#include <iostream>
using namespace std;
class node{
public:
int data;
node *right;
node *left;
node(){
data=0;
right=NULL;
left=NULL;
}
};
class tree{
node *head;
int maxheight;
void delete_tree(node *root);
public:
tree(){head=0;maxheight=-1;}
void pre_display(node* root);
node* get_head(){return head;}
void insert(int key,node* current);
};
void tree::insert(int key,node *current){
if(current==NULL)
{
node *newnode=new node;
newnode->data=key;
current=newnode;
}
else{
if(key<current->data)
insert(key,current->left);
else
insert(key,current->right);
}
return;
}
void tree::pre_display(node *root){
if(root!=NULL)
{
cout<<root->data<<" ";
pre_display(root->left);
pre_display(root->right);
}
}
int main(){
tree BST;
int arr[9]={17,9,23,5,11,21,27,20,22},i=0; …Run Code Online (Sandbox Code Playgroud) 我正在尝试用C++创建一个应用程序.在应用程序中,我有默认构造函数和另一个带有3个参数的构造函数.用户从键盘提供一个整数,它将用于使用非默认构造函数创建对象数组.不幸的是,到目前为止我还没有完成它,因为我遇到了创建对象数组的问题,他们将使用非默认构造函数.有什么建议或帮助吗?
#include<iostream>
#include<cstring>
#include<cstdlib>
#include <sstream>
using namespace std;
class Station{
public:
Station();
Station(int c, char *ad, float a[]);
~Station();
void setAddress(char * addr){
char* a;
a = (char *)(malloc(sizeof(addr+1)));
strcpy(a,addr);
this->address = a;
}
void setCode(int c){
code=c;
}
char getAddress(){
return *address;
}
int getCode(){
return code;
}
float getTotalAmount(){
float totalAmount=0;
for(int i=0;i<4;i++){
totalAmount+=amount[i];
}
return totalAmount;
}
void print(){
cout<<"Code:"<<code<<endl;
cout<<"Address:"<<address<<endl;
cout<<"Total Amount:"<<getTotalAmount()<<endl;
cout<<endl;
}
private:
int code;
char *address;
float amount[4];
};
Station::Station(){
code= 1; …Run Code Online (Sandbox Code Playgroud) 我最近从使用C接口改为OpenCV中的C++接口.在C接口中,C++中似乎不存在各种各样的东西.有谁知道这些问题的解决方案:
1)在C接口中有一个名为Contour Scanner的对象.它被用于逐个查找图像中的轮廓.我将如何在C++中执行此操作?我不想一次找到所有轮廓,而是希望一次找到一个.
2)在C CvSeq中用于表示轮廓,但是在C++ vector <vector<Point> >中使用.在CI中,能够通过使用来访问下一个轮廓h_next.什么是C++相当于 h_next?