Implementation Code of a quad-tree

Source: Internet
Author: User

Implementation Code of a quad-tree

Class quadtreecode
{
Public:
Vector <int> m_numbers;

/* Determine whether two quad-tree codes are equal */
Bool operator = (quadtreecode & temptree)
{
If (m_numbers.size ()! = Temptree. m_numbers.size ())
{
Return false;
}
Else
{
For (INT I = 0; I <m_numbers.size (); I ++)
{
If (m_numbers [I]! = Temptree. m_numbers [I])
{
Return false;
}
}
}

Return true;
}

/* Return the length of the quad-Tree Code */
Int getlength ()
{
Return m_numbers.size ();
}

Int operator [] (INT index)
{
Return m_numbers [Index];
}

};

Enum childtype
{
Ul = 0,
Ur = 3,
LL = 1,
LR = 2
};
Template <class T>
Class quadtreenode
{
Public:
T * m_pdata;
Quadtreenode * m_pupperleft, * m_pupperright, * m_plowerleft, * m_plowerright;
Quadtreecode m_code; // node encoding in the tree
Quadtreenode ()
{
M_pdata = NULL;
M_pupperleft = m_pupperright = m_plowerleft = m_plowerright = NULL;
}

~ Quadtreenode ()
{
Delete m_pdata;
}

/* Return the address of the sub-member */
Quadtreenode ** getchild (childtype ctype)
{
Switch (ctype)
{
Case childtype: ul:
Return & m_pupperleft;
Break;

Case childtype: UR:
Return & m_pupperright;
Break;

Case childtype: LL:
Return & m_plowerleft;
Break;

Case childtype: LR:
Return & m_plowerright;
Break;
}
}
};

Template <class T>
Class Quadtree
{
Public:
Int m_ntreedepth; // depth of the tree
Quadtreenode <t> * m_pheadnode; // the header of the tree.
Quadtree ()
{
M_ntreedepth = 0;
M_pheadnode = NULL;
}

~ Quadtree ()
{
Void (quadtree: * func) (quadtreenode <t> *);
Func = & quadtree: destroynode;
Postorderoperation (m_pheadnode, func );
}

/*
Operation of the quad-tree by post-order traversal
*/
Void postorderoperation (quadtreenode <t> * ptempnode, void (quadtree <t>: * nodeop) (quadtreenode <t> *))
{
If (ptempnode! = NULL)
{
Postorderoperation (ptempnode-> m_plowerleft, nodeop );
Postorderoperation (ptempnode-> m_plowerright, nodeop );
Postorderoperation (ptempnode-> m_pupperleft, nodeop );
Postorderoperation (ptempnode-> m_pupperright, nodeop );
(This-> * nodeop) (ptempnode );
}
}

Void destroynode (quadtreenode <t> * ptempnode)
{
Delete ptempnode;
}

/* Create branches */
Void createbranch (quadtreenode <t> ** ppnode, int treedepth, int currentdepth)
{
If (currentdepth> treedepth)
{
Return;
}
Else
{
Quadtreenode <t> * pnewnode = new quadtreenode <t>;
* Ppnode = pnewnode;
Quadtreenode <t> ** ptempnode;
Createbranch (pnewnode-> getchild (childtype: UL), treedepth, currentdepth + 1 );
Createbranch (pnewnode-> getchild (childtype: UR), treedepth, currentdepth + 1 );
Createbranch (pnewnode-> getchild (childtype: LL), treedepth, currentdepth + 1 );
Createbranch (pnewnode-> getchild (childtype: LR), treedepth, currentdepth + 1 );
}

}

/* Perform operations according to the quad-Tree Code */
Bool operatenodebycode (quadtreecode code, void (* op) (quadtreenode <t> *))
{
Quadtreenode * ptempnode = m_pheadnode;
For (INT I = 0; I <code. getlength (); I ++)
{
Ptempnode = ptempnode-> getchild (code [I]);
If (ptempnode = NULL)
Return false;
}
OP (ptempnode );
Return true;
}

/* The memory structure is created near, and the data content is not assigned a value */
Void createtree (INT treedepth)
{
M_ntreedepth = treedepth;
Createbranch (& m_pheadnode, treedepth, 0 );
}
// Virtual void createnode (quadtreenode <t> * ptempnode) const = 0;
};

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/yhforchina/archive/2008/02/22/2114029.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.