如何将斐波那契数列递归插入二叉树中

发布于 2025-01-04 16:40:06 字数 876 浏览 1 评论 0原文

希望有人可以提供帮助,我不是程序员,但一直对探索斐波那契数列及其递归树感兴趣...

我创建了一个二叉树类以及关联的 TreeNode 类,并且想要生成一棵二叉树由以下方式创建的递归调用:

对于给定的 n 值,f(n) = f(n-1) + f(n-2)

我想将其添加为二叉树类的 InsertFibonacci 方法,替换标准 Insert 方法:

def insertNode(self, root, inputData):
    if root == None:
        return self.addNode(inputData)
    else:
        if inputData <= root.nodeData:
            root.left = self.insertNode(root.left, inputData)
        else:
            root.right = self.insertNode(root.right, inputData)
        return root

我会添加Fib 函数的某种装饰器?

# Fib function
def f(n):

    def helper(n):
        left = f(n-1)
        right = f(n-2)
        return left,right

    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        left, right = helper(n)
        return left + right

Hope someone can help, I'm not a programmer, but have been interested in exploring Fibonacci sequence and it's recursive tree...

I've created a Binary Tree class, along with an associated TreeNode class, and want to generate a binary tree of the recursive calls created by:

f(n) = f(n-1) + f(n-2) for a given value of n

I'd want to add it as an InsertFibonacci method of my Binary Tree class, replacing the standard Insert method:

def insertNode(self, root, inputData):
    if root == None:
        return self.addNode(inputData)
    else:
        if inputData <= root.nodeData:
            root.left = self.insertNode(root.left, inputData)
        else:
            root.right = self.insertNode(root.right, inputData)
        return root

Would I add somekind of decorator to the Fib function?

# Fib function
def f(n):

    def helper(n):
        left = f(n-1)
        right = f(n-2)
        return left,right

    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        left, right = helper(n)
        return left + right

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

霞映澄塘 2025-01-11 16:40:06

这是我能想到的最简单的解决方案:

class FibTree(object):
    def __init__(self, n):
        self.n = n
        if n < 2:
            self.value = n
        else:
            self.left = FibTree(n - 1)
            self.right = FibTree(n - 2)
            self.value = self.left.value + self.right.value

Here's the simplest solution I can think of:

class FibTree(object):
    def __init__(self, n):
        self.n = n
        if n < 2:
            self.value = n
        else:
            self.left = FibTree(n - 1)
            self.right = FibTree(n - 2)
            self.value = self.left.value + self.right.value
祁梦 2025-01-11 16:40:06

这是一种方法:

def insertFibonacci(self, n):
    current = self.addNode(n)
    if n > 1:
        current.left = self.insertFibonacci(n-1)
        current.right = self.insertFibonacci(n-2)
        # if you want the fibonacci numbers instead of the calls:
        # current.value = current.left.value + current.right.value
    return current

假设n为正。
应该返回斐波那契调用树的根。

请注意,这不会是完全相同类型的二叉树;它不能满足二叉搜索树所具有的排序不变性。我假设您只是想使用现有的结构以方便起见。

Here's one way:

def insertFibonacci(self, n):
    current = self.addNode(n)
    if n > 1:
        current.left = self.insertFibonacci(n-1)
        current.right = self.insertFibonacci(n-2)
        # if you want the fibonacci numbers instead of the calls:
        # current.value = current.left.value + current.right.value
    return current

Assumes positive n.
Should return the root of the fibonacci call tree.

Note that this won't exactly be the same kind of binary tree; it won't satisfy the ordering invariant that a binary search tree does. I'm assuming you just want to use your existing structure for convenience.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文