简单二叉树结构
class Node:
def __init__(self,data):
self.data = data
self.left = None
self.right = None
return
class Tree:
def __init__(self):
self.head = None
self.left = None
self.right = None
return
def add_left(self,item):
if not isinstance(item,Node):
item = Node(item)
if not self.head:
self.head = item
else:
self.head.left = item
return
def add_right(self,item):
if not isinstance(item,Node):
item = Node(item)
if not self.head:
self.head = item
else:
self.head.right = item
return
tree = Tree()
tree.add_left(2)
tree.add_right(4)
tree.add_left(3)
print(tree.head.data)
current_node = tree.head
print(tree.head.left.data,current_node.right.data)