SHARE
TWEET

Untitled

a guest Jul 22nd, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/local/bin/python3
  2.  
  3. import pdb
  4.  
  5. class node():
  6.     def __init__(self, x , left = None, right = None, parent = None):
  7.         self.val = x
  8.         self.left = left
  9.         self.right = right
  10.         self.parent = parent
  11.  
  12. def addMe(root, node):
  13.     if root is None:
  14.         return node
  15.     else:
  16.         if node.val < root.val:
  17.             root.left = addMe(root.left, node)
  18.         else:
  19.             root.right = addMe(root.right, node)
  20.  
  21.     return root
  22.  
  23. def inorder(root):
  24.  
  25.     if root is not None:
  26.         inorder(root.left)
  27.         print(root.val)
  28.         inorder(root.right)
  29.    
  30. def preorder(root):
  31.    
  32.     if root is not None:
  33.         print(root.val)
  34.         preorder(root.left)
  35.         preorder(root.right)
  36.    
  37.  
  38. def postorder(root):
  39.    
  40.     if root is not None:
  41.         postorder(root.right)
  42.         postorder(root.left)
  43.         print(root.val)
  44.  
  45. def findTreeHeight(root):
  46.     pass
  47.  
  48.    
  49. if __name__ == '__main__':
  50.     root = None
  51.  
  52.     root = addMe(root,node(10))
  53.     root = addMe(root,node(12))
  54.     root = addMe(root,node(11))
  55.     root = addMe(root,node(13))
  56.     root = addMe(root,node(7))
  57.     root = addMe(root,node(8))
  58.     root = addMe(root,node(5))
  59.     root = addMe(root,node(6))
  60.     root = addMe(root,node(4))
  61.  
  62.  
  63.  
  64.     inorder(root)
  65.     print("#######")
  66.     preorder(root)
  67.     print("#######")
  68.     postorder(root)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top