utils.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import pytest
  2. # 链表
  3. class ListNode:
  4. def __init__(self, val=0, next=None):
  5. self.val = val
  6. self.next = next
  7. # 数组转链表
  8. def arr_to_linklist(arr):
  9. p = None
  10. for n in reversed(arr):
  11. node = ListNode(n)
  12. node.next = p
  13. p = node
  14. return p
  15. # 链表转数组
  16. def linkList_to_arr(head):
  17. res = []
  18. cur = head
  19. while cur is not None:
  20. res.append(cur.val)
  21. cur = cur.next
  22. return res
  23. # 数组转链表
  24. def list_to_linklist(arr):
  25. head = ListNode(arr[0])
  26. p = head
  27. for i in range(1, len(arr)):
  28. p.next = ListNode(arr[i])
  29. p = p.next
  30. return head
  31. # 树
  32. # class Bnode(object):
  33. # def __init__(self, data, left=None, right=None):
  34. # self.data = data
  35. # self.left = left
  36. # self.right = right
  37. #
  38. # class Btree(object):
  39. # def __init__(self, root=None):
  40. # self.root = root
  41. #
  42. # @classmethod
  43. # def bulid(cls, list):
  44. # node_dict = {}
  45. # # 制作节点
  46. # for item in list:
  47. # data = item['data']
  48. # node_dict[data] = Bnode(data)
  49. # # 把节点组装成数
  50. # for item in list:
  51. # data = item['data']
  52. # node = node_dict[data]
  53. # if node['is_root']:
  54. # root = node
  55. # node.left = node_dict.get(item['left'])
  56. # node.right = node_dict.get(item['right'])
  57. # return cls(root)
  58. #
  59. # def preorder(self, subtree):
  60. # # 先序
  61. # if subtree is not None:
  62. # print(subtree.data)
  63. # self.preorder(subtree.left)
  64. # self.preorder(subtree.right)
  65. #
  66. # def reverse(self, subtree):
  67. # # 后序
  68. # if subtree is not None:
  69. # subtree.left, subtree.right = subtree.right, subtree.left
  70. # self.reverse(self, subtree.left)
  71. # self.reverse(self, subtree.right)
  72. #定义二叉树结点类型
  73. class BiTNode:
  74. def __init__(self,root):
  75. self.root = root
  76. self.left_child = None
  77. self.right_child = None
  78. #前序遍历
  79. def print_tree_pre_order(root):
  80. # 先判断二叉树是否为空
  81. if root is None:
  82. return root
  83. # 先根
  84. print(root.data)
  85. # 再左
  86. if root.left_child is not None:
  87. print_tree_pre_order(root.left_child)
  88. # 再右
  89. if root.right_child is not None:
  90. print_tree_pre_order(root.right_child)
  91. #中序遍历二叉树
  92. def print_tree_mid_order(root):
  93. # 先判断二叉树是否为空,当左右节点都为空时
  94. if root is None:
  95. return
  96. # 中序遍历 左根右
  97. # 遍历左子树
  98. if root.left_child is not None:
  99. print_tree_mid_order(root.left_child)
  100. # 遍历根节点
  101. print(root.data)
  102. # 遍历右子树
  103. if root.right_child is not None:
  104. print_tree_mid_order(root.right_child)
  105. #后序遍历
  106. def print_tree_after_order(root):
  107. # 先判断二叉树是否为空
  108. if root is None:
  109. return root
  110. # 再左
  111. if root.left_child is not None:
  112. print_tree_after_order(root.left_child)
  113. # 再右
  114. if root.right_child is not None:
  115. print_tree_after_order(root.right_child)
  116. # 先根
  117. print(root.data)
  118. def array_to_bitree(array):
  119. # 判断arr是否为空
  120. if len(array) == 0:
  121. return BiTNode(array[0])
  122. mid = len(array) // 2 # 有序数组的中间元素的下标
  123. if len(array) > 0:
  124. # 将中间元素作为二叉树的根
  125. root = BiTNode(array[mid])
  126. # 如果左边的元素个数不为零,则递归调用函数,生成左子树
  127. if len(array[:mid]) > 0:
  128. root.left_child = array_to_bitree(array[:mid])
  129. # 如果右边的元素个数不为零,则递归调用函数,生成左子树
  130. if len(array[mid + 1:]) > 0:
  131. root.right_child = array_to_bitree(array[mid + 1:])
  132. return root