Advertisement
Guest User

Untitled

a guest
Jun 17th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.23 KB | None | 0 0
  1. class Node:
  2.  
  3.     def __init__(self, data = None):
  4.  
  5.         self.left = None
  6.         self.right = None
  7.         self.data = data
  8.         if self.data:
  9.             self.count = 1
  10.         else:
  11.             self.count = 0
  12.  
  13.     def insert(self, data):
  14.         if self.data:
  15.             if data < self.data:
  16.                 if self.left is None:
  17.                     self.left = Node(data)
  18.                 else:
  19.                     self.left.insert(data)
  20.             elif data > self.data:
  21.                 if self.right is None:
  22.                     self.right = Node(data)
  23.                 else:
  24.                     self.right.insert(data)
  25.             else :
  26.                 self.count = self.count + 1
  27.         else:
  28.             self.data = data
  29.             self.count = 1
  30.  
  31. # Print the tree
  32.     def PrintTree(self):
  33.         if self.left:
  34.             self.left.PrintTree()
  35.         print( self.data , self.count),
  36.         if self.right:
  37.             self.right.PrintTree()
  38.  
  39. # Use the insert method to add nodes
  40. root = Node()
  41. root.insert("Schimur")
  42. root.insert("ist")
  43. root.insert("der")
  44. root.insert("schlechteste")
  45. root.insert("schlechteste")
  46. root.insert("schlechteste")
  47. root.insert("Bardo")
  48. root.insert("top")
  49.  
  50. root.PrintTree()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement