码迷,mamicode.com
首页 > 编程语言 > 详细

Python非递归遍历多叉树

时间:2018-01-03 18:37:14      阅读:460      评论:0      收藏:0      [点我收藏+]

标签:append   value   try   keyword   index   tac   uil   stack   init   

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_lsit = []
        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

 

Python非递归遍历多叉树

标签:append   value   try   keyword   index   tac   uil   stack   init   

原文地址:https://www.cnblogs.com/wuxie1989/p/8184919.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!