class Node:
def __init__(self,data):
self.left=None
self.right=None
self.data=data
def insert(self,data):
if(self.data):
if(data<self.data):
if(self.left is None):
self.left=Node(data)
else:
self.left.insert(data)
elif(data>self.data):
if(self.right is None):
self.right=Node(data)
else:
self.right.insert(data)
else:
self.data=data
def printdata(self):
print(self.data)
def fore(self):
if(self.data):
print(self.data)
if(self.left):
self.left.fore()
if(self.right):
self.right.fore()
def mid(self):
if(self.left):
self.left.mid()
print(self.data)
if(self.right):
self.right.mid()
def back(self):
if(self.left):
self.left.back()
if(self.right):
self.right.back()
print(self.data)
def ceng(self,root):
queue = []
cur = [root]
while cur:
cur_layer_val = []
next_layer_node = []
for node in cur:
if node:
cur_layer_val.append(node.data)
next_layer_node.extend([node.left, node.right])
if cur_layer_val:
queue.insert(0, cur_layer_val)
cur = next_layer_node
print(queue)
root=Node(5)
root.insert(4)
root.insert(7)
root.insert(3)
root.insert(8)
root.printdata()
root.fore()
root.mid()
root.back()
root.ceng(root)