为二叉树实现DFS和BFS

时间:2016-04-24 18:45:07

标签: python tree depth-first-search breadth-first-search

我正在尝试使用深度优先遍历和广度优先遍历来遍历二叉树,但我遇到了麻烦。我的节点和树实现似乎没问题,我只是不确定如何在深度和广度上正确地遍历树。

class Node:
    def __init__(self, val):
        self.l = None
        self.r = None
        self.v = val

class Tree:
    def __init__(self):
        self.root = None

    def getRoot(self):
        return self.root

    def add(self, val):
        if(self.root == None):
            self.root = Node(val)
        else:
            self._add(val, self.root)

    def _add(self, val, node):
        if(val < node.v):
            if(node.l != None):
                self._add(val, node.l)
            else:
                node.l = Node(val)
        else:
            if(node.r != None):
                self._add(val, node.r)
            else:
                node.r = Node(val)

    def find(self, val):
        if(self.root != None):
            return self._find(val, self.root)
        else:
            return None

    def _find(self, val, node):
        if(val == node.v):
            return node
        elif(val < node.v and node.l != None):
            self._find(val, node.l)
        elif(val > node.v and node.r != None):
            self._find(val, node.r)

    def printTree(self):
        if(self.root != None):
            self._printTree(self.root)

    def _printTree(self, node):
        if(node != None):
            self._printTree(node.l)
            print(str(node.v) + ' ')
            self._printTree(node.r)

    # This doesn't work - graph is not subscriptable
    def dfs(self, graph, start):
        visited, stack = set(), [start]
        while stack:
            vertex = stack.pop()
            if vertex not in visited:
                visited.add(vertex)
                stack.extend(graph[vertex] - visited)
        return visited

     # Haven't tried BFS.  Would use a queue, but unsure of the details.

2 个答案:

答案 0 :(得分:6)

如果它是一棵树,visited可以是一个列表,因为树是非圆形的,所以不需要检查你之前是否访问了一个节点,更重要的是,你想要维持它的顺序你的遍历。

def dfs(self, tree):
    if tree.root is None:
        return []
    visited, stack = [], [tree.root]
    while stack:
        node = stack.pop()
        visited.append(node)
        stack.extend(filter(None, [node.r, node.l]))  
        # append right first, so left will be popped first
    return visited

答案 1 :(得分:4)

您的DFS实施略有不正确。如上所述,您实际上模仿了队列,而不是堆栈。

您当前的代码实际上对于广度优先搜索效果相当不错。它强制节点的兄弟节点在其子节点之前进行评估:

def bfs(self, graph, start):
    visited, queue = set(), [start]
    while stack:
        vertex = queue.pop()
        if vertex not in visited:
            visited.add(vertex)
            # new nodes are added to end of queue
            queue.extend(graph[vertex] - visited)
    return visited

DFS的逻辑要求堆栈的行为如下:当新节点到来时,您需要将其添加到列表的 left ,而不是 right 。这样,在节点的兄弟节点之前强制遍历节点的后代

def dfs(self, graph, start):
    visited, stack = set(), [start]
    while queue:
        vertex = stack.pop()
        if vertex not in visited:
            visited.add(vertex)
            # new nodes are added to the start of stack
            stack = graph[vertex] - visited + stack 
    return visited

其他问题

除此之外,您面临的具体问题是您尚未指定graph 的内容。

如果graph是不支持查找的对象,那么您可以在类定义中使用__getitem__()方法实现

通常,人们满足于使用字典来实现这一点。像{Node: [<list of node's children], ... }这样的东西应该足够了。