Python已知后序遍历和中序遍历,求先序遍历

打印 上一主题 下一主题

主题 999|帖子 999|积分 2997

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
步调一:树的构建

字典

  1. def createTree(arr1,arr2,tree):
  2.     if len(arr1)==0 and len(arr2)==0 :return
  3.     root = len(arr1)-1
  4.     # print(arr1[root],root)
  5.     flag = arr2.index(arr1[root])
  6.     # print(flag)
  7.     len_right = len(arr2)-flag-1
  8.     len_left = flag
  9.     if len(arr2[:flag])>=1:
  10.         lchild = arr1[flag-1]
  11.     else:
  12.         lchild = None
  13.     if len(arr2[flag+1:])>=1:
  14.         rchild = arr1[root-1]
  15.     else:
  16.         rchild = None
  17.    
  18.     tree[arr1[root]] = {'lchild':None,'rchild':None}
  19.     tree[arr1[root]]['lchild'] = lchild
  20.     tree[arr1[root]]['rchild'] = rchild
  21.    
  22.     # print(tree)
  23.     # print(arr1[:flag],arr1[len_left:len_left+len_right])
  24.     # print(arr2[:flag],arr2[flag+1:])
  25.     createTree(arr1[:flag],arr2[:flag],tree) # 左子树
  26.     createTree(arr1[len_left:len_left+len_right],arr2[flag+1:],tree) #右子树
  27. tree = dict()
  28. back = [3,4,2,6,5,1]
  29. mid = [3,2,4,1,6,5]
  30. createTree(back,mid,tree)
复制代码
  1. {1: {'lchild': 2, 'rchild': 5},
  2. 2: {'lchild': 3, 'rchild': 4},
  3. 3: {'lchild': None, 'rchild': None},
  4. 4: {'lchild': None, 'rchild': None},
  5. 5: {'lchild': 6, 'rchild': None},
  6. 6: {'lchild': None, 'rchild': None}}
复制代码
多级嵌套字典

  1. def set_nested_dict_value(d, keys, value):
  2.     """
  3.     根据键列表设置嵌套字典的值
  4.     :param d: 原始字典
  5.     :param keys: 键列表
  6.     :param value: 要设置的值
  7.     """
  8.     # 从第一个键开始,逐层深入
  9.     for key in keys[:-1]:
  10.         # 如果当前键不存在,则创建一个空字典
  11.         if key not in d:
  12.             d[key] = {}
  13.         # 下一层字典
  14.         d = d[key]
  15.     # 设置最后一个键的值
  16.     d[keys[-1]] = value
  17. def createTree(arr1,arr2,tree,step):
  18.     print('------------------------------------------')
  19.     # if len(arr1)==0 and len(arr2)==0 :
  20.     #     print('结束')
  21.     #     return
  22.     root = len(arr1)-1
  23.     print(arr1[root],root)
  24.     flag = arr2.index(arr1[root])
  25.     print(flag)
  26.     len_right = len(arr2)-flag-1
  27.     len_left = flag
  28.     if len(arr2[:flag])>=1:
  29.         lchild = arr1[flag-1]
  30.     else:
  31.         lchild = None
  32.     if len(arr2[flag+1:])>=1:
  33.         rchild = arr1[root-1]
  34.     else:
  35.         rchild = None
  36.         
  37.     tmp = dict()
  38.     tmp['root'] = arr1[root]
  39.     if tmp['root']!= None:
  40.         tmp['lchild'] = {'root':lchild,'lchild':None,'rchild':None} if lchild != None else None
  41.         tmp['rchild'] = {'root':rchild,'lchild':None,'rchild':None} if rchild != None else None
  42.     print('tree',tree)
  43.     print('step',step)
  44.     if tree==dict():
  45.         tree.update(tmp)
  46.     else:
  47.         set_nested_dict_value(tree, step, tmp)
  48.    
  49.     # print(tree)
  50.     # print(arr1[:flag],arr1[len_left:len_left+len_right])
  51.     # print(arr2[:flag],arr2[flag+1:])
  52.     # if len(arr1[:flag])>0 and len(arr2[:flag])>0:
  53.     #     createTree(arr1[:flag],arr2[:flag],tree,step+['lchild']) # 左子树
  54.     # if len(arr1[len_left:len_left+len_right])>0 and len(arr2[flag+1:])>0:
  55.     #     createTree(arr1[len_left:len_left+len_right],arr2[flag+1:],tree,step+['rchild']) #右子树
  56.     # if len(arr1[:flag])==0 and len(arr2[:flag])==0 and len(arr1[len_left:len_left+len_right])==0 and len(arr2[flag+1:])==0:
  57.     #     print('chu')
  58.     #     print(tree)
  59.     left_mid = arr2[:flag]
  60.     right_mid = arr2[flag+1:]
  61.     left_back = arr1[:flag]
  62.     right_back = arr1[len_left:len_left+len_right]
  63.    
  64.     print(tree)
  65.     print(left_back,right_back)
  66.     print(left_mid,right_mid)
  67.     if len(left_back)>0 and len(left_mid)>0:
  68.         createTree(left_back,left_mid,tree,step+['lchild']) # 左子树
  69.     if len(right_back)>0 and len(right_mid)>0:
  70.         createTree(right_back,right_mid,tree,step+['rchild']) #右子树
  71.     if len(left_back)==0 and len(left_mid)==0 and len(right_back)==0 and len(right_mid)==0:
  72.         print('chu')
  73.         print(tree)
  74. tree = dict()
  75. back = [3,4,2,6,5,1]
  76. mid = [3,2,4,1,6,5]
  77. createTree(back,mid,tree,[])
  78. tree
复制代码
简化
  1. def set_nested_dict_value(d, keys, value):
  2.     """
  3.     根据键列表设置嵌套字典的值
  4.     :param d: 原始字典
  5.     :param keys: 键列表
  6.     :param value: 要设置的值
  7.     """
  8.     # 从第一个键开始,逐层深入
  9.     for key in keys[:-1]:
  10.         # 如果当前键不存在,则创建一个空字典
  11.         if key not in d:
  12.             d[key] = {}
  13.         # 下一层字典
  14.         d = d[key]
  15.     # 设置最后一个键的值
  16.     d[keys[-1]] = value
  17. def createTree(arr1,arr2,tree,step):
  18.     root = len(arr1)-1
  19.     flag = arr2.index(arr1[root])
  20.     len_right = len(arr2)-flag-1
  21.     len_left = flag
  22.     tmp = {'root':arr1[root],'lchild':None,'rchild':None}
  23.     if tree==dict():
  24.         tree.update(tmp)
  25.     else:
  26.         set_nested_dict_value(tree, step, tmp)
  27.    
  28.     left_mid = arr2[:flag]
  29.     right_mid = arr2[flag+1:]
  30.     left_back = arr1[:flag]
  31.     right_back = arr1[len_left:len_left+len_right]
  32.    
  33.     if len(left_back)>0 and len(left_mid)>0:
  34.         createTree(left_back,left_mid,tree,step+['lchild']) # 左子树
  35.     if len(right_back)>0 and len(right_mid)>0:
  36.         createTree(right_back,right_mid,tree,step+['rchild']) #右子树
  37. tree = dict()
  38. back = [3,4,2,6,5,1]
  39. mid = [3,2,4,1,6,5]
  40. createTree(back,mid,tree,[])
  41. tree
复制代码
  1. {
  2.         'root': 1,
  3.         'lchild': {
  4.                 'root': 2,
  5.                  'lchild': {'root': 3, 'lchild': None, 'rchild': None},
  6.                  'rchild': {'root': 4, 'lchild': None, 'rchild': None}},
  7.         'rchild': {
  8.                 'root': 5,
  9.                 'lchild': {'root': 6, 'lchild': None, 'rchild': None},
  10.                 'rchild': None}
  11.   }
复制代码
步调二:先序遍历

  1. def preOrder(tree):
  2.    
  3.     print(tree['root'])
  4.     if tree['lchild']:
  5.         preOrder(tree['lchild'])
  6.     if tree['rchild']:
  7.         preOrder(tree['rchild'])
  8. preOrder(tree)
复制代码
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

花瓣小跑

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表