从二叉搜索树中检索

时间:2014-11-10 23:04:38

标签: python python-3.x binary-search-tree

我在python中使用二叉搜索树。但是我的检索方法并不像我想的那样工作。它只在我想要检索根时返回正确的值,并且对所有其他节点都不返回。

这是我的节点类的代码:

class Treenode:
    def __init__(self, item=None, left=None, right=None):
        self.item = item
        self.pleft = left
        self.pright = right
        self.parent = None

    def __str__(self):
        return str(self.item)

我的二叉树代码:

from Treenode import *
class Bintree:
    def __init__(self):
        self.root = None
        self.size = 0

    def insert(self, item):
        self.size += 1
        if self.root == None:
            self.root = Treenode(item)
        else:
            self.insertnode(self.root, item)

    def insertnode(self,node,item):
        if node.pleft == None and node.pright == None:
            if item > node.item:
                newnode = Treenode(item)
                node.pright = newnode
                newnode.parent = node
            else:
                newnode = Treenode(item)
                node.pleft = newnode
                newnode.parent = node
        else:
            if item > node.item:
                if node.pright != None:
                    self.insertnode(node.pright, item)
                else:
                    newnode = Treenode(item)
                    node.pright = newnode
                    newnode.parent = node 
            else:
                if node.pleft != None:
                    self.insertnode(node.pleft, item)
                else:
                    newnode = Treenode(item)
                    node.pleft = newnode
                    newnode.parent = node                  

    def print_inorder(self, root):
        if root == None:
            pass
        else:
            self.print_inorder(root.pleft)
            print(root.item)
            self.print_inorder(root.pright)

    def print_postorder(self, root):
        if root == None:
            pass
        else:
            self.print_postorder(root.pleft)
            self.print_postorder(root.pright)
            print(root.item)

    def print_preorder(self, root):
        if root == None:
            pass
        else:
            print(root.item)
            self.print_preorder(root.pleft)
            self.print_preorder(root.pright)

    def retrieve(self, node, item):
        if node == None:
            return 'Empty Tree'
        else:
            if node.item == item:
                return str(node)
            elif node.item > item:
                self.retrieve(node.pleft, item)
            elif node.item < item:
                self.retrieve(node.pright, item)

因此,Bintree中的最后一个方法为除Root之外的所有值返回None,但它应该返回节点的值。

填充树:

import BinTree
t = BinTree.Bintree()
t.insert(10)
t.insert(8)
t.insert(15)
t.insert(2)
t.insert(0)
t.insert(25)
t.insert(1)
t.insert(10)
t.print_preorder(t.root)
print('-----------------------')
t.print_inorder(t.root)
print('-----------------------')
t.print_postorder(t.root)
print('-----------------------')
temp = t.retrieve(t.root, 10)
print(temp)

1 个答案:

答案 0 :(得分:0)

您的问题是您没有在代码的所有路径中返回值,因此您没有看到任何路径。打印没有返回值的内容,打印无。 像这样更改检索方法:

def retrieve(self, node, item):
    if node == None:
        return 'Empty Tree'
    else:
        if node.item == item:
            return str(node)
        elif node.item > item:
            return self.retrieve(node.pleft, item)
        elif node.item < item:
            return self.retrieve(node.pright, item)