tree.py 模板

# -*- coding: utf-8 -*-tree.py模板

#
# Copyright (C) 2010-2013 Renstech Ltd. All Rights Reserved
#
# This file is part of supernova.
#
# Authors:
# 'zhouyifeng'

# pylint: disable-msg=E1101

import json


class Node:
    def __init__(self, name, value, level=0):
        self.name = name
        self.value = value
        self.level = level
        self.url = None
        self.target = '_self'
        self.open = False
        self.path = name
        self.icon = None
        self.iconClose = None
        self.iconOpen = None
        self.parent = None
        self.children = []

    @property
    def isParent(self):
        return len(self.children) > 0

    def add_node(self, node):
        """
        添加子节点
        :param node: 所要添加的子节点
        :type node: Node
        """

        node.level = self.level + 1
        node.parent = self
        node.path = self.path + '/' + node.name
        self.children.append(node)

    def insert_node(self, index, node):
        node.level = self.level + 1
        node.parent = self
        node.path = self.path + '/' + node.name
        self.children.insert(index, node)

    def remove_node(self, node):
        self.children.remove(node)

    def pop_node(self):
        self.children.pop()

    def clear(self):
        self.children = []

    def get_node(self, name):
        for child in self.children:
            if child.name == name:
                return child

        return None

    def find_node(self, value):
        for child in self.children:
            if child.value == value:
                return child

            node = child.find_node(value)
            if node:
                return node

        return None

    def search_node(self, name):
        for child in self.children:
            if child.name == name:
                return child

            node = child.search_node(name)
            if node:
                return node

        return None

    def sort(self):
        self.children.sort(lambda x, y: cmp(x.value, y.value))
        for child in self.children:
            child.sort()

    def sort_by_name(self):
        self.children.sort(lambda x, y: cmp(x.name, y.name))
        for child in self.children:
            child.sort_by_name()

    def show(self, layer=0):
        print "  " * layer + self.name
        map(lambda child: child.show(layer + 1), self.children)

    def show_value(self, layer=0):
        print "  " * layer + self.value
        map(lambda child: child.show(layer + 1), self.children)

    def to_dict(self):
        result = {'name': self.name,
                  'value': self.value,
                  'level': self.level,
                  'open': self.open,
                  'path': self.path}

        if self.url:
            result['url'] = self.url
            result['target'] = self.target
        if self.icon:
            result['icon'] = self.icon
        if self.iconClose:
            result['iconClose'] = self.iconClose
        if self.iconOpen:
            result['iconOpen'] = self.iconOpen
        if self.children:
            result['children'] = [child.to_dict() for child in self.children]

        return result

    def to_json(self):
        return json.dumps(self.to_dict(), indent=4)

    def __unicode__(self):
        return self.name

    def __str__(self):
        return self.name


class Tree:
    def __init__(self, name=None):
        self.name = name
        self.nodes = []

    def to_json(self):
        return json.dumps([node.to_dict() for node in self.nodes], indent=4)

    def add_node(self, node):
        """
        树中添加节点
        :param node:所要添加的节点
        :type node:Node
        """

        node.level = 0
        node.parent = self
        self.nodes.append(node)

    def insert_node(self, index, node):
        node.level = 0
        node.parent = self
        self.nodes.insert(index, node)

    def remove_node(self, node):
        self.nodes.remove(node)

    def pop(self):
        self.nodes.pop()

    def sort(self):
        self.nodes.sort(lambda x, y: cmp(x.value, y.value))
        for node in self.nodes:
            node.sort()

    def sort_by_name(self):
        self.nodes.sort(lambda x, y: cmp(x.name, y.name))
        for node in self.nodes:
            node.sort_by_name()

    def get_node(self, name):
        for node in self.nodes:
            if node.name == name:
                return node

        return None

    def find_node(self, value):
        for node in self.nodes:
            if node.value == value:
                return node

            child_node = node.find_node(value)
            if child_node:
                return child_node

        return None

    def search_node(self, name):
        for node in self.nodes:
            if node.name == name:
                return node

            child_node = node.search_node(name)
            if child_node:
                return child_node

        return None

    def __unicode__(self):
        return self.name

    def __str__(self):
        return self.name


if __name__ == '__main__':
    root = Node("root", "root")

    sub_node1 = Node("a1", "a1")
    root.add_node(sub_node1)
    sub_node1.add_node(Node("a2", "a2"))
    sub_node1.add_node(Node("b2", "b2"))

    sub_node2 = Node("b1", "b1")
    root.add_node(sub_node2)
    sub_node2.add_node(Node("d2", "d2"))
    sub_node2.add_node(Node("c2", "c2"))

    print root.to_json()

    root.sort()
    print root.to_json()

    node = root.find_node("c2")
    if node:
        print node
    else:
        print "not find"

    node = root.find_node("c3")
    if node:
        print node
    else:
        print "not find"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

忆枫717

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值