python 实现 四叉树(二维平面进行管理的)

我的梦境 提交于 2020-02-18 21:40:48
# -*- coding: utf-8 -*-
'''

@author: zou_albert
'''
import pdb
class Node():    #  普通节点  反复调用
    def __init__(self, parent ,options, pos_num):  ## pos_num  代表子节点的那个方位的数字
        self.depth = parent.depth + 1
        # if (self.depth >= options.tree_deep - 1):
        #
        #     print "ERROR!!!! Maximum depth exceeded"
        #     exit()

        # pdb.set_trace()
        self.parent_id = 0
        self.is_leaf = 0   #  是否是叶节点呢  暂时可能不用
        self.is_root = 0

        self.parent = parent    # 父母节点

        # if 1 > 0:
        #     print "zoujunboq"
        deltax = (parent.xmax - parent.xmin) / 2
        deltay = (parent.ymax - parent.ymin) / 2
        # if(self.depth == options.tree_deep - 1):   根据这个判断没有叶节点的  应该不用的

        ## pos_num = 0 up_left , 1 up_right,  2  down_left,  3  down_left,
        if(pos_num == 0):  #up_left

            self.xmin = parent.xmin         # 节点覆盖区域的正方形的边界的数值
            self.ymin = parent.ymax - deltay
            self.xmax = parent.xmin + deltax
            self.ymax = parent.ymax   # 节点覆盖区域的正方形的边界的数值
        elif(pos_num == 1):   ## pos_num = 0 up_left , 1 up_right,  2  down_left,  3  down_left,

            self.xmin = parent.xmax - deltax
            self.ymin = parent.ymax - deltay
            self.xmax = parent.xmax
            self.ymax = parent.ymax   # 节点覆盖区域的正方形的边界的数值
        elif(pos_num == 3):   ## pos_num = 0 up_left , 1 up_right,  2  down_left,  3  down_left,
            self.xmax = parent.xmax
            self.xmin = parent.xmax - deltax
            self.ymin = parent.ymin
            self.ymax = parent.ymin + deltay
        else:
            self.xmin = parent.xmin
            self.xmax = parent.xmin + deltax
            self.ymin = parent.ymin
            self.ymax = parent.ymin + deltay

        # print "zoujunbo",i
        # i += 1
        self.xc = (self.xmin + self.xmax) / 2.0
        self.yc = (self.ymin + self.ymax) / 2.0   #  节点覆盖区域的正方形的中心节点的坐标

        self.up_left =   0           ## pos_num = 0 up_left , 1 up_right,  2  down_left,  3  down_left,
        self.up_right =  0
        self.down_left = 0
        self.down_right = 0          ## 四个儿子节点是哪一个的  需要在子节点之中进行赋值的,暂时随便初始化一下的

class rootNode():
    def __init__(self, options):

        self.depth = 0
        self.parent  = 0

        self.xmin = 0
        self.ymin = 0
        self.xmax =  options.dim
        self.ymax =  options.dim

        self.xc = (self.xmin + self.xmax) / 2.0
        self.yc = (self.ymin + self.ymax) / 2.0

        self.up_left = 0    ## pos_num = 0 up_left , 1 up_right,  2  down_left,  3  down_left,
        self.up_right = 0
        self.down_left = 0
        self.down_right = 0

        self.parent_id = 0
        self.is_leaf = 0  # 是否是叶节点呢  暂时可能不用
        self.is_root = 0



class Tree():
    def __init__(self, options):

        self.root = rootNode(options)
        self.treeQueue = []
        self.treeQueue.append(self.root)


        self.add_four_child_node(options, self.root, self.treeQueue, True)


    def add_four_child_node(self, options, parent, treeQueue, end = False):  ###  进行递归 初始化所有的节点
        # if(end == True):
        #     global tree_depthddd
        #     # tree_depthddd = options.tree_deep -1
        #     tree_depthddd = 2


        if parent.depth == 5:   ### 四叉树一共   6层  0-5的开始的
            return 0   ### 在此处进行返回,跳出函数的结构的

        if parent != 0:
            

            node0 = Node(parent, options, 0)
            treeQueue.append(node0)
            parent.up_left = node0



            node1 = Node(parent, options, 1)
            treeQueue.append(node1)
            parent.up_right = node1

            node2 = Node(parent, options, 2)
            treeQueue.append(node2)
            parent.down_left = node2

            node3 = Node(parent, options, 3)
            treeQueue.append(node3)
            parent.down_right = node3



            # print treeQueue
            self.add_four_child_node(options, node0, treeQueue)
            self.add_four_child_node(options, node1, treeQueue)
            self.add_four_child_node(options, node2, treeQueue)
            self.add_four_child_node(options, node3, treeQueue)








            # for pos_num in range(4):
            #     node = Node(parent, options, pos_num)
            #     treeQueue.append(node)
            #     print treeQueue
            #     if(pos_num == 0):
            #         parent.up_left = node
            #     elif(pos_num == 1):
            #         parent.up_right = node
            #     elif(pos_num == 2):
            #         parent.down_left = node
            #     else:
            #         parent.down_right = node
            #     self.add_four_child_node(options, node, treeQueue)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!