Python中BST的基于指针的实现

时间:2019-04-07 07:57:24

标签: python pointers binary-search-tree

我无法理解我的课程手册提供的在Python中实现BST的代码。我知道有很多简单的方法可以做到这一点,但是考试题是基于下面代码中使用指针的方法进行的。

令我困扰的一件事是,至少在链接列表中,无需使用“以前的节点/当前节点英寸蠕虫技术”进行插入,因此,我对是否需要这样的行表示怀疑self.PreviousNodePointer = ThisNodePointer。我当然可能是错的。我猜想我们在处理有序结构时可能需要这种蠕虫?我对教科书的解决方案不信任,是因为它确实包含许多已确认的错误。

有人可以告诉我在使用相同基本方法的情况下是否可以更简单地进行插入吗?

class TreeNode: # object for each node
    def __init__(self):
        self.Data = ""
        self.LeftPointer = -1
        self.RightPointer = -1

class BinaryTree:
    def __init__(self, n): # initialisation with list length n
        self.null = -1
        self.RootPointer = self.null
        self.FreePointer = 0
        self.Tree = []
        for i in range (0, n):
            self.Tree.append(TreeNode())
            self.Tree[i].LeftPointer = i + 1
        self.Tree[n-1].LeftPointer = self.null

    def display(self): # print a display in order of index positions
        print("{0:^5}|{1:^10}|{2:^10}|{3:^10}|".format("", "Left", "Data", "Right"))
        for i in range (0, len(self.Tree)):
            index = "[" + str(i) + "]"
            print("{0:^5}|{1:^10}|{2:^10}|{3:^10}|".format(index, self.Tree[i].LeftPointer, self.Tree[i].Data, self.Tree[i].RightPointer))
        print("RootPointer:", self.RootPointer)
        print("FreePointer:", self.FreePointer)

    def insert(self, item): # insert item, returns true if inserted and false if set full
        if self.FreePointer == self.null:
            return False # no room in list
        NewNodePointer = self.FreePointer
        # set free pointer to next position in "free list" if that is
        # the right term
        self.FreePointer = self.Tree[self.FreePointer].LeftPointer
        # Add data to node at free node pointer
        self.Tree[NewNodePointer].Data = item
        # Why do we need to set these to null?
        self.Tree[NewNodePointer].LeftPointer = self.null
        self.Tree[NewNodePointer].RightPointer = self.null
        #If this is the first node to be inserted into tree
        if self.RootPointer == self.null:
            self.RootPointer = NewNodePointer
        else:
            ThisNodePointer = self.RootPointer
            while ThisNodePointer != self.null:
                self.PreviousNodePointer = ThisNodePointer
                if self.Tree[ThisNodePointer].Data > item:
                    self.TurnedLeft = True
                    ThisNodePointer = self.Tree[ThisNodePointer].LeftPointer
                else:
                    self.TurnedLeft = False
                    ThisNodePointer = self.Tree[ThisNodePointer].RightPointer
            if self.TurnedLeft:
                self.Tree[self.PreviousNodePointer].LeftPointer = NewNodePointer
            else:
                self.Tree[self.PreviousNodePointer].RightPointer = NewNodePointer
        return True

    def find(self, item): # returns position of an item, -1 if not found
        ThisNodePointer = self.RootPointer
        while ThisNodePointer != self.null and self.Tree[ThisNodePointer].Data != item:
            if self.Tree[ThisNodePointer].Data > item:
                ThisNodePointer = self.Tree[ThisNodePointer].LeftPointer
            else:
                ThisNodePointer = self.Tree[ThisNodePointer].RightPointer
        return ThisNodePointer

#######################
##  Example commands ##
#######################

MyTree = BinaryTree(6) # Create a tree of length 6
MyTree.display() # display tree in a table
MyTree.insert(10) # insert number 10
MyTree.insert(40)
MyTree.insert(30)
MyTree.insert(50)
MyTree.insert(60)
MyTree.display()
print("Item found at position", MyTree.find(30)) # print the position of number 30

0 个答案:

没有答案