class Node(): def __init__(self,item): self.item = item self.left = None self.right = None class Tree(): def __init__(self): self.root = None def addNode(self,item): node = Node(item) if self.root == None:#若是樹中沒有節點,把第一個的節點給root self.root = node return cur = self.root #不改變root節點.賦值 while 1 : q = [cur] # 將節點加到列表中,而後在pop出來,左右爲空,添加節點,不爲空,添加到列表中繼續判斷 # 要賦值,不然每次都要pop,pop太多值了,一次. # if q.pop(0).left == None :#若是左節點沒有 # q.pop(0).left = node #給左節點賦值 # return #退出函數 # else:.... n = pop(0) if n.left == None :#若是左節點沒有 n.left = node #給左節點賦值 return #退出函數 else: q.append(n.left)#若是左節點有,加到列表中判斷 if n.right == None : n.right = node return else: q.append(n.right)
廣度排序node
前序 (根左右)python
寫出一個來,而後套到別的樹算法
有序的深度排序數據結構
中序app
class SortTree(): def __init__(self): self.root = None def midTravel(self,root): if root = None return # 左根右 self.midTravel(root.left) # 遞歸, 循環直到return print(root.item) self.midTravel(root.right) def insertNode(self,item): node = Node(item) cur = self.root # 數爲空 if self.root == None: self.root = node return # 樹爲非空 while True: if cur.left > node.item : cur.left = node break else: cur = cur.left if cur.right > node.item : cur.right = node break else: cur = cur.right
單鏈表寫出以後,雙鏈表也能夠,循環列表函數
排序二叉樹code
寫出來,寫樹的高度,什麼均可以了.排序
深度排序:遞歸
中序有用, 也沒有排序, 自動排序it
low = 0 high = len() -1 mid = (low+high)//2 while low <= high: if items[mid]>item : low = ... if < : high = ... else : find = T return find # 基於有序 減半 速度
def search(alist,item): find = False low = 0 high = len(alist)-1 while low <= high: mid = (low+high) // 2 #中心位置的元素下標(切割點) if alist[mid] < item: low = mid + 1 elif alist[mid] > item: high = mid - 1 else:#alist[mid] == item find = True break return find alist = [1,2] print(search(alist,22))
#將原始列表中的最大值找出且放置在列表最右側(將元素兩兩比較,將數值大的數逐步向後移動) def sort(alist): for i in range(len(alist)-1): if alist[i] > alist[i+1]: alist[i],alist[i+1] = alist[i+1],alist[i] return alist
def sort(alist): for j in range(len(alist)-1): #交換 for i in range(len(alist)-1-j): if alist[i] > alist[i+1]: alist[i],alist[i+1] = alist[i+1],alist[i] return alist
alist = [3,6,1,4,8,2]
print(sort(alist))
----->[1, 2, 3, 4, 6, 8]
將最大值一次找出, 放在列表最右
#將列表中的最大值的下標找到 def sort(alist): max_index = 0 #最大值的下標 for i in range(1,len(alist)): if alist[max_index] < alist[i]: max_index = i print(max_index)
#將列表中的最大值一次找出,放置在列表最右側 def sort(alist): max_index = 0 #最大值的下標 for i in range(1,len(alist)): if alist[max_index] < alist[i]: max_index = i alist[max_index],alist[len(alist)-1] = alist[len(alist)-1],alist[max_index] return alist
def sort(alist): for j in range(len(alist),1,-1): max_index = 0 #最大值的下標 for i in range(1,j): #len(alist) == > j if alist[max_index] < alist[i]: max_index = i alist[max_index],alist[j-1] = alist[j-1],alist[max_index] return alist
alist = [3,6,1,4,8,2]
sort(alist)
---->[1, 2, 3, 4, 6, 8]
相比來講,交換的次數少了, 在第一循環內部, 第二循環外部
class SortTree(): def __init__(self): self.root = None def midTravel(self,root): if root == None: return #左根右 self.midTravel(root.left) print(root.item) self.midTravel(root.right) def insertNode(self,item): node = Node(item) cur = self.root #樹爲空 if self.root == None: self.root = node return #樹爲非空 while True: if cur.item > node.item: if cur.left == None: cur.left = node break else: cur = cur.left else: if cur.right == None: cur.right = node break else: cur = cur.right
t = SortTree() t.insertNode(3) t.insertNode(8) t.insertNode(3) t.insertNode(1) t.insertNode(1) t.midTravel(t.root)
---->
1
1
3
3
8
def sort(alist,start,end): low = start high = end #結束遞歸的條件 if low > high: return mid = alist[low] while low < high: while low < high: if alist[high] > mid: high -= 1 else: alist[low] = alist[high] break while low < high: if alist[low] < mid: low += 1 else: alist[high] = alist[low] break # if low == high: alist[low] = mid sort(alist,low+1,end) #將基準右側的子列表進行遞歸操做 sort(alist,start,high-1) return alist