• 1.
    class Tree(object):
  • 2.
      x = 0
  • 3.
      l = None
  • 4.
      r = None
  • 5.
     
  • 6.
    def has_children(T):
  • 7.
        children = [ test for test in T[1:] if test is not None]
  • 8.
        return bool(len(children))
  • 9.
     
  • 10.
    T = (5, (6, (7, None, None), (9, None, None)), (10, (20, None, None), None))
  • 11.
    # T = (5, (3, None, None), (10, None, None))
  • 12.
     
  • 13.
    global height
  • 14.
     
  • 15.
    height = 0
  • 16.
    is_root_call = True
  • 17.
     
  • 18.
    def child_is_higher(child_val, parent_val):
  • 19.
        return child_val > parent_val
  • 20.
     
  • 21.
    def rec_function(node, height, is_root_call):
  • 22.
        if is_root_call:
  • 23.
            is_root_call = False
  • 24.
        else:
  • 25.
            height += 1
  • 26.
        print(height * "----- " + " => Je rentre dans un nouveau node et suis [{}] à hauteur {}".format(node[0], height))
  • 27.
        if has_children(node):
  • 28.
            children = node[1:]
  • 29.
            for child in children:
  • 30.
                if child is not None and child_is_higher(child[0], node[0]):
  • 31.
                    rec_function(child, height, is_root_call)
  • 32.
                else:
  • 33.
                    continue
  • 34.
     
  • 35.
    rec_function(T, height, is_root_call)
class Tree(object): x = 0 l = None r = None def has_children(T): children = [ test for test in T[1:] if test is not None] return bool(len(children)) T = (5, (6, (7, None, None), (9, None, None)), (10, (20, None, None), None)) # T = (5, (3, None, None), (10, None, None)) global height height = 0 is_root_call = True def child_is_higher(child_val, parent_val): return child_val > parent_val def rec_function(node, height, is_root_call): if is_root_call: is_root_call = False else: height += 1 print(height * "----- " + " => Je rentre dans un nouveau node et suis [{}] à hauteur {}".format(node[0], height)) if has_children(node): children = node[1:] for child in children: if child is not None and child_is_higher(child[0], node[0]): rec_function(child, height, is_root_call) else: continue rec_function(T, height, is_root_call)

Copy Code to Clipboard Raw

Sign-in or Create a Free Account.

Snippi uses Twitter or Facebook to register and create accounts. Registration will enable you to keep track of your snippets. We never spam your timeline or wall. Ever.

Sign-in with Facebook Sign-in with Twitter