快速排序&堆排序&二叉树遍历(pre/in/post)
快速排序:
1 def quick_sort(lists, left, right): 2 # 快速排序 3 if left >= right: 4 return lists 5 key = lists[left] 6 low = left 7 high = right 8 while left < right: 9 while left < right and lists[right] >= key: 10 right -= 1 11 lists[left] = lists[right] 12 while left < right and lists[left] <= key: 13 left += 1 14 lists[right] = lists[left] 15 lists[right] = key 16 quick_sort(lists, low, left - 1) 17 quick_sort(lists, left + 1, high) 18 return lists
快速排序的基本思路:
1、切分
2、递归
切分的重点在于精准定位关键字key,我们一般将列表(或切分子列表)的首位list[first]作为key,然后从自右往左扫描(规则是比key大的放在左边,比key小的,放在右边),一旦发现有list[n]<key,执行list[first],list[n]=list[n],list[first],即调换两者的值,并执行自右往左扫描操作。如此循环。
由上可得,当左index与右index相遇时,即key值左边均为<key,右边均为>key,便可执行递归操作,调用自身sort(0,key_index-1),sort(key_index+1,len(list)-1),操作完成,返回list
堆排序:
1 array = [9,3,24,54,74,8,2,234,24,65,7,34,3,34,100,1000,57,56] 2 3 for i in range(len(array)-1,0,-1): 4 for j in range(int(i/2),0,-1): 5 father_tree_index = j 6 7 left_array_index = father_tree_index * 2 - 1 8 right_array_index = father_tree_index * 2 9 10 left_array = array[left_array_index] 11 father_array = array[father_tree_index - 1] 12 13 if father_array < left_array: 14 array[father_tree_index - 1],array[left_array_index] = array[left_array_index],array[father_tree_index - 1] 15 father_array = array[father_tree_index - 1] 16 17 if right_array_index < len(array[0:i + 1]): 18 right_array = array[right_array_index] 19 if father_array < right_array: 20 array[father_tree_index - 1],array[right_array_index] = array[right_array_index],array[father_tree_index - 1] 21 else: 22 pass 23 array[0],array[i] = array[i],array[0] 24 25 print(array)
堆排序基本思路:
利用list_index与抽象出来的完全二叉树中每一个father_tree_index以及fathertree的left_tree_index、right_tree_index之间的关系,循环对每一个father_tree进行处理,类似于selectsort,其中要注意,抽象出来的tree的index要与实参list的index区分开来,否则会提示list out of range
二叉树遍历:
1 class TreeNode(object): 2 def __init__(self,data=0,left=0,right=0): 3 self.data = data 4 self.left = left 5 self.right = right 6 class Btree(object): 7 def __init__(self,root=0): 8 self.root = root 9 #前序遍历,根,左,右 10 def preOrder(self,treenode): 11 if treenode is 0: 12 return 13 else: 14 print(treenode.data) 15 self.preOrder(treenode.left) 16 self.preOrder(treenode.right) 17 #中序遍历,左,根,右 18 def inOrder(self,treenode): 19 if treenode is 0: 20 return 21 self.inOrder(treenode.left) 22 print(treenode.data) 23 self.inOrder(treenode.right) 24 #后序遍历,左,右,根 25 def postOrder(self,treenode): 26 if treenode is 0: 27 return 28 self.postOrder(treenode.left) 29 self.postOrder(treenode.right) 30 print(treenode.data) 31 if __name__ == "__main__": 32 n1 = TreeNode(8) 33 n2 = TreeNode(4,n1,) 34 n3 = TreeNode(11) 35 n4 = TreeNode(12) 36 n5 = TreeNode(13) 37 n6 = TreeNode(7) 38 n7 = TreeNode(9,n3,n4) 39 n8 = TreeNode(10,n5,) 40 n9 = TreeNode(5,n7,) 41 n10 = TreeNode(6,0,n8) 42 n11 = TreeNode(2,n2,n9) 43 n12 = TreeNode(3,n10,n6) 44 root = TreeNode('root',n11,n12) 45 46 #实例化Btree 47 btree = Btree(root) 48 49 print('preOrder'.center(50,'-')) 50 btree.preOrder(btree.root) 51 52 print('inOrder'.center(50,'-')) 53 btree.inOrder(btree.root) 54 55 print('postOrder'.center(50,'-')) 56 btree.postOrder(btree.root)
二叉树遍历基本思路:
1、建树
2、preOrder前序遍历:根,左,右;inOrder中序遍历:左,根,右;postOrder后序遍历:左,右,根
浙公网安备 33010602011771号