insert函数明明俩个参数,为啥main函数调用传一个参数?!
#include <iostream>
#include <iomanip>
using namespace std;
typedef int T;
struct Node{
T data;
Node* left;
Node* right;
Node(const T& d):data(d),left(),right(){}
//~Node(){cout<<"~Node(),"<<data<<endl;}
};
class Bst{
Node* root;
int sz;
typedef Node* tree;
//把p节点插入到t树中
void insert(Node* p, tree&t){
if(p==NULL) return;
if(t==NULL) t = p;
else if(p->data>t->data) insert(p,t->right);
else/*if(p->data<t->data)*/
insert(p, t->left);
}
void travel(tree&t){
if(t==NULL) return;
travel(t->left);
cout << t->data << ' ';
travel(t->right);
}
void clear(tree&t){
if(t==NULL) return;
clear(t->left);
clear(t->right);
delete t;
}
tree& find(const T& d, tree& t){
if(t==NULL) return t;
if(t->data==d) return t;
if(d>t->data) return find(d, t->right);
return find(d, t->left);
}
int high(tree& t){
if(t==NULL) return 0;
int lh = high(t->left);
int rh = high(t->right);
return (lh>rh?lh:rh) + 1;
}
void print(tree&t, int s, char c){
if(t==NULL) return;
print(t->right, s+3, '/');
cout << setw(s) << c << t->data << endl;
print(t->left, s+3, '\\');\\输出结果并没有'\\' ,只有'/'符号
}
public:
Bst():root(),sz(){}
~Bst(){clear();}
void insert(const T& d){
Node* pn = new Node(d);
insert(pn, root);
sz++;
}
void travel(){ travel(root);cout << endl;}
void clear(){clear(root);root=NULL;sz=0;}
tree& find(const T& d){return find(d, root);}
bool remove(const T& d){
tree& t = find(d);
if(t==NULL) return false;
//将左子树插入到右子树
insert(t->left, t->right);
//右子树向上提一级
Node* p = t;
t = t->right;
delete p;
sz--;
return true;
}
void removeAll(const T& d){ while(remove(d)); }
void modify(const T& old, const T& newVal){
while(remove(old)) insert(newVal);
}
int high(){ return high(root);}
void print(){print(root, 0, '*');}
int size(){ return sz;}
};
int main()
{
Bst t;
t.insert(5);
t.insert(3);
t.insert(2);
t.insert(4);
t.insert(8);
t.insert(6);
t.insert(9);
cout << t.high() << endl;
t.print();
//t.clear();
t.travel();
cout << t.size() << endl;
}
[解决办法]
int 写错了