首页 > 解决方案 > NameError:名称“高度”未定义

问题描述

我对python没有经验。

我正在做一个大学活动,我写了一门课来找到二叉树的高度。

但是当我要递归调用该函数时,我会收到以下消息:

NameError                                 Traceback (most recent call last)
<ipython-input-5-109fbd93416e> in <module>
      7 raiz.insert(3)
      8 
----> 9 heigth(raiz)

NameError: name 'heigth' is not defined 

功能是:

def heigth(self, n:"Node")-> int:
        if n:
            return 1
        else:
            left = heigth(n.left)
            right = heigth(n.right)
            if(left < right):
                return right + 1
            else:
                return left + 1

所有代码:

from typing import List

class Node:
    def __init__(self, key, left:"Node"=None, right:"Node"=None):
        self.key = key
        self.left = left
        self.right = right

    def print_tree(self):
        """
        Prints the tree from the current node
        """
        if self.left:
            self.left.print_tree()
        print(self.key, end=" ")
        if self.right:
            self.right.print_tree()


    def insert(self, key) -> bool:
        """
        Insert a node in the tree that has the key "key"
        """
        if key < self.key:
            if self.left:
                return self.left.insert(key)
            else:
                self.left = Node(key)
                return True
        elif key > self.key:
            if self.right:
                return self.right.insert(key)
            else:
                self.right = Node(key)
                return True
        else:
            return False


    def search(self, key) -> bool:
        """
        Returns true if the key exists in the tree
        """
        if key < self.key:
            if self.left:
                return self.left.search(key)
        elif key > self.key:
            if self.right:
                return self.right.search(key)
        else:
            return True
        return False


    def to_sorted_array(self, arr_result:List =None) -> List:
        """
        Returns a vector of the ordered keys.
        arr_result: Parameter with the items already added.
        """
        if(arr_result == None):
            arr_result = []

        if self.left:
            self.left.to_sorted_array(arr_result)

        arr_result.append(self.key)

        if self.right:
            self.right.to_sorted_array(arr_result)
        return arr_result

    def max_depth(self,current_max_depth:int=0) -> int:
        """
        calculates the greatest distance between the root node and the leaf
        current_max_depth: Value representing the longest distance so far
                           when calling for the first time, there is no need to use it
        """
        current_max_depth = current_max_depth +1
        val_left,val_right = current_max_depth,current_max_depth

        if self.left:
            val_left = self.left.max_depth(current_max_depth)
        if self.right:
            val_right = self.right.max_depth(current_max_depth)

        if(val_left>val_right):
            return val_left
        else:
            return val_right

    def position_node(self, key, current_position:int=1) -> int:
        """
            Returns the position of the desired node in the tree
            current_position: represents the position of the tree at that moment
                           when calling for the first time, there is no need to use it
        """
        if key < self.key:
            if self.left:
                return self.left.position_node(key, current_position*2)
        elif key > self.key:
            if self.right:
                return self.right.position_node(key, current_position*2+1)
        else:
            return current_position

    def heigth(self, n:"Node")-> int:
        if n:
            return 1
        else:
            left = heigth(n.left)
            right = heigth(n.right)
            if(left < right):
                return right + 1
            else:
                return left + 1

标签: pythonbinary-tree

解决方案


您正在使用类的方法,但是当您将其称为 aheigth时,它不是在对象的类中查找方法,而是在找不到它的模块级别上查找该方法,因此会出现错误。

尝试用 替换heigth调用self.heigth,以便调用类的方法。


推荐阅读