简体   繁体   English

打印二叉搜索树右子树中的所有节点

[英]Printing all the nodes in the right subtree of a Binary Search Tree

I am fairly new to data structures, and recursion.我对数据结构和递归相当陌生。 So, I decided to try and implement a method that would print all the values of all the nodes of the right subtree (in ascending order, ie: 50, 65, 72, 91, 99) in this given tree just as a learning experience.因此,我决定尝试实现一种方法,该方法将在此给定树中打印右子树的所有节点的所有值(按升序,即:50、65、72、91、99)作为学习经验.

Here is the tree that I am working with, visually.这是我正在使用的树,视觉上。 树本身 And I have quite a few problems understanding how to recurse through the right subtree.而且我在理解如何通过正确的子树进行递归时遇到了很多问题。

That's what I have tried doing so far (the actual method is at the bottom):这就是我到目前为止所做的尝试(实际方法在底部):

class BinarySearchTree:
    _root: Optional[Any]
    # The left subtree, or None if the tree is empty.
    _left: Optional[BinarySearchTree]
    # The right subtree, or None if the tree is empty.
    _right: Optional[BinarySearchTree]

def __init__(self, root: Optional[Any]) -> None:
    """Initialize a new BST containing only the given root value.
    """
    if root is None:
        self._root = None
        self._left = None
        self._right = None
    else:
        self._root = root
        self._left = BinarySearchTree(None)
        self._right = BinarySearchTree(None)

def is_empty(self) -> bool:
    """Return True if this BST is empty.

    >>> bst = BinarySearchTree(None)
    >>> bst.is_empty()
    True
    """
    return self._root is None

# That is what I have tried doing so far.
def print_right_subtree(self) -> None:
    """Print the right subtree in order

    >>> bst = BinarySearchTree(41)
    >>> left = BinarySearchTree(20)
    >>> left._left = BinarySearchTree(11)
    >>> left._right = BinarySearchTree(29)
    >>> left._right._right = BinarySearchTree(32)
    >>> right = BinarySearchTree(65)
    >>> right._left = BinarySearchTree(50)
    >>> right._right = BinarySearchTree(91)
    >>> right._right._left = BinarySearchTree(72)
    >>> right._right._right = BinarySearchTree(99)
    >>> bst._left = left
    >>> bst._right = right
    >>> bst.print_right_subtree()
    50
    65
    72
    91
    99
    """
    if self.is_empty():
        pass
    else:
        # I am not really sure what to do here... 
        # I have tried setting self._left = None, but that just made things even more complicated!
        print(self._root)
        self._right.print_right_subtree()

Any help would be extremely appreciated!任何帮助将不胜感激! Also, if any of you guys have a tutorial that I can follow, that would really great for a newbie like myself :).另外,如果你们中有人有我可以遵循的教程,那对于像我这样的新手来说真的很棒:)。

If you want to print the nodes in the right subtree, you just have to call your print_tree on the attribute of your tree corresponding to his right node.如果你想打印右子树中的节点,你只需要在你的树的属性上调用你的print_tree对应于他的右节点。

First, you define a print_tree method:首先,定义一个print_tree方法:

def print_tree(self) -> None:
    if self.is_empty():
        pass
    else:
        # you are free to do additional things here such as print node value or etc..
        self._left.print_tree()
        self._right.print_tree()

And then the print_right_subtree method:然后是print_right_subtree方法:

def print_right_subtree(self) -> None:
    self._right.print_tree() # which correspond to the print_tree of the _right attribute

Since you're not asking for code itself, and rather for help to write your own code...由于您不是要求代码本身,而是要求帮助您编写自己的代码......

  1. There are a million ways to do it.有一百万种方法可以做到。 Some are more optimized.有些更优化。 Some are faster to write.有些写得更快。 It all depends on what you need.这一切都取决于你需要什么。

  2. Here I think you need to understand what a tree is.在这里我想你需要了解一棵树是什么。 Any of your subtree, is a tree by itself.您的任何子树本身都是一棵树。 So, you've got to understand that print the right tree does mean anything and everything.所以,你必须明白print the right tree确实意味着任何事情。 Only the right branch of each tree for instance ?例如,只有每棵树的右分支? Or all the tree of the first right branch ?还是第一个右分支的所有树? Maybe the second branch ?也许是第二个分支?

  3. If I got it right (Da bum tss !), you want to print the right branch of the tree that's called root on your schema.如果我做对了(Da bum tss !),您希望在您的架构上打印名为 root 的树的正确分支。 Why not say I want to print all the numbers above 41 instead ?为什么不说I want to print all the numbers above 41呢? That way, even if you want to print your tree beginning from a sub-tree, it would be really easy to do so !这样,即使您想从子树开始打印树,也很容易!

  4. You need to visualize what your algorithm would do.你需要想象你的算法会做什么。 Here, you want to print all the numbers above 41 (the right branch of the main tree).在这里,您要打印 41(主树的右分支)以上的所有数字。 Let me write you pseudo code for this (suppose you're already on your root node which has a value of 65 :让我为此编写伪代码(假设您已经在根节点上,其值为 65 :

    • I want to write all the numbers in ascending orders...我想按升序写所有数字...
    • My root is 65. My left is 50, my right is 91.我的根是 65。我左边是 50,我右边是 91。
    • Which is the lowest ?哪个最低? 50. Does it have another branches ? 50. 它还有别的分店吗? No. Print it !不。打印它!
    • My root is still 65, my right is 91. My root is lower than my branch ?我的根还是65,我的右边是91。我的根比我的树枝低? Print it !打印出来! And go to the right branch.然后去正确的分支。
    • My main is now 91, my left is 72, my right 99.我的主要现在是 91,我的左边是 72,我的右边是 99。
    • Which is the lowest ?哪个最低? You got your recursion.你得到了你的递归。

And even after all this, you could still choose to make another faster - to write, not to compute !即使在这一切之后,您仍然可以选择让另一个更快 - 编写,而不是计算! - solution. - 解决方案。 Gather all the values from the branch you need, and print the sorted values !从您需要的分支中收集所有值,并打印排序后的值!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM