Python 在BST中查找比给定值小的值的数目

Python 在BST中查找比给定值小的值的数目,python,count,binary-search-tree,Python,Count,Binary Search Tree,我试图在二元搜索树(有n个元素)中找到一些比给定值x小的值。我已经写了一个递归函数。但是,当我运行它时,它返回Nonetype。例如,BST中包含5、1、2、7、6、8。我试图得到小于6的值,但输出为None。我的代码哪里有问题 这是我的代码: class Node: def __init__(self, x): self.data = x self.left = None self.right = None def addToN

我试图在二元搜索树(有n个元素)中找到一些比给定值x小的值。我已经写了一个递归函数。但是,当我运行它时,它返回Nonetype。例如,BST中包含5、1、2、7、6、8。我试图得到小于6的值,但输出为None。我的代码哪里有问题

这是我的代码:

class Node:
    def __init__(self, x):
        self.data = x
        self.left = None
        self.right = None

    def addToNode(self, x):
        # root

        # add to left
        if (x < self.data):
            # empty -> 1
            if (self.left == None):
                p = Node(x)
                self.left = p
            else:
                # recursion
                self.left.addToNode(x)

        # add to right
        else:
            if (self.right == None):
                p = Node(x)
                self.right = p
            else:
                # recursion
                self.right.addToNode(x)

    def Nodesearchlower(self, x):
        if self.left and self.right:
            if (x > self.data):
                return 1 + self.right.Nodesearchlower(x) + self.left.Nodesearchlower(x)
            else:
                return 0 + self.left.Nodesearchlower(x)
        elif self.left:
            if (x > self.data):
                return 1 + self.left.Nodesearchlower(x)
            else:
                return 0 + self.left.Nodesearchlower(x)
        elif self.right:
            if (x > self.data):
                return 1 + self.right.Nodesearchlower(x)
            else:
                return 0 + self.right.Nodesearchlower(x)
        else:
            if (x > self.data):
                return 1
            else:
                return 0


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

    def addToBST(self, x):
        # empty -> 1
        if (self.root == None):
            p = Node(x)
            self.root = p
        # 1 => 2, 2 => 3, ...
        else:
            self.root.addToNode(x)

    def BSTsearchlower(self, x):
        self.root.Nodesearchlower(x)


# main
n, x = map(int, input().split())
a = list(map(int, input().split()))
bst = BST()
for i in range(len(a)):
    bst.addToBST(a[i])
ans = bst.BSTsearchlower(x)
print(ans)
类节点:
定义初始化(self,x):
self.data=x
self.left=无
self.right=无
def addToNode(自身,x):
#根
#添加到左边
如果(x<自身数据):
#空->1
如果(self.left==无):
p=节点(x)
self.left=p
其他:
#递归
self.left.addToNode(x)
#向右加
其他:
如果(self.right==无):
p=节点(x)
self.right=p
其他:
#递归
self.right.addToNode(x)
def Nodesearchlower(自身,x):
如果self.left和self.right:
如果(x>self.data):
返回1+self.right.Nodesearchlower(x)+self.left.Nodesearchlower(x)
其他:
返回0+self.left.Nodesearchlower(x)
elif self.left:
如果(x>self.data):
返回1+self.left.Nodesearchlower(x)
其他:
返回0+self.left.Nodesearchlower(x)
elif self.right:
如果(x>self.data):
返回1+self.right.Nodesearchlower(x)
其他:
返回0+self.right.Nodesearchlower(x)
其他:
如果(x>self.data):
返回1
其他:
返回0
BST级:
定义初始化(自):
self.root=None
def ADDTOST(自我,x):
#空->1
如果(self.root==无):
p=节点(x)
self.root=p
# 1 => 2, 2 => 3, ...
其他:
self.root.addToNode(x)
def BSTREACHLOWER(自身,x):
self.root.Nodesearchlower(x)
#主要
n、 x=map(int,input().split())
a=列表(映射(int,input().split())
bst=bst()
对于范围内的i(len(a)):
bst.addToBST(a[i])
ans=bst.bst搜索下限(x)
打印(ans)

我已经发现了我的代码的问题:我在类BST中的函数BSTsearchlower(self,x)中遗漏了“return”。

BSTsearchlower不会从节点搜索lower返回值