I am trying to check if a node has a sibling using a recursive method, but it seems that my method doesn't go into the tree besides the first two children. So in this example, if I want to check if 6 has a sibling, my recursive call doesn't reach it.

def sibling(T,k):
x = T.find(k)
if x == None:
return None
if x == T.right:
if not T.left.is_empty:
return T.left.key
else:
return None
elif x == T.left:
if not T.right.is_empty:
return T.right.key
else:
return None
elif k > T.key:
sibling(T.right, k)
elif k < T.key:
sibling(T.left, k)
pass
Here is the expected output:
T = bst_list[3]
sibling(T,0): 3
sibling(T,1): None
sibling(T,2): None
sibling(T,3): 0
sibling(T,4): None
sibling(T,5): None
sibling(T,6): 8
sibling(T,7): 10
sibling(T,8): 6
sibling(T,9): 13
Here is my output:
T = bst_list[3]
sibling(T,0): 3
sibling(T,1): None
sibling(T,2): None
sibling(T,3): 0
sibling(T,4): None
sibling(T,5): None
sibling(T,6): None
sibling(T,7): None
sibling(T,8): None
sibling(T,9): None
Also this is my BST class which I cannot edit:
def __init__(self, k=None):
if (k is None): # if k is None, it will create an empty tree
self.is_empty = True
else: # otherwise it will create a tree with a single node, containing k
self.is_empty = False
self.key=k
self.left=BST() # left child will be an empty tree
self.right=BST() # right child will be an empty tree
def find(self,key,parent=None,return_parent=False):
# Returns address of node where key is.
# If return_parent is True, it also returns the parent of the node where key is
# Returning the parent makes deletions simpler
if self.is_empty:
if return_parent:
return None, None
return None
if self.key == key:
if return_parent:
return parent, self
return self
if self.key>key:
return self.left.find(key,self,return_parent)
return self.right.find(key,self,return_parent)
Thank you! I just had to add return to the recursive calls >.< !!!!