python

时间:2017-03-14 15:22:44

标签: python binary-search-tree

我编写了一个名为Node的类,其中包含一些用于创建二叉搜索树的函数。除了应该计算BST高度的函数height()之外,所有函数都能正常工作。考虑到我没有平衡树,它的回报率与我预期的相比非常小。我期待的数字接近于N,其中N是我在树中输入的数字量。这是代码:

from __future__ import print_function
import random

class Node(object):

    def __init__(self, data):

        self.left = None
        self.right = None
        self.data = data

    def insert(self, data):

        if self.data:
            if data < self.data:
                if self.left is None:
                    self.left = Node(data)
                else:
                    self.left.insert(data)
            elif data > self.data:
                if self.right is None:
                    self.right = Node(data)
                else:
                    self.right.insert(data)
        else:
            self.data = data

    def lookup(self, data, parent=None):

        if data < self.data:
            if self.left is None:
                return None, None
            return self.left.lookup(data, self)
        elif data > self.data:
            if self.right is None:
               return None, None
            return self.right.lookup(data, self)
        else:
            return self, parent

    def delete(self, data):

        node, parent = self.lookup(data)
        if node is not None:
            children_count = node.children_count()
            if children_count == 0:

                if parent:
                    if parent.left is node:
                        parent.left = None
                    else:
                        parent.right = None
                else:
                    self.data = None
            elif children_count == 1:

                if node.left:
                    n = node.left
                else:
                    n = node.right
                if parent:
                    if parent.left is node:
                        parent.left = n
                    else:
                        parent.right = n
                else:
                    self.left = n.left
                    self.right = n.right
                    self.data = n.data
            else:

                parent = node
                successor = node.right
                while successor.left:
                     parent = successor
                     successor = successor.left

                node.data = successor.data

                if parent.left == successor:
                    parent.left = successor.right
                else:
                    parent.right = successor.right

    def compare_trees(self, node):

        if node is None:
            return False
        if self.data != node.data:
            return False
        res = True
        if self.left is None:
            if node.left:
                return False
        else:
            res = self.left.compare_trees(node.left)
        if res is False:
            return False
        if self.right is None:
            if node.right:
                return False
        else:
            res = self.right.compare_trees(node.right)
        return res

    def print_tree(self):

        if self.left:
            self.left.print_tree()
        print(self.data, end=" ")
        if self.right:
            self.right.print_tree()

    def height(self, root):
        if root is None:
            return 0
        else:
            return max(self.height(root.left), self.height(root.right)) + 1



random.seed(3)



bst = Node(random.randint(1,1000)) 
for i in range(1,80000,1):
    bst.insert(random.randint(1,1000))

print(bst.height(bst))

2 个答案:

答案 0 :(得分:6)

你的答案很低,因为你总是只插入从1到1000的数字,所以现有的数字总是保持不变,你认为你插入的是1,80000个数字,但实际上是因为从1到1随机生成相同的数字1000你实际上只插入1000个值,从1到1000最多。

错误代码

bst = Node(random.randint(1,1000)) 
for i in range(1,80000,1):
    bst.insert(random.randint(1,1000))

print(bst.height(bst))

<强>修改

bst = Node(random.randint(1,80000)) 
for i in range(1,80000,1):
    bst.insert(random.randint(1,80000))

print(bst.height(bst))

您的代码工作正常,您可以执行下面的代码并使用下面的图片进行检查 enter image description here

bst = Node(7)
list1 = [3,11,1,5,9,13,4,6,8,12,14,8.5]
for i in list1:
    bst.insert(i)
print(bst.height(bst))
bst.print_tree()

<强>输出继电器

5
1 3 4 5 6 7 8 8.5 9 11 12 13 14

答案 1 :(得分:0)

您应该声明为排序数组以获取二叉搜索树的最大高度。 但这可能不适用于1000或10,000的较大数字。它适用于500个元素,因为你的插入递归可能会超过python中的最大递归深度

UPTO 500

bst = Node(0)
list1 = list(range(1,500,1))
for i in list1:
    bst.insert(i)
print(bst.height(bst))

<强>输出

499

1000个元素

bst = Node(0)
list1 = list(range(1,500,1))
for i in list1:
    bst.insert(i)
print(bst.height(bst))

<强>输出

self.right.insert(data)
self.right = Node(data)

RecursionError: maximum recursion depth exceeded