Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node:
- def __init__(self, value):
- self.value = value
- self.left = None
- self.right = None
- def insert(self , value):
- current = self
- previous = None
- while current:
- if current.value > value:
- previous = current
- current = current.left
- else:
- previous = current
- current = current.right
- if previous.value > value:
- previous.left = Node(value)
- else:
- previous.right = Node(value)
- def in_order_traversal(self):
- current = self
- stack = list()
- while current or len(stack):
- if current:
- stack.append(current)
- current = current.left
- else:
- current = stack.pop()
- print(current.value)
- current = current.right
- def print(self):
- self.in_order_traversal()
- t = Node(1)
- t.insert(43)
- t.insert(55)
- t.insert(18)
- t.insert(25)
- t.insert(13)
- t.insert(3)
- t.insert(60)
- t.print()
Advertisement
Add Comment
Please, Sign In to add comment