def MakePB(A, L, R):
   return [A, L, R]

def Akar(PB):
   return PB[0]

def Left(PB):
   return PB[1]

def Right(PB):
   return PB[2]

def isTreeEmpty(PB):
   return PB == []

def isDaun(PB):
   return isTreeEmpty(Left(PB)) and isTreeEmpty(Right(PB))
 
def isExistRight(PB):
   return PB[2] != [] and PB[2] != None

def isExistLeft(PB):
   return PB[1] != [] and PB[1] != None

def BSTFind(PB, F, S):
   if isTreeEmpty(PB):
      return PB
   else:
      if not F(PB):
         return Akar(PB)
      else:
         return BSTFind(S(PB), F, S)
      
def SumTree(PB): # Fungsi Antara, tdk perlu lambda (semoga)
   if isTreeEmpty(PB):
      return 0
   elif isDaun(PB):
      return Akar(PB)
   else:
      return SumTree(Left(PB)) + Akar(PB) + SumTree(Right(PB))
   
def isRightSubLeftPos(PB, F):
   print(SumTree(Right(PB)))
   print(SumTree(Left(PB)))
   print(SumTree(Right(PB)) - SumTree(Left(PB)))
   return F(SumTree(Right(PB)) - SumTree(Left(PB)))

GivenATree = MakePB(10, MakePB(5, MakePB(3, [], []), MakePB(7, [], [])), MakePB(17, MakePB(13, [], []), MakePB(21, [], [])))
# Visualisasi BST GAT:
#         /10\
#    /5\        /17\
#  3   7      13   20

print(BSTFind(
   Right(GivenATree), # PohonBiner kita
   lambda x: isExistLeft(x), 
      # Fungsi yang akan dioperasikan (isExistLeft mengecek apakah terdapat bilangan yang lebih kecil dari dia.)
   lambda y: Left(y)))
      # Mengembalikan anak kiri dari y ketika tidak isExistLeft

print(BSTFind(
   Left(GivenATree), # PohonBiner kita
   lambda x: isExistRight(x), 
      # Fungsi yang akan dioperasikan (isExistRight mengecek apakah terdapat bilangan yang lebih besar dari dia.)
   lambda y: Right(y)))
      # Mengembalikan anak kanan dari y ketika tidak isExistRight

print(isRightSubLeftPos(
   GivenATree, 
   lambda x : x > 0))