顺序,预购,后序遍历不起作用

时间:2016-01-03 10:14:23

标签: python tree binary-search-tree

我正在尝试在binary search tree中实施python并尝试在inorderpreorderpostorder中打印树的节点,但不幸的是我的结果不正确。 这是我的代码:

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


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

    def get_root(self):
        return self.root

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

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

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

    def _find(self, val, node):
        if val == node.v:
            return Node
        else:
            if val < node.v and node is not None:
                self._find(val, node.l)
            if val > node.v and node is not None:
                self._find(val, node.r)

    def delete_tree(self):
        self.root = None

    def print_in_order(self): # Left, Node, Right
        if self.root is None:
            return None
        else:
            self._in_order(self.root)

    def _in_order(self, node):
        if node is not None:
            self._in_order(node.l)
            print str(node.v) + ' '
            self._in_order(node.r)

    def print_pre_order(self): # Node, Left, Right
        if self.root is None:
            return None
        else:
            self._pre_order(self.root)

    def _pre_order(self, node):
        if node is not None:
            print str(node.v) + ' '
            self._pre_order(node.l)
            self._pre_order(node.r)

    def print_post_order(self): # Left, Right, Node
        if self.root is None:
            return None
        else:
            self._post_order(self.root)

    def _post_order(self, node):
        if node is not None:
            self._post_order(node.l)
            self._post_order(node.r)
            print str(node.v) + ' '

if __name__ == '__main__':
    t = BinarySearchTree()
    t.insert(20)
    t.insert(10)
    t.insert(30)
    t.insert(5)
    t.insert(15)
    t.insert(25)
    t.insert(35)
    print 'In Order Traversal:    \n', t.print_in_order()
    print '\nPre Order Traversal: \n', t.print_pre_order()
    print '\nPost Order Traversal:\n', t.print_post_order()

有人可以告诉我,我做错了什么吗? 我的输出如下:InorderPreorder返回相同的输出。

In Order Traversal:    
20 
10 
30 
5 
15 
25 
35 
None

Pre Order Traversal: 
20 
10 
30 
5 
15 
25 
35 
None

Post Order Traversal:
35 
25 
15 
5 
30 
10 
20 
None

1 个答案:

答案 0 :(得分:3)

遍历功能没问题。但在_add中,进行了以下比较:

if val < node.l:
    ...

应替换为:

if val < node.v:
    ...

将新值与当前节点值进行比较,而不是导致错误比较结果的左侧节点;导致错误的树结构。