• Python非递归遍历多叉树


    class Queue:
        def __init__(self,max_size):
            self.max_size = int(max_size)
            self.queue = []
    
        def put(self,data):
            if self.max_size > 0:
                if self.full():
                    raise ValueError('Queue is full!')
                else:
                    self._put(data)
    
        def get(self):
            if self._queue_size() > 0:
                result = self._get()
                empty_flag = False
            else:
                result = None
                empty_flag = True
            return result
    
        def empty(self):
            if self._queue_size() == 0:
                return True
            else:
                return False
    
        def full(self):
            if self._queue_size() == self.max_size:
                return True
            else:
                return False
    
        def _put(self,data):
            self.queue.append(data)
    
        def _get(self):
            result = self.queue[0]
            self.queue.pop(0)
            return result
    
        def _queue_size(self):
            return len(self.queue)
    
    class TreeRoot:
        def __init__(self,root_node):
            self.root = root_node
    
        def travel_dbfs(self):#图的深度遍历
            stack_list = []
            visited = []
            stack_list.append(self.root)
            visited.append(self.root)
            while len(stack_list) > 0:
                x = stack_list[-1]
                for w in x.child_list:
                    if not w in visited:
                        print(w.name)
                        visited.append(w)
                        stack_lsit.append(w)
                        break
                if stack_list[-1] == x:
                    stack_list.pop()
    
        def travel_bfs(self):#图的广度遍历
            queue = Queue(100000)
            visited = []
            queue.put(self.root)
            visited.append(self.root)
            while not queue.empty():
                v =queue.get()
                i = 1
                try:
                    w=v.child_list[i]
                except IndexError:
                    w = None
                while w:
                    if not w in visited:
                        print(w.name)
                        visited.append(w)
                        queue.put(w)
                        i = i+1
                        try:
                            w = v.child_list[i]
                        except IndexError:
                            w = None
            return visited
    
        def search(self,keyword):
            visited_list = self.travel_bfs()
            for v in visited_list:
                if v.data == keyword:
                    return v
            return None
    
        def built(self,node):
            parent = node.parent
            v = self.search(parent.data)
            v.child_list.append(node)
    
    
    class TreeNode:
        def __init__(self,name,groupid):
            self.parent = None
            self.child_list = []
            self.name = name
            self.groupid = groupid
  • 相关阅读:
    日积月累--小技巧之四
    深入理解object C中复制对象的用法(二)
    状压dp-poj-1170-Shopping Offers
    linux printk函数学习
    I.MX6 WIFI wireless_tools 移植
    I.MX6 AW-NB177NF WIFI 驱动移植问题
    VS 一些配置设置
    I.MX6 boot from Micro SD
    Android studio 构建太慢
    Android gif 录屏
  • 原文地址:https://www.cnblogs.com/wuxie1989/p/8184919.html
Copyright © 2020-2023  润新知