当前位置:   article > 正文

python 三序遍历的迭代和递归实现_python3 层序遍历

python3 层序遍历

 1、一些关于生成和遍历打印链表节点的测试代码

  1. class LinkNode():
  2. # 单链表的节点
  3. def __init__(self, val=0, next=None):
  4. self.val = val
  5. self.next = next
  6. def generate_link(arr):
  7. # input:数组
  8. # output:链表首节点
  9. res = pre = LinkNode()
  10. for i in arr:
  11. tmp = LinkNode(val=i)
  12. pre.next = tmp
  13. pre = pre.next
  14. return res.next
  15. def look_link(root):
  16. # 遍历打印链表
  17. while root:
  18. # print(root.val)
  19. root = root.next
  20. print("look_link finish")
  21. look_link(generate_link(list(range(10))))
  22. class BinaryTreeNode():
  23. # 二叉树节点
  24. def __init__(self, val=0, left=None, right=None):
  25. self.val = val
  26. self.left = left
  27. self.right = right
  28. def generate_tree(arr):
  29. # input:数组
  30. # output:二叉树首节点
  31. if not arr:
  32. return
  33. # 原数组上修改
  34. arr[0] = BinaryTreeNode(arr[0])
  35. for i in range(1, len(arr)):
  36. # print('----arr[i]', i, arr[i])
  37. if arr[i] is not None:
  38. arr[i] = BinaryTreeNode(arr[i])
  39. # print('\t', i & 1, i // 2, arr[i])
  40. # 利用二叉树的节点位置和其左子树右子树位置的关系
  41. # 节点A的位置是n,则左节点是2n,右节点是2n + 1
  42. if i & 1 == 1:
  43. # 奇数:是左节点
  44. arr[(i -1) // 2].left = arr[i]
  45. else:
  46. # 偶数:是右节点
  47. arr[(i -1) // 2].right = arr[i]
  48. # print('---generate_tree-', arr)
  49. # for i in arr:
  50. # print(i.val)
  51. return arr[0]
  52. def look_tree(root):
  53. # 遍历打印二叉树
  54. if not root:
  55. return
  56. print("look_tree start")
  57. # 层序遍历
  58. stack = [root]
  59. while stack:
  60. tmp = stack.pop(0)
  61. print("\tlook: ", tmp.val)
  62. if tmp.val is None:
  63. continue
  64. if tmp.left:
  65. stack.append(tmp.left)
  66. if tmp.right:
  67. stack.append(tmp.right)
  68. print("look_tree finish")
  69. # look_tree(generate_tree([1,2,5,3,4,None,6]))
  70. # 保证二叉树的左边 < 中间< 右边

层序遍历

  1. # 层序遍历
  2. r = generate_tree([1,2,5,3,4,None,6])
  3. # levelOrder(r)
  4. # 迭代(一维)
  5. def one_level_diedai(root):
  6. res = []
  7. stack = [root]
  8. while stack:
  9. node = stack.pop(0)
  10. res.append(node.val)
  11. if node.left:
  12. stack.append(node.left)
  13. if node.right:
  14. stack.append(node.right)
  15. return res
  16. res = one_level_diedai(r)
  17. print("----one_level_diedai ", res)
  18. # 迭代 (二维数组)
  19. def levelOrder(root):
  20. if not root:
  21. return
  22. print("levelOrder start")
  23. res = []
  24. stack = [root]
  25. while stack:
  26. tmp_list = []
  27. length = len(stack)
  28. for i in range(length): # 每次弹出当层的次数
  29. tmp = stack.pop(0) # 先进先出
  30. print("\tlook: ", tmp.val)
  31. if tmp.val is None:
  32. continue
  33. tmp_list.append(tmp.val)
  34. if tmp.left:
  35. stack.append(tmp.left)
  36. if tmp.right:
  37. stack.append(tmp.right)
  38. res.append(tmp_list)
  39. print("levelOrder finish", res)
  40. return res

前序遍历

  1. # 迭代
  2. def first_order_diedai(root):
  3. res = []
  4. stack = []
  5. while stack or root:
  6. while root:
  7. stack.append(root)
  8. res.append(root.val) # 注意
  9. root = root.left
  10. root = stack.pop()
  11. root = root.right
  12. return res
  13. # 递归
  14. def first_order_digui(root):
  15. if root:
  16. res.append(root.val)
  17. first_order_digui(root.left)
  18. first_order_digui(root.right)
  19. r = generate_tree([1,2,5,3,4,None,6])
  20. # res = []
  21. # firstOrder_digui(r)
  22. # print("----res ", res)
  23. res = first_order_diedai(r)
  24. print("----first_order_diedai ", res)

中序遍历

  1. # 中序遍历
  2. def mid_bianli(root):
  3. res = []
  4. stack = []
  5. while stack or root:
  6. while root:
  7. stack.append(root)
  8. root = root.left
  9. root = stack.pop()
  10. res.append(root.val) # 注意
  11. root = root.right
  12. return res
  13. # 递归
  14. def mid_bianli_digui(root):
  15. if root:
  16. mid_bianli_digui(root.left)
  17. res.append(root.val) # 注意
  18. mid_bianli_digui(root.right)
  19. res = mid_bianli(r)
  20. print("----mid_bianli ", res)

后序遍历

  1. # 迭代
  2. # 左右中:即中右左的翻转
  3. def last_bianli(root):
  4. res = []
  5. stack = []
  6. while stack or root:
  7. while root:
  8. stack.append(root)
  9. res.append(root.val) # 先序的倒序
  10. root = root.right # 从右子树开始
  11. root = stack.pop()
  12. root = root.left
  13. return res[::-1] # 倒序
  14. # 递归
  15. def first_order_digui(root):
  16. if root:
  17. first_order_digui(root.left)
  18. first_order_digui(root.right)
  19. res.append(root.val)
  20. res = last_bianli(r)
  21. print("----last_bianli ", res)
  22. # res = []
  23. # first_order_digui(r)
  24. # print("----first_order_digui ", res)

链表展开

  1. def flatten(root):
  2. # 把每个子树合成链表
  3. # 把左子树的节点一次插入到根节点和右节点中间
  4. if not root:
  5. # 空节点
  6. return
  7. print("flatten", root, root.val)
  8. # 只有右节点时不用处理
  9. flatten(root.left)
  10. flatten(root.right)
  11. if root.left:
  12. print('0----left', root.left, root.left.val)
  13. # 把 左节点插入到根节点和右节点中间
  14. tmp = root.right # 暂存右节点
  15. root.right = root.left # 把 右节点指向原先的左节点
  16. tmp2 = root.right
  17. while tmp2 and tmp2.right:
  18. tmp2 = tmp2.right
  19. tmp2.right = tmp # 找到左子树的最右节点,然后指向 根节点的右节点
  20. root.left = None # 把左节点置空
  21. print('\t', root.left, root.right.val)
  22. r = generate_tree([1,2,5,3,4,None,6])
  23. look_tree(r)
  24. flatten(r)
  25. look_tree(r) # 1,2,3,4,5,6

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/514835
推荐阅读
相关标签
  

闽ICP备14008679号