in_chainz

Untitled

Dec 19th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. class Node:
  2.     def __init__(self, x):
  3.         self.key = x
  4.         self.left = None
  5.         self.right = None
  6.  
  7.  
  8. def insert(tree, x):
  9.     if tree is None:
  10.         return Node(x)
  11.     if x < tree.key:
  12.         tree.left = insert(tree.left, x)
  13.     elif x > tree.key:
  14.         tree.right = insert(tree.right, x)
  15.     return tree
  16.  
  17.  
  18. def height(tree):
  19.     if tree is None:
  20.         return 0
  21.     return max(height(tree.left), height(tree.right)) + 1
  22.  
  23.  
  24. def go(tree):
  25.     if tree is None:
  26.         return
  27.     go(tree.left)
  28.     print(tree.key, end=' ')
  29.     go(tree.right)
  30.  
  31.  
  32. def traverse(tree):
  33.     go(tree)
  34.     print()
  35.  
  36.  
  37. a = list(map(int, input().split()))[:-1]
  38. tree = None
  39. for i in a:
  40.     tree = insert(tree, i)
  41. # traverse(tree)
  42. print(height(tree))
Advertisement
Add Comment
Please, Sign In to add comment