Reputation: 125
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, i.e: 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 :).
Upvotes: 0
Views: 929
Reputation: 86
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
Upvotes: 2
Reputation: 3306
Since you're not asking for code itself, and rather for help to write your own code...
There are a million ways to do it. Some are more optimized. Some are faster to write. It all depends on what you need.
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 ?
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 !
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 :
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 !
Upvotes: 1