简体   繁体   中英

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.

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.

First, you define a print_tree method:

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:

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. 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. Why not say I want to print all the numbers above 41 instead ? 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). Let me write you pseudo code for this (suppose you're already on your root node which has a value of 65 :

    • I want to write all the numbers in ascending orders...
    • My root is 65. My left is 50, my right is 91.
    • Which is the lowest ? 50. Does it have another branches ? No. Print it !
    • My root is still 65, my right is 91. My root is lower than my branch ? Print it ! And go to the right branch.
    • My main is now 91, my left is 72, my right 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 !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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