Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- class DataPoint{
- public:
- int x, y, z;
- float value;
- DataPoint(int _x = 0, int _y = 0, int _z = 0, float _value = 0)
- {x = _x, y = _y, z = _z, value = _value;}
- bool operator==(DataPoint other){
- return (x == other.x && y == other.y && z == other.z && value == other.value);
- }
- bool operator<(DataPoint other){
- return (x < other.x);
- }
- };
- int BAD_ADRESS = -1;
- class Tree{
- private:
- class TreeNode{
- public:
- DataPoint content;
- TreeNode *lft, *rght;
- TreeNode(DataPoint _content = DataPoint())
- {lft = rght = nullptr, content = _content;}
- } *root;
- public:
- Tree():
- root(nullptr)
- {}
- void add_element(DataPoint _content){
- if (root == nullptr) {root = new TreeNode(_content); return;}
- TreeNode *cur = root;
- while(cur != nullptr){
- if (cur->content < _content){
- if (cur->rght == nullptr) {cur->rght = new TreeNode(_content); break;}
- else cur = cur->rght;
- }else{
- if (cur->lft == nullptr) {cur->lft = new TreeNode(_content); break;}
- else cur = cur->lft;
- }
- }
- }
- DataPoint get_root(){
- return root->content;
- }
- void *find_element(DataPoint _element){
- TreeNode *cur = root;
- while(cur != nullptr){
- if (cur->content == _element) return &(cur->content);
- if (cur->content < _element) cur = cur->rght;
- else cur = cur->lft;
- }
- return &BAD_ADRESS;
- }
- };
- int main(){
- DataPoint a(1, 1, 1, 1);
- Tree *t = new Tree();
- t->add_element(a);
- a = DataPoint(1, 2, 2, 2);
- t->add_element(a);
- void *answer = t->find_element(a);
- if (*static_cast<int*> (answer) == -1) cout << "Error: Not found!\n";
- else{
- DataPoint x = *static_cast<DataPoint*> (answer);
- cout << "{" << x.x << ' ' << x.y << ' ' << x.z << ' ' << x.value << "} was successfully found!\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment