空二进制树输出中的Python打印特定字符

发布于 2025-01-24 04:47:22 字数 1794 浏览 3 评论 0原文

我现在有一个二进制树,如下所示,

class Node:
    def __init__(self,data):
        # Left Child
        self.left = None
        # Right Child
        self.right = None
        # Node Value
        self.data = data

    def insert(self, data):
    # Compare the new value with the parent node
        if self.data:
            # Less than or equal goes to the Left
            if data <= self.data:
                if self.left is None:
                    self.left = Node(data)
                else:
                    self.left.insert(data)
            # Greater than goes to the right
            elif data > self.data:
                if self.right is None:
                    self.right = Node(data)
                else:
                    self.right.insert(data)
        else:
            self.data = data

假设我们填充了以下二进制树,

Numbers = [4, 7, 9, 1 , 3]

for index in range(len(Numbers)):
    if index == 0:
        root = Node(Numbers[index])
    else:
         root.insert(Numbers[index])

该树生成以下二进制树:

      4
     / \
   1     7
  / \    / \
 .   3  .   9

我已经写了以下二进制树来打印二进制树:

def PrintTree(self, root):
    thislevel = [root]

    while thislevel:
        nextlevel = list()
        Curr_Level = ""
        for n in thislevel:
            Curr_Level += " " + str(n.data)

            if n.left: 
                nextlevel.append(n.left)
            if n.right: 
                nextlevel.append(n.right)

        print(Curr_Level)
        thislevel = nextlevel

root.PrintTree(root=root)

它生成以下输出,

4
1 7
3 9

但是,我希望代码打印代码带有“@”的空条目,即我希望我的代码输出以下内容:

4
1 7
@ 3 @ 9

如何调整我的printtree函数以实现这一目标?

I have a binary tree generated as follows

class Node:
    def __init__(self,data):
        # Left Child
        self.left = None
        # Right Child
        self.right = None
        # Node Value
        self.data = data

    def insert(self, data):
    # Compare the new value with the parent node
        if self.data:
            # Less than or equal goes to the Left
            if data <= self.data:
                if self.left is None:
                    self.left = Node(data)
                else:
                    self.left.insert(data)
            # Greater than goes to the right
            elif data > self.data:
                if self.right is None:
                    self.right = Node(data)
                else:
                    self.right.insert(data)
        else:
            self.data = data

Now suppose we populate the binary tree with the following

Numbers = [4, 7, 9, 1 , 3]

for index in range(len(Numbers)):
    if index == 0:
        root = Node(Numbers[index])
    else:
         root.insert(Numbers[index])

Which generates the following binary tree:

      4
     / \
   1     7
  / \    / \
 .   3  .   9

I have written the following to print the binary tree:

def PrintTree(self, root):
    thislevel = [root]

    while thislevel:
        nextlevel = list()
        Curr_Level = ""
        for n in thislevel:
            Curr_Level += " " + str(n.data)

            if n.left: 
                nextlevel.append(n.left)
            if n.right: 
                nextlevel.append(n.right)

        print(Curr_Level)
        thislevel = nextlevel

root.PrintTree(root=root)

which generates the following output

4
1 7
3 9

However, I would like the code print the empty entries with an "@", i.e. I want my code to output the following:

4
1 7
@ 3 @ 9

How can I go about adjusting my PrintTree function to achieve this?

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

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

发布评论

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

评论(1

归途 2025-01-31 04:47:22

运行BFS的修改版本。更具体地说,从根节点开始运行广度第一次搜索(BFS)。这将为每个节点分配一个级别。在BFS期间,当当前节点有一个失踪的孩子时,将其位置添加到FIFO队列。每次将其从队列中删除时打印一个节点。每次从队列中删除具有新级别的节点时,都会以新的线路打印。

Run a modified version of BFS. More specifically, run breadth first search (BFS) starting from the root node. This will assign a level to each node. During BFS, when a current node has a missing child, add an @ node in its place to the FIFO queue. Print a node each time it is removed from the queue. Print in a new line each time a node with a new level is removed from the queue.

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