二叉搜索树 - 存储对父节点的引用

发布于 2025-01-03 13:28:04 字数 994 浏览 0 评论 0原文

我希望有人可以帮助我,我不是编程专业人士,但正在使用Python来学习和实验二叉树。

下面是我的代码,并尝试尝试在其节点中存储对节点父节点的引用,但其父节点的存储不适用于叶节点。在构建树的过程中有没有办法做到这一点?

我还想知道对于给定的节点,它是“左”节点还是“右”节点。我认为由于节点存储在 TreeNode.left 或 TreeNode.right 的实例中,我也许能够在 Python 中获得对此的引用,如 n._name_ 或类似的东西。你能告诉我判断一个节点是左还是右的正确方法吗?

我的最终目标是通过级别顺序遍历来可视化我的树。

class TreeNode:
 left, right, data = None, None, 0

def __init__(self,nodeData, left = None, right = None, parent = None):
 self.nodeData = nodeData
 self.left = left
 self.right = right
 self.parent = self

class Tree:
 def __init__(self):
  self.root = None

 def addNode(self, inputData):
  return TreeNode(inputData)

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

I hope someone can help me, I'm not a programming professional, but am using Python to learn and experiment with binary trees.

Below is the code I have, and have attempted to try and store a reference to a node's parent in it's node, the storing of it's parent node, won't work for leaf nodes though. Is there a way of doing this during the process of building the tree?

I'd also like to know for a given node, whether is's a 'Left or 'Right' node. I thought seeing as the node is stored in an instance of TreeNode.left or TreeNode.right, I might be able to get a reference to this in Python, as in n._name_ or something like that. Could you tell me the correct way to find whether a node is left or right?

My ultimate goal will be to visualise my tree through a level order traversal.

class TreeNode:
 left, right, data = None, None, 0

def __init__(self,nodeData, left = None, right = None, parent = None):
 self.nodeData = nodeData
 self.left = left
 self.right = right
 self.parent = self

class Tree:
 def __init__(self):
  self.root = None

 def addNode(self, inputData):
  return TreeNode(inputData)

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

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

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

发布评论

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

评论(1

澜川若宁 2025-01-10 13:28:04

这有很多很多问题。既然是作业,我就提供一个提示。

def __init__(self,nodeData, left = None, right = None, parent = None):
 self.nodeData = nodeData
 self.left = left
 self.right = right
 self.parent = self

为什么 self.parent 没有设置为 parent

There are many, many things wrong with this. Since it's homework, I'll supply one hint.

def __init__(self,nodeData, left = None, right = None, parent = None):
 self.nodeData = nodeData
 self.left = left
 self.right = right
 self.parent = self

Why isn't self.parent set to parent?

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