【问题标题】:How to include a parent pointer with a insert function in Binary Search Tree如何在二叉搜索树中包含带有插入函数的父指针
【发布时间】:2020-11-12 08:45:10
【问题描述】:

我有一个 find_successor 函数,我想调用 current.parent 但我意识到它是 None 类型,因为在我的 insert() 中我从未将 parent 作为指针包含在内。我想知道如何在我的 insert() 中实现一个父指针,而不必过多地修改代码。

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

class Tree(object):
 PREORDER = 1
 INORDER = 2
 POSTORDER = 3

  def __init__(self):
    # Do not create any other private variables.
    # You may create more helper methods as needed.
    self.root = None

  def print(self):
   # Print the data of all nodes in order
   self.__print(self.root)

  def __print(self, curr_node):
    # Recursively print a subtree (in order), rooted at curr_node
    if curr_node is not None:
        self.__print(curr_node.left)
        print(str(curr_node.data), end=' ')  # save space
        self.__print(curr_node.right)

  def insert(self, data):
    # Find the right spot in the tree for the new node
    # Make sure to check if anything is in the tree
    # Hint: if a node n is null, calling n.getData() will cause an error
    if self.root is None:
        self.root = Node(data)
    else:
        current = self.root
        while True:
            if current.data > data:
                if current.left == None:
                    current.left = Node(data)
                else:
                    current = current.left
            elif current.data < data:
                if current.right == None:
                    current.right = Node(data)
                    break
                else:
                    current = current.right
            else:
                break

这是我正在处理的 find_successor 函数,我可以在其中使用辅助方法来查找节点,但它没有父节点。

    def find_successor(self, data):
# helper method to implement the delete method but may be called on its own
# if the right subtree of the node is nonempty,then the successor is just
# the leftmost node in the right subtree.
# If the right subtree of the node is empty,then go up the tree until a node that is
# the left child of its parent is encountered. The parent of the found node will be the
# successor to the initial node.

# Return object of successor if found else return None
    current = self.__find_node(data)
    if current.right:
       while (current.right):
          current = current.right
          if current.left:
              current = current.left
          else:
              return current
       return current
   else:
      print(current.parent.data)


  def __find_node(self, data):
# returns the node with that particular data value else returns None
   current = self.root
   while current is not None:
       if (data > current.data):

         current = current.right
       elif (data < current.data):
          current = current.left
       elif (data == current.data):
          return current
   return None

这是我的测试用例

class T3_successor(unittest.TestCase)`:

   def test_successor(self):
     print("\n")
     print("successor function")
     tree_success = lab3.Tree()
     tree_success.insert(8)
     tree_success.insert(3)
     tree_success.insert(10)
     tree_success.insert(1)
     tree_success.insert(6)
     tree_success.insert(4)
     tree_success.insert(7)
     tree_success.insert(14)
     tree_success.insert(13) 

     easy_success =tree_success.find_successor(8).data
     medium_success = tree_success.find_successor(10).data
     tough_success = tree_success.find_successor(7).data

     self.assertEqual(easy_success, 10)
     self.assertEqual(medium_success, 13)
     self.assertEqual(tough_success, 8)

print("\n")

【问题讨论】:

    标签: python binary-search-tree parent-child parent


    【解决方案1】:

    不应该很复杂:在您的 insert() 方法中,当您将节点设置为新节点时,还将它们的父节点(您现在不这样做)设置为您来自的最后一个节点。 当你这样做时,除了根节点之外的每个节点都会有一个指向它们父节点的指针。

    【讨论】:

    • 您介意添加一些代码来详细说明吗?我尝试了类似的东西,但每次我打电话给父母时,它要么只是指向当前数据点,要么指向一个非类型......
    • 是的,它应该看起来像这样(由于字符限制仅显示一侧)` def insert(self, data): if self.root is None: self.root = Node(data) else: current = self.root while True: if current.data > data: if current.left == None: current.left = Node(data) current.left.parent = current`
    • 谢谢!我意识到我在 find_successor 上有一个错误,因为我得到的第一个测试用例是 8,当然 8 没有父级,所以我无法打印它的父级!如果没有您的意见,就不会发现这一点,谢谢:))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-10
    • 1970-01-01
    • 2014-09-19
    • 2015-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多