簡體   English   中英

打印二叉搜索樹右子樹中的所有節點

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

我對數據結構和遞歸相當陌生。 因此,我決定嘗試實現一種方法,該方法將在此給定樹中打印右子樹的所有節點的所有值(按升序,即:50、65、72、91、99)作為學習經驗.

這是我正在使用的樹,視覺上。 樹本身 而且我在理解如何通過正確的子樹進行遞歸時遇到了很多問題。

這就是我到目前為止所做的嘗試(實際方法在底部):

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()

任何幫助將不勝感激! 另外,如果你們中有人有我可以遵循的教程,那對於像我這樣的新手來說真的很棒:)。

如果你想打印右子樹中的節點,你只需要在你的樹的屬性上調用你的print_tree對應於他的右節點。

首先,定義一個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()

然后是print_right_subtree方法:

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

由於您不是要求代碼本身,而是要求幫助您編寫自己的代碼......

  1. 有一百萬種方法可以做到。 有些更優化。 有些寫得更快。 這一切都取決於你需要什么。

  2. 在這里我想你需要了解一棵樹是什么。 您的任何子樹本身都是一棵樹。 所以,你必須明白print the right tree確實意味着任何事情。 例如,只有每棵樹的右分支? 還是第一個右分支的所有樹? 也許是第二個分支?

  3. 如果我做對了(Da bum tss !),您希望在您的架構上打印名為 root 的樹的正確分支。 為什么不說I want to print all the numbers above 41呢? 這樣,即使您想從子樹開始打印樹,也很容易!

  4. 你需要想象你的算法會做什么。 在這里,您要打印 41(主樹的右分支)以上的所有數字。 讓我為此編寫偽代碼(假設您已經在根節點上,其值為 65 :

    • 我想按升序寫所有數字...
    • 我的根是 65。我左邊是 50,我右邊是 91。
    • 哪個最低? 50. 它還有別的分店嗎? 不。打印它!
    • 我的根還是65,我的右邊是91。我的根比我的樹枝低? 打印出來! 然后去正確的分支。
    • 我的主要現在是 91,我的左邊是 72,我的右邊是 99。
    • 哪個最低? 你得到了你的遞歸。

即使在這一切之后,您仍然可以選擇讓另一個更快 - 編寫,而不是計算! - 解決方案。 從您需要的分支中收集所有值,並打印排序后的值!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM