tree.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #列表函数
  2. def BinaryTree(r):
  3. return [r, [], []]
  4. #插入左子树
  5. def insertLeft(root, newBranch):
  6. t = root.pop(1)
  7. if len(t) > 1:
  8. root.insert(1, [newBranch, t, []])
  9. else:
  10. root.insert(1, [newBranch, [], []])
  11. return root
  12. #插入右子树
  13. def insertRight(root, newBranch):
  14. t = root.pop(2)
  15. if len(t) > 1:
  16. root.insert(2, [newBranch, [], t])
  17. else:
  18. root.insert(2, [newBranch, [], []])
  19. return root
  20. #树的访问函数
  21. def getRootVal(root):
  22. return root[0]
  23. def setRootVal(root, newval):
  24. root[0] = newval
  25. def getLeftChild(root):
  26. return root[1]
  27. def getRightChild(root):
  28. return root[2]
  29. # 二叉树的访问函数
  30. def getLeftChild(self):
  31. return self.leftChild
  32. def getRightChild(self):
  33. return self.rightChild
  34. def setRootValue(self, obj):
  35. self.key = obj
  36. def getRootValue(self):
  37. return self.key
  38. # 定义节点
  39. class BinaryTree:
  40. def __init__(self, rootObj):
  41. self.key = rootObj
  42. self.leftChild = None # 左子节点也是树
  43. self.rightChild = None # 右子节点也是树
  44. # 插入左子节点
  45. def insertLeft(self, newNode):
  46. if self.leftChild == None:
  47. self.leftChild = newNode
  48. else:
  49. t = BinaryTree(newNode)
  50. t.leftChild = self.leftChild
  51. self.leftChild = t
  52. # 插入右子节点
  53. def insertRight(self, newNode):
  54. if self.rightChild == None:
  55. self.rightChild = newNode
  56. else:
  57. t = BinaryTree(newNode)
  58. t.rightChild = self.rightChild
  59. self.rightChild = t
  60. # 树的遍历
  61. # 前序
  62. def preorder(tree):
  63. if tree:
  64. print(tree.getRootVal())
  65. preorder(tree.getLeftChild())
  66. preorder(tree.getRightChild())
  67. # 中序
  68. def inorder(tree):
  69. if tree:
  70. inorder(tree.getLeftChild())
  71. print(tree.getRootVal())
  72. inorder(tree.getRightChild())
  73. # 后序
  74. def postorder(tree):
  75. if tree:
  76. postorder(tree.getLeftChild())
  77. postorder(tree.getRightChild())
  78. print(tree.getRootVal())
  79. # 树
  80. # class Bnode(object):
  81. # def __init__(self, data, left=None, right=None):
  82. # self.data = data
  83. # self.left = left
  84. # self.right = right
  85. #
  86. # class Btree(object):
  87. # def __init__(self, root=None):
  88. # self.root = root
  89. #
  90. # @classmethod
  91. # def bulid(cls, list):
  92. # node_dict = {}
  93. # # 制作节点
  94. # for item in list:
  95. # data = item['data']
  96. # node_dict[data] = Bnode(data)
  97. # # 把节点组装成数
  98. # for item in list:
  99. # data = item['data']
  100. # node = node_dict[data]
  101. # if node['is_root']:
  102. # root = node
  103. # node.left = node_dict.get(item['left'])
  104. # node.right = node_dict.get(item['right'])
  105. # return cls(root)
  106. #
  107. # def preorder(self, subtree):
  108. # # 先序
  109. # if subtree is not None:
  110. # print(subtree.data)
  111. # self.preorder(subtree.left)
  112. # self.preorder(subtree.right)
  113. #
  114. # def reverse(self, subtree):
  115. # # 后序
  116. # if subtree is not None:
  117. # subtree.left, subtree.right = subtree.right, subtree.left
  118. # self.reverse(self, subtree.left)
  119. # self.reverse(self, subtree.right)
  120. # 回溯通用模板
  121. # res = []
  122. #
  123. # def backtrack(路径, 选择列表):
  124. # if 满足结束条件:
  125. # res.append(路径)
  126. # return
  127. #
  128. # if 满足剪枝条件: return
  129. #
  130. # for 选择 in 选择列表:
  131. # 做选择
  132. # backtrack(路径, 选择列表)
  133. # 撤销选择