Four-fork tree and eight-fork tree

Source: Internet
Author: User
Tags border color data structures

Pre-order

A quadtree or four-dollar Tree is also called the Q-tree (Q-tree). Four fork tree is widely used in image processing, spatial data index, fast collision detection in 2D, storage sparse data, and eight-fork tree (octree) is mainly applied to 3D graphics processing. This can be useful for programming games. This paper focuses on the introduction of the principle and structure of quadtree and eight-fork tree, which helps you to establish the basic idea of four-fork tree and eight-fork tree in your mind. This article does not explain the two data structures at the same time, but only the four-fork tree, because the establishment of the eight-fork tree can be driven by the establishment of Quadtree. If there are shortcomings, hope can not hesitate to point out, to improve. ^_^ Welcome email:zhanxinhang@gmail.com

structure and principle of four-fork tree and eight-fork tree

Quadtree (Q-tree) is a tree-shaped data structure. Quadtree is defined as: it can have up to four sub-nodes per node, usually a part of the two-dimensional space is subdivided into four quadrants or regions and the relevant information in the area into the four Fork tree node. This area can be a square, a rectangle, or any shape. The following is a schematic of a two-dimensional spatial structure (left) and a storage structure (right) for a four-fork tree (note node color and grid border color):

Each node of the quadtree represents a rectangular region (as the black root node above represents the rectangular area of the outermost black border), and each rectangular area can be divided into four small rectangular regions, which are four small rectangular regions as the rectangular regions represented by the four child nodes.

The eight-fork tree extends the scene from two-dimensional space to three-dimensional space compared to quadtree. The eight-fork Tree (octree) is defined as: if it is not empty, any node in the tree will have just eight child nodes, or 0, that is, the child nodes will not have 0 and 8 of the number. So, what does this have to do. Imagine a cube in which we can cut at least as many small cubes as equal. The answer is 8. The structure diagram of the following Yagi tree is shown below:

The C-language description of the QUADTREE storage structure:

/* Quadrant Division of a rectangular region::
          
       UL (1)   |    UR (0)
     ----------|-----------
       LL (2)   |    LR (3) The
following enumeration of the quadrant types
*/
typedef enum
{
    UR = 0,
    UL = 1,
    LL = 2,
    LR = 3
} Quadrantenum;

/* Rectangle structure */
typedef struct QUADRECT_T
{    
    double left  , 
            top, right 
            , 
            bottom;
} quadrect_t;

/* Four fork tree node type structure */
typedef struct QUADNODE_T
{
    quadrect_t    rect;          The rectangular region represented by the node
    list_t        *lst_object;   node data, the node type is generally linked list, can store multiple objects
    struct  quadnode_t  *sub[4];//four children pointing node 
}quadnode_t;

/* Four fork tree type structure */
typedef struct QUADTREE_T
{
    quadnode_t  *root;
    int         depth;           Depth}quadtree_t of Quadtree                    
;


Four establishment of a fork tree

1, the use of Quadtree sub-grid, such as the first picture of this paper < four-storey complete quadtree structure diagram, according to the grid graph on the left graph to create a complete quadtree as shown in the figure.

Pseudo code:

Funtion quadtreebuild (depth, rect)

{

quadtree->depth = depth;


/* Create a branch, root of root tree, depth depth, rectangular area represented by rect root node */

Quadcreatebranch (root, depth, rect);

}


Funtion quadcreatebranch (n, Depth,rect)

{

if (depth!=0)

{

n = new node; Opening new nodes

n->rect = rect; Stores the rectangular area represented by the node in the node

The rect is divided into four parts rect[ur], Rect[ul], Rect[ll], RECT[LR];


/* Create each child branch */

Quadcreatebranch (N->sub[ur], depth-1, rect [UR]);

Quadcreatebranch (N->sub[ul], depth-1, rect [UL]);

Quadcreatebranch (N->sub[ll], depth-1, rect [LL]);

Quadcreatebranch (N->SUB[LR], depth-1, rect [LR]);

}

}

2, assume that there are n objects in a rectangular area, as shown below a black dot represents an object, each object's coordinate position is known, with a four-tree node to store an object, constructed as shown in the right image of the four-fork tree.


The method also uses the recursive method to divide the partition block of the rectangle, and then divides it into the square until it contains only one object in each sub-rectangular area.

Pseudo code:

Funtion Quadtreebuild ()

{

Quadtree = {empty};
for (i = 1;i<n;i++)//Traverse all objects

{

Quadinsert (i, root);//Inserting I objects into quadtree

}

Eliminate redundant nodes;//After performing the above loop, there may be a leaf node in the quadtree that has empty data to be removed
}


Funtion Quadinsert (i,n)//The number of objects stored by each node in the four fork tree after the function is inserted is either 1 or 0

{

if (node n has children)

{

Which child node C that I should put on the N node by dividing the area?

Quadinsert (I,C);

}

else if (node n stores an object)

{

Create four children for n nodes;

Move the object in the N node to the child node where it should be placed;

Which child node C that I should put on the N node by dividing the area?

Quadinsert (I,C);

}

else if (n node data is empty)

{

Store I in node n;

}

}


(The above two methods of establishment are used as extrapolate)


find an object with a four-fork tree

1, using blind search, similar to the recursive traversal of the two-fork tree, can search for an object by post-order traversal or pre-sequence traversal or in-sequence traversal, and the time complexity is O (n).

2, according to the location of the object in the region to search, the use of divide and conquer thought, time complexity only with the depth of the four fork tree. The more the search is in the region, the more obvious it is, than the blind search.


Pseudo code:

Funtion Find (n, POS,)

{

If (the position of the object stored by the N node is the position indicated by POS)

Return N;

If (Pos in First quadrant)

temp = Find (N->sub[ur], POS);

else if (Pos in second quadrant)

temp = Find (N->sub[ul], POS);

else if (Pos in the third quadrant)

temp = Find (N->sub[ll], POS);

else//pos in Quadrant four

temp = Find (N->SUB[LR], POS);

return temp;

}

Conclusion:

Mature words: The method of structure, the way of algorithm. More than one kind of data structure to solve the problem of the method, more than one method on a more than one mode of thinking. Wish you a happy study. ^_^

==============================================

Disclaimer: Copyright, reprint Please specify Source: http://blog.csdn.net/zhanxinhang/article/details/6706217

Reference: Wikipedia, Baidu Encyclopedia

Reference:cs267:lecture, APR 1996 Fast hierarchical Methods for the n-body problem, part 1



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.