<div class="article_title">
<span class="ico ico_type_Original"></span>
<h1>
<span class="link_title"><a href="/zhanxinhang/article/details/6706217">
<font color="red">[置顶]</font>
四叉树与八叉树
</a>
</span>
</h1>
</div>
<div class="article_manage clearfix">
<div class="article_l">
<span class="link_categories">
标签:
<a href="http://www.csdn.net/tag/%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84" target="_blank" οnclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">数据结构</a><a href="http://www.csdn.net/tag/%e5%ad%98%e5%82%a8" target="_blank" οnclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">存储</a><a href="http://www.csdn.net/tag/struct" target="_blank" οnclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">struct</a><a href="http://www.csdn.net/tag/%e5%9b%be%e5%83%8f%e5%a4%84%e7%90%86" target="_blank" οnclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">图像处理</a><a href="http://www.csdn.net/tag/methods" target="_blank" οnclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">methods</a><a href="http://www.csdn.net/tag/%e5%9b%be%e5%bd%a2" target="_blank" οnclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">图形</a>
</span>
</div>
<div class="article_r">
<span class="link_postdate">2011-08-21 15:34</span>
<span class="link_view" title="阅读次数">36811人阅读</span>
<span class="link_comments" title="评论次数"> <a href="#comments" οnclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_pinglun'])">评论</a>(33)</span>
<span class="link_collect tracking-ad" data-mod="popu_171"> <a href="javascript:void(0);" οnclick="javascript:collectArticle('%e5%9b%9b%e5%8f%89%e6%a0%91%e4%b8%8e%e5%85%ab%e5%8f%89%e6%a0%91','6706217');return false;" title="收藏" target="_blank">收藏</a></span>
<span class="link_report"> <a href="#report" οnclick="javascript:report(6706217,2);return false;" title="举报">举报</a></span>
</div>
</div> <style type="text/css">
.embody{
padding:10px 10px 10px;
margin:0 -20px;
border-bottom:solid 1px #ededed;
}
.embody_b{
margin:0 ;
padding:10px 0;
}
.embody .embody_t,.embody .embody_c{
display: inline-block;
margin-right:10px;
}
.embody_t{
font-size: 12px;
color:#999;
}
.embody_c{
font-size: 12px;
}
.embody_c img,.embody_c em{
display: inline-block;
vertical-align: middle;
}
.embody_c img{
width:30px;
height:30px;
}
.embody_c em{
margin: 0 20px 0 10px;
color:#333;
font-style: normal;
}
</style>
<script type="text/javascript">
$(function () {
try
{
var lib = eval("("+$("#lib").attr("value")+")");
var html = "";
if (lib.err == 0) {
$.each(lib.data, function (i) {
var obj = lib.data[i];
//html += '<img src="' + obj.logo + '"/>' + obj.name + " ";
html += ' <a href="' + obj.url + '" target="_blank">';
html += ' <img src="' + obj.logo + '">';
html += ' <em><b>' + obj.name + '</b></em>';
html += ' </a>';
});
if (html != "") {
setTimeout(function () {
$("#lib").html(html);
$("#embody").show();
}, 100);
}
}
} catch (err)
{ }
});
</script>
<div class="category clearfix">
<div class="category_l">
<img src="http://static.blog.csdn.net/images/category_icon.jpg">
<span>分类:</span>
</div>
<div class="category_r">
<label οnclick="GetCategoryArticles('806341','zhanxinhang','top','6706217');">
<span οnclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_fenlei']);">2.数据结构与算法<em>(11)</em></span>
<img class="arrow-down" src="http://static.blog.csdn.net/images/arrow_triangle _down.jpg" style="display:inline;">
<img class="arrow-up" src="http://static.blog.csdn.net/images/arrow_triangle_up.jpg" style="display:none;">
<div class="subItem">
<div class="subItem_t"><a href="http://blog.csdn.net/zhanxinhang/article/category/806341" target="_blank">作者同类文章</a><i class="J_close">X</i></div>
<ul class="subItem_l" id="top_806341">
</ul>
</div>
</label>
</div>
</div>
<div class="bog_copyright">
<p class="copyright_p">版权声明:本文为博主原创文章,未经博主允许不得转载。</p>
</div>
<div id="article_content" class="article_content tracking-ad" data-mod="popu_307" data-dsm="post">
<p><span style="font-size:16px"><strong>前序</strong></span></p>
<p>转载自:http://blog.csdn.net/zhanxinhang/article/details/6706217四叉树或四元树也被称为Q树(Q-Tree)。<strong>四叉树广泛应用于图像处理、空间数据索引、2D中的快速碰撞检测、存储稀疏数据等,而八叉树<span style="font-family:arial,宋体,sans-serif; line-height:24px; font-size:13px">(Octree)</span>主要应用于3D图形处理。对游戏编程,这会很有用。</strong>本文着重于对四叉树与八叉树的原理与结构的介绍,帮助您在脑海中建立四叉树与八叉树的基本思想。本文并不对这两种<a href="http://lib.csdn.net/base/datastructure" class="replace_word" title="算法与数据结构知识库" target="_blank" style="color:#df3434; font-weight:bold;">数据结构</a>同时进行详解,而只对四叉树进行详解,因为八叉树的建立可由四叉树的建立推得。若有不足之处,望能不吝指出,以改进之。^_^
欢迎Email:<u><span style="font-size:13px"><a href="http://write.blog.csdn.net/postedit/zhanxinhang@gmail.com" target="_blank"><span style="color:rgb(153,153,153)">zhanxinhang@gmail.com</span></a></span></u></p>
<p><strong><span style="font-size:16px">四叉树与八叉树的结构与原理</span></strong></p>
<p>四叉树(Q-Tree)是一种树形数据结构。四叉树的定义是:它的每个节点下至多可以有四个子节点,通常把一部分二维空间细分为四个象限或区域并把该区域里的相关信息存入到四叉树节点中。这个区域可以是正方形、矩形或是任意形状。以下为四叉树的二维空间结构(左)和存储结构(右)示意图(注意节点颜色与网格边框颜色):</p>
<p style="text-align:center"> <img src="http://hi.csdn.net/attachment/201108/20/0_1313865588OPCK.gif" alt=""></p>
<p style="text-align:left">四叉树的每一个节点代表一个矩形区域(如上图黑色的根节点代表最外围黑色边框的矩形区域),每一个矩形区域又可划分为四个小矩形区域,这四个小矩形区域作为四个子节点所代表的矩形区域。<br>
</p>
<p>较之四叉树,八叉树将场景从二维空间延伸到了三维空间。<span style="font-family:arial,宋体,sans-serif; line-height:24px"><span style="font-size:13px">八叉树(Octree)的定义是:若不为空树的话,树中任一节点的子节点恰好只会有八个,或零个,也就是子节点不会有0与8以外的数目。那么,这要用来做什么?想象一个立方体,我们最少可以切成多少个相同等分的小立方体?</span></span><span style="font-family:arial,宋体,sans-serif; line-height:24px"><span style="font-size:13px">答案就是8个</span></span><span style="font-family:arial,宋体,sans-serif; font-size:15px; line-height:24px">。</span>如下八叉树的结构示意图所示:</p>
<p style="text-align:center"> <img src="http://hi.csdn.net/attachment/201108/20/0_1313865581gEU3.gif" alt=""></p>
<p> </p>
<p>四叉树存储结构的<a href="http://lib.csdn.net/base/c" class="replace_word" title="C语言知识库" target="_blank" style="color:#df3434; font-weight:bold;">C语言</a>描述:</p>
<p></p>
<div class="dp-highlighter bg_cpp"><div class="bar"><div class="tools"><b>[cpp]</b> <a href="#" class="ViewSource" title="view plain" οnclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" target="_blank">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" οnclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" target="_blank">copy</a><div style="position: absolute; left: 703px; top: 1542px; width: 18px; height: 18px; z-index: 99;"><embed id="ZeroClipboardMovie_1" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" name="ZeroClipboardMovie_1" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=1&width=18&height=18" wmode="transparent" width="18" height="18" align="middle"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" οnclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" target="_blank">print</a></span><a href="#" class="About" title="?" οnclick="dp.sh.Toolbar.Command('About',this);return false;" target="_blank">?</a></div></div><ol start="1" class="dp-cpp"><li class="alt"><span><span class="comment">/* 一个矩形区域的象限划分::</span> </span></li><li class=""><span><span class="comment"> </span> </span></li><li class="alt"><span><span class="comment"> UL(1) | UR(0)</span> </span></li><li class=""><span><span class="comment"> ----------|-----------</span> </span></li><li class="alt"><span><span class="comment"> LL(2) | LR(3)</span> </span></li><li class=""><span><span class="comment">以下对该象限类型的枚举</span> </span></li><li class="alt"><span><span class="comment">*/</span><span> </span></span></li><li class=""><span><span class="keyword">typedef</span><span> </span><span class="keyword">enum</span><span> </span></span></li><li class="alt"><span>{ </span></li><li class=""><span> UR = 0, </span></li><li class="alt"><span> UL = 1, </span></li><li class=""><span> LL = 2, </span></li><li class="alt"><span> LR = 3 </span></li><li class=""><span>}QuadrantEnum; </span></li><li class="alt"><span> </span></li><li class=""><span><span class="comment">/* 矩形结构 */</span><span> </span></span></li><li class="alt"><span><span class="keyword">typedef</span><span> </span><span class="keyword">struct</span><span> quadrect_t </span></span></li><li class=""><span>{ </span></li><li class="alt"><span> <span class="datatypes">double</span><span> left, </span></span></li><li class=""><span> top, </span></li><li class="alt"><span> right, </span></li><li class=""><span> bottom; </span></li><li class="alt"><span>}quadrect_t; </span></li><li class=""><span> </span></li><li class="alt"><span><span class="comment">/* 四叉树节点类型结构 */</span><span> </span></span></li><li class=""><span><span class="keyword">typedef</span><span> </span><span class="keyword">struct</span><span> quadnode_t </span></span></li><li class="alt"><span>{ </span></li><li class=""><span> quadrect_t rect; <span class="comment">//节点所代表的矩形区域</span><span> </span></span></li><li class="alt"><span> list_t *lst_object; <span class="comment">//节点数据, 节点类型一般为链表,可存储多个对象</span><span> </span></span></li><li class=""><span> <span class="keyword">struct</span><span> quadnode_t *sub[4]; </span><span class="comment">//指向节点的四个孩子 </span><span> </span></span></li><li class="alt"><span>}quadnode_t; </span></li><li class=""><span> </span></li><li class="alt"><span><span class="comment">/* 四叉树类型结构 */</span><span> </span></span></li><li class=""><span><span class="keyword">typedef</span><span> </span><span class="keyword">struct</span><span> quadtree_t </span></span></li><li class="alt"><span>{ </span></li><li class=""><span> quadnode_t *root; </span></li><li class="alt"><span> <span class="datatypes">int</span><span> depth; </span><span class="comment">// 四叉树的深度 </span><span> </span></span></li><li class=""><span>}quadtree_t; </span></li></ol></div><pre name="code" class="cpp" style="display: none;">/* 一个矩形区域的象限划分::
UL(1) | UR(0)
----------|-----------
LL(2) | LR(3)
以下对该象限类型的枚举
*/
typedef enum
{
UR = 0,
UL = 1,
LL = 2,
LR = 3
}QuadrantEnum;
/* 矩形结构 */
typedef struct quadrect_t
{
double left,
top,
right,
bottom;
}quadrect_t;
/* 四叉树节点类型结构 */
typedef struct quadnode_t
{
quadrect_t rect; //节点所代表的矩形区域
list_t *lst_object; //节点数据, 节点类型一般为链表,可存储多个对象
struct quadnode_t *sub[4]; //指向节点的四个孩子
}quadnode_t;
/* 四叉树类型结构 */
typedef struct quadtree_t
{
quadnode_t *root;
int depth; // 四叉树的深度
}quadtree_t;</pre><br>
<br>
<p><strong><span style="font-size:16px">四叉树的建立</span></strong></p>
<p></p>
<p>1、利用四叉树分网格,如本文的第一张图<四层完全四叉树结构示意图>,根据左图的网格图形建立如右图所示的完全四叉树。</p>
<p>伪码:</p>
<p><span style="font-family:'Courier New'">Funtion QuadTreeBuild ( depth, rect )</span></p>
<p><span style="font-family:'Courier New'"> {</span></p>
<blockquote style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:40px; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; border-width:initial; border-color:initial; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">
<p><span style="font-family:'Courier New'">QuadTree->depth = depth;</span></p>
<p><span style="font-family:'Courier New'"><br>
</span></p>
<p><span style="font-family:'Courier New'"><span style="font-family:'Courier New'">/*创建分支,root树的根,depth深度,rect根节点代表的矩形区域*/</span><br>
</span></p>
<p><span style="font-family:'Courier New'">QuadCreateBranch ( root, depth, rect );</span></p>
</blockquote>
<p><span style="font-family:'Courier New'"> }</span></p>
<p><span style="font-family:'Courier New'"><br>
</span></p>
<p><span style="font-family:'Courier New'">Funtion QuadCreateBranch ( n, depth,rect )</span></p>
<p><span style="font-family:'Courier New'"> {</span></p>
<blockquote style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:40px; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; border-width:initial; border-color:initial; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">
<p><span style="font-family:'Courier New'">if ( depth!=0 )</span></p>
<p><span style="font-family:'Courier New'"> {</span></p>
</blockquote>
<blockquote style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:40px; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; border-width:initial; border-color:initial; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">
<blockquote style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:40px; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; border-width:initial; border-color:initial; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">
<p><span style="font-family:'Courier New'">n = new node; //开辟新节点</span></p>
<p><span style="font-family:'Courier New'">n ->rect = rect; //将该节点所代表的矩形区域存储到该节点中</span></p>
<p><span style="font-family:'Courier New'">将rect划成四份 rect[UR], rect[UL], rect[LL], rect[LR];</span></p>
<p><span style="font-family:'Courier New'"><br>
</span></p>
<p><span style="font-family:'Courier New'">/*创建各孩子分支*/</span></p>
</blockquote>
</blockquote>
<blockquote style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:40px; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; border-width:initial; border-color:initial; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">
<blockquote style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:40px; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; border-width:initial; border-color:initial; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">
<p><span style="font-family:'Courier New'">QuadCreateBranch ( n->sub[UR], depth-1, rect [UR] );</span></p>
<p><span style="font-family:'Courier New'">QuadCreateBranch ( n->sub[UL], depth-1, rect [UL] );<br>
</span></p>
<p><span style="font-family:'Courier New'">QuadCreateBranch ( n->sub[LL], depth-1, rect [LL] );<br>
</span></p>
<p><span style="font-family:'Courier New'">QuadCreateBranch ( n->sub[LR], depth-1, rect [LR] );<br>
</span></p>
</blockquote>
</blockquote>
<blockquote style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:40px; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; border-width:initial; border-color:initial; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">
<p><span style="font-family:'Courier New'"> }</span></p>
</blockquote>
<p><span style="font-family:'Courier New'"> }</span></p>
<br>
<p>2、假设在一个矩形区域里有N个对象,如下左图一个黑点代表一个对象,每个对象的坐标位置都是已知的,用四叉树的一个节点存储一个对象,构建成如下右图所示的四叉树。</p>
<p style="text-align:center"><img src="http://hi.csdn.net/attachment/201108/20/0_1313865592s6Yc.gif" alt=""><br>
</p>
<p></p>
<p>方法也是采用递归的方法对该矩形进行划分分区块,分完后再往里分,直到每一个子矩形区域里只包含一个对象为止。</p>
<p>伪码:</p>
<p></p>
<p><span style="font-family:'Courier New'">Funtion QuadtreeBuild()</span></p>
<p><span style="font-family:'Courier New'"> {</span></p>
<p><span style="font-family:'Courier New'"> Quadtree = {empty};<br>
For (i = 1;i<n;i++) //遍历所有对象<br>
</span></p>
<blockquote style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:40px; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; border-width:initial; border-color:initial; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">
<p><span style="font-family:'Courier New'">{</span></p>
</blockquote>
<blockquote style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:40px; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; border-width:initial; border-color:initial; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">
<p><span style="font-family:'Courier New'"> QuadInsert(i, root);//将i对象插入四叉树</span></p>
</blockquote>
<blockquote style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:40px; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; border-width:initial; border-color:initial; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">
<p><span style="font-family:'Courier New'">}</span><span style="font-family:'Courier New'"> </span></p>
</blockquote>
<p><span style="font-family:'Courier New'"> 剔除多余的节点; //<span style="font-family:'Courier New'">执行完上面循环后,四叉树中可能有数据为空的叶子节点需要剔除</span><br>
} </span></p>
<p><span style="font-family:'Courier New'"><br>
Funtion QuadInsert(i,n) //该函数插入后四叉树中的每个节点所存储的对象数量不是1就是0</span></p>
<p><span style="font-family:'Courier New'"> {</span><span style="font-family:'Courier New'"> </span></p>
<p><span style="font-family:'Courier New'"> if(节点n有孩子)<br>
</span></p>
<blockquote style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:40px; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; border-width:initial; border-color:initial; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">
<p><span style="font-family:'Courier New'"> { </span></p>
</blockquote>
<blockquote style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:40px; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; border-width:initial; border-color:initial; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">
<p><span style="font-family:'Courier New'"> 通过划分区域判断i应该放置于n节点的哪一个孩子节点c; </span></p>
</blockquote>
<blockquote style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:40px; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; border-width:initial; border-color:initial; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">
<p><span style="font-family:'Courier New'"> QuadInsert(i,c);</span></p>
</blockquote>
<blockquote style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:40px; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; border-width:initial; border-color:initial; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">
<p><span style="font-family:'Courier New'"> }</span></p>
</blockquote>
<p><span style="font-family:'Courier New'"> else if(节点n存储了一个对象)</span></p>
<blockquote style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:40px; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; border-width:initial; border-color:initial; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">
<p><span style="font-family:'Courier New'"> {</span></p>
</blockquote>
<blockquote style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:40px; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; border-width:initial; border-color:initial; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">
<p><span style="font-family:'Courier New'"> 为n节点创建四个孩子;</span></p>
</blockquote>
<blockquote style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:40px; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; border-width:initial; border-color:initial; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">
<p><span style="font-family:'Courier New'"> 将n节点中的对象移到它应该放置的孩子节点中;</span></p>
</blockquote>
<blockquote style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:40px; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; border-width:initial; border-color:initial; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">
<p><span style="font-family:'Courier New'"> 通过划分区域判断i应该放置于n节点的哪一个孩子节点c;</span></p>
</blockquote>
<blockquote style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:40px; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; border-width:initial; border-color:initial; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">
<p><span style="font-family:'Courier New'"> QuadInsert(i,c);</span></p>
<p><span style="font-family:'Courier New'"> }</span></p>
</blockquote>
<p><span style="font-family:'Courier New'"> else if(n节点数据为空) </span></p>
<blockquote style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:40px; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; border-width:initial; border-color:initial; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">
<p><span style="font-family:'Courier New'"> {</span></p>
</blockquote>
<blockquote style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:40px; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; border-width:initial; border-color:initial; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">
<p><span style="font-family:'Courier New'"> 将i存储到节点n中;</span></p>
</blockquote>
<blockquote style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:40px; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; border-width:initial; border-color:initial; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px">
<p><span style="font-family:'Courier New'"> }</span></p>
</blockquote>
<p><span style="font-family:'Courier New'"> } </span></p>
<p><span style="font-family:'Courier New'"><br>
</span></p>
<p>(以上两种建立方法作为举一反三之用)</p>
<p><br>
</p>
<p><strong><span style="font-size:16px">用四叉树查找某一对象</span></strong></p>
<p>1、采用盲目搜索,与二叉树的递归遍历类似,可采用后序遍历或前序遍历或中序遍历对其进行搜索某一对象,时间复杂度为O(n)。</p>
<p> </p>
<p>2、根据对象在区域里的位置来搜索,采用分而治之思想,时间复杂度只与四叉树的深度有关。比起盲目搜索,这种搜索在区域里的对象越多时效果越明显</p>
<p style="text-align:center"><img src="http://hi.csdn.net/attachment/201108/20/0_1313865576IER6.gif" alt=""><br>
</p>
<p>伪码:</p>
<p></p>
<p><span style="font-family:'Courier New'">Funtion find ( n, pos, )</span></p>
<p><span style="font-family:'Courier New'"> {</span></p>
<p><span style="font-family:'Courier New'"> If (n节点所存的对象位置为 pos所指的位置 )</span></p>
<p><span style="font-family:'Courier New'"> Return n;</span></p>
<p><span style="font-family:'Courier New'"> If ( pos位于第一象限 )</span></p>
<p><span style="font-family:'Courier New'"> temp = find ( n->sub[UR], pos );</span></p>
<p><span style="font-family:'Courier New'"> else if ( pos位于第二象限)</span></p>
<p><span style="font-family:'Courier New'"> temp = find ( n->sub[UL], pos );</span></p>
<p><span style="font-family:'Courier New'"> else if ( pos位于第三象限 )</span></p>
<p><span style="font-family:'Courier New'"> temp = find ( n->sub[LL], pos );</span></p>
<p><span style="font-family:'Courier New'"> else //pos 位于第四象限</span></p>
<p><span style="font-family:'Courier New'"> temp = find ( n->sub[LR], pos );</span></p>
<p><span style="font-family:'Courier New'"> return temp; </span></p>
<p><span style="font-family:'Courier New'"> } </span></p>
<p><span style="font-family:'Courier New'; font-size:16px"><strong>结语:</strong></span></p>
<p><span style="font-family:'Courier New'; font-size:13px">熟话说:结构之法,<a href="http://lib.csdn.net/base/datastructure" class="replace_word" title="算法与数据结构知识库" target="_blank" style="color:#df3434; font-weight:bold;">算法</a>之道。多一种数据结构就多一种解决问题的方法,多一种方法就多一种思维模式。祝君学习愉快!^_^</span></p>
<p> ==============================================<span style="color:rgb(153,153,153); font-size:13px"></span></p>
<p><span style="color:rgb(153,153,153); font-size:13px"><span style="color:rgb(153,153,153); font-size:13px">声明:版权所有,转载请注明出处:</span> <a href="http://blog.csdn.net/zhanxinhang/article/details/6706217" target="_blank">http://blog.csdn.net/zhanxinhang/article/details/6706217</a><br>
</span></p>
<span style="color:rgb(153,153,153); font-size:13px"></span>
<p>参考:维基百科、百度百科</p>
<p>参考:<strong><span style="font-size:13px">CS267: Lecture 24, Apr 11 1996 </span><span style="font-weight:normal; font-size:13px">Fast Hierarchical Methods for the N-body Problem, Part 1</span></strong></p>
<p><strong><span style="font-weight:normal; font-size:13px"><br>
</span></strong></p>
<p><strong><span style="font-weight:normal; font-size:13px"><br>
</span></strong></p>
</div>
<!-- Baidu Button BEGIN -->
<div class="bdsharebuttonbox tracking-ad bdshare-button-style0-16" style="float: right;" data-mod="popu_172" data-bd-bind="1500014597584">
<a href="#" class="bds_more" data-cmd="more" style="background-position:0 0 !important; background-image: url(http://bdimg.share.baidu.com/static/api/img/share/icons_0_16.png?v=d754dcc0.png) !important" target="_blank"></a>
<a href="#" class="bds_qzone" data-cmd="qzone" title="分享到QQ空间" style="background-position:0 -52px !important" target="_blank"></a>
<a href="#" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博" style="background-position:0 -104px !important" target="_blank"></a>
<a href="#" class="bds_tqq" data-cmd="tqq" title="分享到腾讯微博" style="background-position:0 -260px !important" target="_blank"></a>
<a href="#" class="bds_renren" data-cmd="renren" title="分享到人人网" style="background-position:0 -208px !important" target="_blank"></a>
<a href="#" class="bds_weixin" data-cmd="weixin" title="分享到微信" style="background-position:0 -1612px !important" target="_blank"></a>
</div>
<script>window._bd_share_config = { "common": { "bdSnsKey": {}, "bdText": "", "bdMini": "1", "bdMiniList": false, "bdPic": "", "bdStyle": "0", "bdSize": "16" }, "share": {} }; with (document) 0[(getElementsByTagName('head')[0] || body).appendChild(createElement('script')).src = 'http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion=' + ~(-new Date() / 36e5)];</script>
<!-- Baidu Button END -->
<!--172.16.140.13-->
<!-- Baidu Button BEGIN -->
<script type="text/javascript" id="bdshare_js" data="type=tools&uid=1536434" src="http://bdimg.share.baidu.com/static/js/bds_s_v2.js?cdnversion=416671"></script>
<script type="text/javascript">
document.getElementById("bdshell_js").src = "http://bdimg.share.baidu.com/static/js/shell_v2.js?cdnversion=" + Math.ceil(new Date()/3600000)
</script>
<!-- Baidu Button END -->
<div id="digg" articleid="6706217">
<dl id="btnDigg" class="digg digg_enable" οnclick="btndigga();">
<dt>顶</dt>
<dd>34</dd>
</dl>
<dl id="btnBury" class="digg digg_enable" οnclick="btnburya();">
<dt>踩</dt>
<dd>1</dd>
</dl>
</div>
<div class="tracking-ad" data-mod="popu_222"><a href="javascript:void(0);" target="_blank"> </a> </div>
<div class="tracking-ad" data-mod="popu_223"> <a href="javascript:void(0);" target="_blank"> </a></div>
<script type="text/javascript">
function btndigga() {
$(".tracking-ad[data-mod='popu_222'] a").click();
}
function btnburya() {
$(".tracking-ad[data-mod='popu_223'] a").click();
}
</script>
<ul class="article_next_prev">
<li class="prev_article"><span οnclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_shangyipian']);location.href='http://blog.csdn.net/zhanxinhang/article/details/6690339';">上一篇</span><a href="http://blog.csdn.net/zhanxinhang/article/details/6690339" οnclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_shangyipian'])">[转]每天读一遍,不久你就会变!</a></li>
<li class="next_article"><span οnclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_xiayipian']);location.href='http://blog.csdn.net/zhanxinhang/article/details/6710285';">下一篇</span><a href="http://blog.csdn.net/zhanxinhang/article/details/6710285" οnclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_xiayipian'])">每日微软面试题——day 7(找数组中唯一出现两次的数)</a></li>
</ul>
<div style="clear:both; height:10px;"></div>
<div class="similar_article">
<h4></h4>
<div class="similar_c" style="margin:20px 0px 0px 0px">
<div class="similar_c_t">
相关文章推荐
</div>
<div class="similar_wrap tracking-ad" data-mod="popu_36">
<ul class="similar_list fl">
<li>
<em>•</em>
<a href="http://blog.csdn.net/Chinamming/article/details/16918733" title="关于二叉树、四叉树和八叉树" strategy="BlogCommendFromBaidu" target="_blank">关于二叉树、四叉树和八叉树</a>
</li>
<li>
<em>•</em>
<a href="http://blog.csdn.net/pizi0475/article/details/45071633" title="四叉树+八叉树" strategy="BlogCommendFromBaidu" target="_blank">四叉树+八叉树</a>
</li>
<li>
<em>•</em>
<a href="http://blog.csdn.net/Augusdi/article/details/36177517" title="四叉树和八叉树概述" strategy="BlogCommendFromBaidu" target="_blank">四叉树和八叉树概述</a>
</li>
<li>
<em>•</em>
<a href="http://blog.csdn.net/ZJU_fish1996/article/details/70121083" title="[图形学] 游戏中的场景管理 - 四叉树 八叉树" strategy="BlogCommendFromBaidu" target="_blank">[图形学] 游戏中的场景管理 - 四叉树 八叉树</a>
</li>
<li>
<em>•</em>
<a href="http://blog.csdn.net/Augusdi/article/details/36001543" title="八叉树Octree" strategy="BlogCommendFromBaidu" target="_blank">八叉树Octree</a>
</li>
</ul>
<ul class="similar_list fr">
<li>
<em>•</em>
<a href="http://blog.csdn.net/Chinamming/article/details/16918523" title="八叉树和十六叉树结构" strategy="BlogCommendFromBaidu" target="_blank">八叉树和十六叉树结构</a>
</li>
<li>
<em>•</em>
<a href="http://blog.csdn.net/viewcode/article/details/8445616" title="八叉树及K-D树的应用和实现" strategy="BlogCommendFromBaidu" target="_blank">八叉树及K-D树的应用和实现</a>
</li>
<li>
<em>•</em>
<a href="http://blog.csdn.net/zhouxuguang236/article/details/12312099" title="四叉树空间索引原理及其实现" strategy="BlogCommendFromBaidu" target="_blank">四叉树空间索引原理及其实现</a>
</li>
<li>
<em>•</em>
<a href="http://blog.csdn.net/Augusdi/article/details/36178609" title="八叉树" strategy="BlogCommendFromBaidu" target="_blank">八叉树</a>
</li>
<li>
<em>•</em>
<a href="http://blog.csdn.net/hjwang1/article/details/52420767" title="四叉树与八叉树" strategy="BlogCommendFromCsdn" target="_blank">四叉树与八叉树</a>
</li>
</ul>
</div>
</div>
</div>