class Node():
'''
节点类型
'''
def __init__(self,data,level=0,fatherNode = None):
self.data = data#节点数据
self.level = level#节点类型
self.fatherNode = fatherNode#父节点
self.children = []#子节点
self.path = ''#节点路径
def getPath(self,path=''):
path = self.data + '/' + path if path else self.data
if self.fatherNode == None:
return path
else:
return self.fatherNode.getPath(path)
class MenuTree():
def __init__(self,rootData):
self.initTree(rootData)
def initTree(self,rootData):
self.root = Node(rootData)
rootChildNode_1 = Node('第一章',level=1,fatherNode=self.root)
node1 = Node('1.1', level=2,fatherNode=rootChildNode_1)
node2 = Node('1.2',level=2,fatherNode=rootChildNode_1)
node3 = Node('1.3',level=2,fatherNode=rootChildNode_1)
node4 = Node('1.4',level=2,fatherNode=rootChildNode_1)
node5 = Node('1.5',level=2,fatherNode=rootChildNode_1)
rootChildNode_1.children.append(node1)
rootChildNode_1.children.append(node2)
rootChildNode_1.children.append(node3)
rootChildNode_1.children.append(node4)
rootChildNode_1.children.append(node5)
self.root.children.append(rootChildNode_1)
rootChildNode2 = Node('第二章',level=1,fatherNode=self.root)
node1 = Node('2.1', level=2, fatherNode=rootChildNode2)
node2 = Node('2.2', level=2, fatherNode=rootChildNode2)
node3 = Node('2.3', level=2, fatherNode=rootChildNode2)
node4 = Node('2.4', level=2, fatherNode=rootChildNode2)
node5 = Node('2.5', level=2, fatherNode=rootChildNode2)
rootChildNode2.children.append(node1)
rootChildNode2.children.append(node2)
rootChildNode2.children.append(node3)
rootChildNode2.children.append(node4)
rootChildNode2.children.append(node5)
self.root.children.append(rootChildNode2)
rootChildNode3 = Node('第三章',level=1,fatherNode=self.root)
node1 = Node('3.1', level=2, fatherNode=rootChildNode3)
node2 = Node('3.2', level=2, fatherNode=rootChildNode3)
node3 = Node('3.3', level=2, fatherNode=rootChildNode3)
node4 = Node('3.4', level=2, fatherNode=rootChildNode3)
node5 = Node('3.5', level=2, fatherNode=rootChildNode3)
rootChildNode3.children.append(node1)
rootChildNode3.children.append(node2)
rootChildNode3.children.append(node3)
rootChildNode3.children.append(node4)
rootChildNode3.children.append(node5)
self.root.children.append(rootChildNode3)
rootChildNode4 = Node('第四章',level=1,fatherNode=self.root)
node1 = Node('4.1', level=2, fatherNode=rootChildNode4)
node2 = Node('4.2', level=2, fatherNode=rootChildNode4)
node3 = Node('4.3', level=2, fatherNode=rootChildNode4)
node4 = Node('4.4', level=2, fatherNode=rootChildNode4)
node5 = Node('4.5', level=2, fatherNode=rootChildNode4)
rootChildNode4.children.append(node1)
rootChildNode4.children.append(node2)
rootChildNode4.children.append(node3)
rootChildNode4.children.append(node4)
rootChildNode4.children.append(node5)
self.root.children.append(rootChildNode4)
rootChildNode5 = Node('第五章',level=1,fatherNode=self.root)
node1 = Node('5.1', level=2, fatherNode=rootChildNode5)
node2 = Node('5.2', level=2, fatherNode=rootChildNode5)
node3 = Node('5.3', level=2, fatherNode=rootChildNode5)
node4 = Node('5.4', level=2, fatherNode=rootChildNode5)
node5 = Node('5.5', level=2, fatherNode=rootChildNode5)
rootChildNode5.children.append(node1)
rootChildNode5.children.append(node2)
rootChildNode5.children.append(node3)
rootChildNode5.children.append(node4)
rootChildNode5.children.append(node5)
self.root.children.append(rootChildNode5)
rootChildNode6 = Node('第六章',level=1,fatherNode=self.root)
node1 = Node('6.1', level=2, fatherNode=rootChildNode6)
node2 = Node('6.2', level=2, fatherNode=rootChildNode6)
node3 = Node('6.3', level=2, fatherNode=rootChildNode6)
node4 = Node('6.4', level=2, fatherNode=rootChildNode6)
node5 = Node('6.5', level=2, fatherNode=rootChildNode6)
rootChildNode6.children.append(node1)
rootChildNode6.children.append(node2)
rootChildNode6.children.append(node3)
rootChildNode6.children.append(node4)
rootChildNode6.children.append(node5)
self.root.children.append(rootChildNode6)
@staticmethod
def viewTree(node):
level = node.level
print(' '*level+node.data)
children = node.children
for itemNode in children:
MenuTree.viewTree(itemNode)
@staticmethod
def findNodeByData(node,nodeData):
if node.data == nodeData:
return node
else:
children = node.children
if len(children)>0:
for child in children:
revNode = MenuTree.findNodeByData(child, nodeData)
if revNode!=None:
return revNode
else:
pass
tree = MenuTree('目录')
# Tree.viewTree(tree.root)
while True:
recevieStr = input('查看树输入1,添加树输入2,查找树节点输入3,删除树节点输入4:')
if recevieStr == '1':
print('查看树')
MenuTree.viewTree(tree.root)
elif recevieStr == '2':
print('添加树')
inputNewTree = input('请输入插入节点')
elif recevieStr == '3':
print('查找树节点')
findNodeData = input('请输入查找节点')
findNode = MenuTree.findNodeByData(tree.root, findNodeData)
if findNode == None:
print('没有这个节点')
else:
print('find success')
path = findNode.getPath()
print(path)
elif recevieStr == '4':
print('删除树节点')
'''
节点类型
'''
def __init__(self,data,level=0,fatherNode = None):
self.data = data#节点数据
self.level = level#节点类型
self.fatherNode = fatherNode#父节点
self.children = []#子节点
self.path = ''#节点路径
def getPath(self,path=''):
path = self.data + '/' + path if path else self.data
if self.fatherNode == None:
return path
else:
return self.fatherNode.getPath(path)
class MenuTree():
def __init__(self,rootData):
self.initTree(rootData)
def initTree(self,rootData):
self.root = Node(rootData)
rootChildNode_1 = Node('第一章',level=1,fatherNode=self.root)
node1 = Node('1.1', level=2,fatherNode=rootChildNode_1)
node2 = Node('1.2',level=2,fatherNode=rootChildNode_1)
node3 = Node('1.3',level=2,fatherNode=rootChildNode_1)
node4 = Node('1.4',level=2,fatherNode=rootChildNode_1)
node5 = Node('1.5',level=2,fatherNode=rootChildNode_1)
rootChildNode_1.children.append(node1)
rootChildNode_1.children.append(node2)
rootChildNode_1.children.append(node3)
rootChildNode_1.children.append(node4)
rootChildNode_1.children.append(node5)
self.root.children.append(rootChildNode_1)
rootChildNode2 = Node('第二章',level=1,fatherNode=self.root)
node1 = Node('2.1', level=2, fatherNode=rootChildNode2)
node2 = Node('2.2', level=2, fatherNode=rootChildNode2)
node3 = Node('2.3', level=2, fatherNode=rootChildNode2)
node4 = Node('2.4', level=2, fatherNode=rootChildNode2)
node5 = Node('2.5', level=2, fatherNode=rootChildNode2)
rootChildNode2.children.append(node1)
rootChildNode2.children.append(node2)
rootChildNode2.children.append(node3)
rootChildNode2.children.append(node4)
rootChildNode2.children.append(node5)
self.root.children.append(rootChildNode2)
rootChildNode3 = Node('第三章',level=1,fatherNode=self.root)
node1 = Node('3.1', level=2, fatherNode=rootChildNode3)
node2 = Node('3.2', level=2, fatherNode=rootChildNode3)
node3 = Node('3.3', level=2, fatherNode=rootChildNode3)
node4 = Node('3.4', level=2, fatherNode=rootChildNode3)
node5 = Node('3.5', level=2, fatherNode=rootChildNode3)
rootChildNode3.children.append(node1)
rootChildNode3.children.append(node2)
rootChildNode3.children.append(node3)
rootChildNode3.children.append(node4)
rootChildNode3.children.append(node5)
self.root.children.append(rootChildNode3)
rootChildNode4 = Node('第四章',level=1,fatherNode=self.root)
node1 = Node('4.1', level=2, fatherNode=rootChildNode4)
node2 = Node('4.2', level=2, fatherNode=rootChildNode4)
node3 = Node('4.3', level=2, fatherNode=rootChildNode4)
node4 = Node('4.4', level=2, fatherNode=rootChildNode4)
node5 = Node('4.5', level=2, fatherNode=rootChildNode4)
rootChildNode4.children.append(node1)
rootChildNode4.children.append(node2)
rootChildNode4.children.append(node3)
rootChildNode4.children.append(node4)
rootChildNode4.children.append(node5)
self.root.children.append(rootChildNode4)
rootChildNode5 = Node('第五章',level=1,fatherNode=self.root)
node1 = Node('5.1', level=2, fatherNode=rootChildNode5)
node2 = Node('5.2', level=2, fatherNode=rootChildNode5)
node3 = Node('5.3', level=2, fatherNode=rootChildNode5)
node4 = Node('5.4', level=2, fatherNode=rootChildNode5)
node5 = Node('5.5', level=2, fatherNode=rootChildNode5)
rootChildNode5.children.append(node1)
rootChildNode5.children.append(node2)
rootChildNode5.children.append(node3)
rootChildNode5.children.append(node4)
rootChildNode5.children.append(node5)
self.root.children.append(rootChildNode5)
rootChildNode6 = Node('第六章',level=1,fatherNode=self.root)
node1 = Node('6.1', level=2, fatherNode=rootChildNode6)
node2 = Node('6.2', level=2, fatherNode=rootChildNode6)
node3 = Node('6.3', level=2, fatherNode=rootChildNode6)
node4 = Node('6.4', level=2, fatherNode=rootChildNode6)
node5 = Node('6.5', level=2, fatherNode=rootChildNode6)
rootChildNode6.children.append(node1)
rootChildNode6.children.append(node2)
rootChildNode6.children.append(node3)
rootChildNode6.children.append(node4)
rootChildNode6.children.append(node5)
self.root.children.append(rootChildNode6)
@staticmethod
def viewTree(node):
level = node.level
print(' '*level+node.data)
children = node.children
for itemNode in children:
MenuTree.viewTree(itemNode)
@staticmethod
def findNodeByData(node,nodeData):
if node.data == nodeData:
return node
else:
children = node.children
if len(children)>0:
for child in children:
revNode = MenuTree.findNodeByData(child, nodeData)
if revNode!=None:
return revNode
else:
pass
tree = MenuTree('目录')
# Tree.viewTree(tree.root)
while True:
recevieStr = input('查看树输入1,添加树输入2,查找树节点输入3,删除树节点输入4:')
if recevieStr == '1':
print('查看树')
MenuTree.viewTree(tree.root)
elif recevieStr == '2':
print('添加树')
inputNewTree = input('请输入插入节点')
elif recevieStr == '3':
print('查找树节点')
findNodeData = input('请输入查找节点')
findNode = MenuTree.findNodeByData(tree.root, findNodeData)
if findNode == None:
print('没有这个节点')
else:
print('find success')
path = findNode.getPath()
print(path)
elif recevieStr == '4':
print('删除树节点')