C++实现四叉树(附完整源码)
C++实现四叉树
#ifndef __QuardTree__
#define __QuardTree__
#include <vector>
#include <iostream>
using namespace std;
template<typename T>
struct Point{
T x;
T y;
Point(){
}
Point(T _x, T _y) :x(_x), y(_y){
}
};
template<typename T>
struct Node{
Node* R[4];
Point<T> pt;
Node* parent;
};
template<typename ElemType>
class QuardTree
{
public:
QuardTree();
~QuardTree();
void Insert(const Point<ElemType>& pos);
void BalanceInsert(const Point<ElemType>& pos );
int nodeCount();
int TPLS();
int Height();
void RegionResearch(ElemType left, ElemType right, ElemType botom, ElemType top, int& visitednum,int& foundnum);
void clear();
private:
Node<ElemType>* root;
int Compare(const Node<ElemType>* node, const Point<ElemType>& pos);
bool In_Region(Point<ElemType> t, ElemType left, ElemType right, ElemType botom, ElemType top);
bool Rectangle_Overlapse_Region(ElemType L, ElemType R, ElemType B, ElemType T, ElemType left, ElemType right, ElemType botom, ElemType top);
void RegionResearch(Node<ElemType>* t, ElemType left, ElemType right, ElemType botom, ElemType top, int& visitednum, int& foundnum);
int Depth(Node<ElemType>* &);
int nodeCount(const Node<ElemType>*);
void clear(Node < ElemType>*& p);
void Insert(Node<ElemType>*& , const Point<ElemType>& pos);//递归插入节点
};
template<typename T>
QuardTree<T>::QuardTree()
{
root = NULL;
}
template<typename T>
QuardTree<T>::~QuardTree()
{
clear(root);
}
template<typename T>
int QuardTree<T>::TPLS()
{
return Depth(root);
}
template<typename T>
int QuardTree<T>::Compare(const Node<T>* node, const Point<T>& pos)
{
if (pos.x == node->pt.x && pos.y == node->pt.y) return 0;
if (pos.x >= node->pt.x && pos.y>node->pt.y) return 1;
if (pos.x&