Python实现"建立四叉树"的一种方法

我们想要使用一棵四叉树来储存一个 N x N的布尔值网络。网络中每一格的值只会是真或假。树的根结点代表整个网络。对于每个结点, 它将被分等成四个孩子结点直到这个区域内的值都是相同的.

每个结点还有另外两个布尔变量: isLeafvalisLeaf当这个节点是一个叶子结点时为真。val 变量储存叶子结点所代表的区域的值。

你的任务是使用一个四叉树表示给定的网络。下面的例子将有助于你理解这个问题:

给定下面这个8 x 8网络,我们将这样建立一个对应的四叉树:

由上文的定义,它能被这样分割:

对应的四叉树应该像下面这样,每个结点由一对(isLeaf, val)所代表.

对于非叶子结点,val可以是任意的,所以使用 *代替。

提示:

N将小于 1000 且确保是 2 的整次幂。
如果你想了解更多关于四叉树的知识,你可以参考这个 wiki 页面。

1、递归实现,引进numpy包操作数组

def construct(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: Node
        """
        import numpy as np      #引入numpy
        if len(grid) == 1:      #矩阵为1阶
            return Node(grid[0][0], True, None, None, None, None)
        real = False
        fatal = False
        for row in grid:    #判断当前矩阵的真假值
            if True in row:
                real = True
            if False in row:
                fatal = True
            if real and fatal:
                break
        if real and not fatal:     #当前矩阵为真
            return Node(True, True, None, None, None, None)
        if not real and fatal:      #当前矩阵为假
            return  Node(False, True, None, None, None, None)
        if real and fatal:          #当前矩阵既包含真值又包含假值
            grid = np.array(grid)   #二维列表转数组,方便下标操作
            topleft = self.construct(grid[0:len(grid) // 2, 0:len(grid) // 2].tolist())
            topRight = self.construct(grid[0:len(grid) // 2, len(grid) // 2:].tolist())
            bottomLeft = self.construct(grid[len(grid) // 2:, 0:len(grid) // 2].tolist())
            bottomRight = self.construct(grid[len(grid) // 2:, len(grid) // 2:].tolist())
            return Node("*", False, topleft, topRight, bottomLeft, bottomRight)

不引用numpy切割数组(参考他人)

def construct(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: Node
        """
        if len(grid) == 1:      #矩阵为1阶
            return Node(grid[0][0], True, None, None, None, None)
        real = False
        fatal = False
        for row in grid:    #判断当前矩阵的真假值
            if True in row:
                real = True
            if False in row:
                fatal = True
            if real and fatal:
                break
        if real and not fatal:     #当前矩阵为真
            return Node(True, True, None, None, None, None)
        if not real and fatal:      #当前矩阵为假
            return  Node(False, True, None, None, None, None)
        if real and fatal:          #当前矩阵既包含真值又包含假值,将矩阵等分四份
            topleft = self.construct([row[0:len(grid) // 2] for row in grid[0:len(grid) // 2]])
            topRight = self.construct([row[len(grid) // 2:] for row in grid[0:len(grid) // 2]])
            bottomLeft = self.construct([row[0:len(grid) // 2] for row in grid[len(grid) // 2:]])
            bottomRight = self.construct([row[len(grid) // 2:] for row in grid[len(grid) // 2:]])
            return Node("*", False, topleft, topRight, bottomLeft, bottomRight)

算法题来自:https://leetcode-cn.com/problems/construct-quad-tree/description/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值