#include <iostream>
using namespace std;
typedef char T;
class bst{
struct Node{
T data;
Node* L;
Node* R;
Node(const T& d):data(d),L(),R(){}
Node(const T& d,Node*l,Node*r):data(d),L(l),R(r){}
};
typedef Node* tree;
Node* rp;
int n;
public:
bst():rp(),n(){}
void clear(){clear(rp);n=0;}
~bst(){clear();}
void insert(const T& d){insert(rp,new Node(d));++n;}
tree& find(const T& d){return find(rp,d);}
void travel()const{travel(rp);cout<<endl;}
bool empty()const{return rp==NULL;}
bool remove(const T& d){
tree& t = find(d);
if(t==NULL) return false;
Node* p = t;
if(t->L!=NULL) insert(t->R, t->L);
t = t->R;
delete p;
--n;
return true;
}
const T& root()const{if(!rp) throw"空";return rp->data;}
int size()const{return n;}
void update(const T& olddata,const T& newdata){
if(remove(olddata)) insert(newdata);
}
void insert(tree& t, Node* p){
if(t==NULL) t = p;
else if(p->data < t->data) insert(t->L,p);
else insert(t->R,p);
}
tree& find(tree& t, const T& d){//返回以d为根的子树的根指针
if(t==NULL) return t;//没找到
else if(d==t->data) return t;//找到了
else if(d<t->data) return find(t->L,d);
else return find(t->R,d);
}
void travel(tree t)const{
if(t!=NULL){
travel(t->L);
cout << t->data << ' ';
travel(t->R);
}
}
void clear(tree& t){
if(t!=NULL){
clear(t->L);
clear(t->R);
delete t; t=NULL;
}
}
int high(tree t){
if(t==NULL) return 0;
int lh = high(t->L);
int rh = high(t->R);
return 1+(lh>rh?lh:rh);
}
};
int main()
{
bst b;
b.insert('k');b.insert('s');b.insert('f');b.insert('t');
b.insert('a');b.insert('m');b.insert('x');b.insert('e');
b.insert('w');b.insert('b');b.insert('u');b.insert('j');
b.travel();
b.remove('k');b.remove('m');b.remove('j');b.remove('u');
b.travel();
b.update('b','k');b.update('k','b');b.update('x','*');
b.travel();
while(!b.empty()) b.remove(b.root());
cout<<"size:"<<b.size()<<endl;
b.travel();
}
#include <iostream>
using namespace std;
#include <iomanip>
typedef char T;
class bst{
struct Node{
T data;
Node* L;
Node* R;
Node(const T& d):data(d),L(),R(){}
Node(const T& d,Node*l,Node*r):data(d),L(l),R(r){}
};
typedef Node* tree;
Node* rp;
int n;
public:
bst():rp(),n(){}
void clear(){clear(rp);n=0;}
~bst(){clear();}
void insert(const T& d){insert(rp,new Node(d));++n;}
tree& find(const T& d){return find(rp,d);}
void travel()const{travel(rp);cout<<endl;}
bool empty()const{return rp==NULL;}
bool remove(const T& d){
tree& t = find(d);
if(t==NULL) return false;
Node* p = t;
if(t->L!=NULL) insert(t->R, t->L);
t = t->R;
delete p;
--n;
return true;
}
const T& root()const{if(!rp) throw"空";return rp->data;}
int size()const{return n;}
void update(const T& olddata,const T& newdata){
if(remove(olddata)) insert(newdata);
}
void insert(tree& t, Node* p){
if(t==NULL) t = p;
else if(p->data < t->data) insert(t->L,p);
else insert(t->R,p);
}
tree& find(tree& t, const T& d){//返回以d为根的子树的根指针
if(t==NULL) return t;//没找到
else if(d==t->data) return t;//找到了
else if(d<t->data) return find(t->L,d);
else return find(t->R,d);
}
void travel(tree t)const{
if(t!=NULL){
travel(t->L);
cout << t->data << ' ';
travel(t->R);
}
}
void clear(tree& t){
if(t!=NULL){
clear(t->L);
clear(t->R);
delete t; t=NULL;
}
}
int high(tree t){
if(t==NULL) return 0;
int lh = high(t->L);
int rh = high(t->R);
return 1+(lh>rh?lh:rh);
}
void print(tree t, int space, char sign){
if(t==NULL) return;
print(t->R,space+3,'/');
cout << setw(space+1) << sign << t->data << endl;
print(t->L,space+3,'\\');
}
void print(){ print(rp,0,'*');cout<<"---------"<<endl; }
};
int main()
{
bst b;
b.insert('k');b.insert('s');b.insert('f');b.insert('t');
b.insert('a');b.insert('m');b.insert('x');b.insert('e');
b.insert('w');b.insert('b');b.insert('u');b.insert('j');
b.print();
b.remove('k');b.remove('m');b.remove('j');b.remove('u');
b.print();
b.update('b','k');b.update('k','b');b.update('x','*');
b.print();
while(!b.empty()) b.remove(b.root());
cout<<"size:"<<b.size()<<endl;
b.print();
}
#include <iostream>
using namespace std;
class Person{
string name;
int age;
string addr;
public:
Person(const char* n, int a, const char* ad)
:name(n),age(a),addr(ad){}
friend bool operator<(const Person& a, const Person& b){
return a.name<b.name;
}
friend bool operator==(const Person& a, const Person& b){
return a.name==b.name;
}
friend ostream& operator<<(ostream& o, const Person& a){
o << a.name << ':' << a.age << ',' << a.addr;
return o;
}
};
/*Person* binarysearch(Person* a, int n, const string& name)
{
if(n<=0) return NULL;
int mid = n/2;
Person t(name.c_str(),0,"");
if(a[mid]==t) return a+mid;
if(t<a[mid]) return binarysearch(a,mid,name);
return binarysearch(a+mid+1,n-(mid+1),name);
} */
Person* binarysearch(Person* a, int n, const string& name)
{
int b=0, e=n-1;
Person t(name.c_str(),0,"");
while(b<=e){
int mid = (b+e)/2;
if(a[mid]==t) return a+mid;
if(t<a[mid]) e = mid-1;
else b = mid+1;
}
return NULL;
}
int main()
{
Person a[5]={
Person("李霖",21,"新疆乌鲁木齐"),
Person("钟玉龙",23,"大连"),
Person("何军军",20,"重庆农村"),
Person("蒲嗣良",16,"四川绵阳农村"),
Person("王刚",18,"吉林通化")
};
for(int i=0; i<5; i++)
for(int j=i+1; j<5; j++)
if(a[j]<a[i])
swap(a[j],a[i]);
for(int i=0; i<5; i++)
cout << a[i] << endl;
string name;
cout << "请输入姓名:";
cin >> name;
Person* p = binarysearch(a,5,name);
if(p!=NULL) cout << *p << endl;
else cout << "找不到" << endl;
}