c++实现二叉查找树示例实现二叉查找树示例
主要介绍了c++实现二叉查找树示例,实现二叉查找树的基本功能,需要的朋友可以参考下
复制代码 代码如下:
/**
实现二叉查找树的基本功能
*/
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <string>
using namespace std;
const int M = 10000;
//定义数据节点
class dNode{
public:
string name;
int age;
bool sex;
dNode(){
age = 0;
name = "no name";
sex = true;//nan
}
dNode(string name, int age, bool sex):name(name), age(age), sex(sex){}
//打印节点
void show(){
cout << "name: " << this->name << endl;
cout << "age: " << this->age << endl;
cout << "sex: " << this->sex << endl;
cout << "******************************" << endl;
}
//重载赋值符号
bool operator = (const dNode &d){
this->age = d.age;
this->name = d.name;
this->sex = d.sex;
}
//重载相等符号
bool operator == (const dNode &d){
return name == d.name && age == d.age && sex == sex;
}
//按照年龄重载大于符号
bool operator > (const dNode &d){
return age > d.age;
}
//按照年龄重载小于符号
bool operator < (const dNode &d){
return age < d.age;
}
};
//定义二叉查找树的节点
//这里规定树中没有重复节点,这里需要对一个节点记录出现多少次
class bstNode{
public:
bstNode *left;
bstNode *right;
bstNode *parent; //执行父亲,便于向上访问,如果数据量大,并且向上找的使用率不大就不要来减少空间
dNode data; //该节点在树中出现的次数
int count;