(一)數據結構綜述node
(二)鏈表python
1 class Node(object): 2 def __init__(self,element=None): 3 self.element = element 4 self.next = None 5 6 # 這種建立鏈表的方法是很是野雞的,這裏主要是爲了演示 7 head = Node() 8 n1 = Node(111) 9 n2 = Node(222) 10 n3 = Node(333) 11 head.next = n1 12 n1.next = n2 13 n2.next = n3 14 15 def log_link_list(head): 16 """ 17 打印鏈表中全部的元素 18 """ 19 n = head.next 20 while n.next is not None: 21 print(n.element) 22 n = n.next 23 print(n.element) 24 25 26 def append(head,element): 27 """ 28 在鏈表的末尾添加元素 29 """ 30 n = head 31 while n.next is not None: 32 n = n.next 33 node = Node() 34 n.next = node 35 node.element = element 36 37 38 def preappend(head,element): 39 """ 40 鏈表的頭插法 41 """ 42 node = Node(element) 43 node.next = head.next 44 head.next = node 45 46 47 def pop(head): 48 tail = head 49 while tail.next is not None: 50 tail = tail.next 51 # 拿到最後一個元素的 pointer 了 52 n = head 53 while n.next is not tail: 54 n = n.next 55 n.next = None 56 return tail.element 57 58 preappend(head,999) 59 append(head,666) 60 log_link_list(head) 61 62 pop(head) 63 log_link_list(head)
(三) 二叉樹web
class Tree(object): def __init__(self,element=None): self.element = element self.left = None self.right = None def traversal(self): """ 二叉樹的前序,中序,後續遍歷 改變 print 的位置就能夠 """ if self.left is not None: self.left.traversal() if self.right is not None: self.right.traversal() print(self.element) def reverse(self): """ 交換左右子樹 """ self.left, self.right = self.right, self.left if self.left is not None: self.left.reverse() if self.right is not None: self.right.reverse() def test(): # 手動構建二叉樹,這種方法很是野雞。就只有在學習的時候用 # 在實際的應用中,通常都是自動生成的 t = Tree(0) left = Tree(1) right = Tree(2) t.left = left t.right = right t.traversal() if __name__ == '__main__': test()
(四)字典,又叫哈希表,Map等數組
如: ‘dog’ ------> 'd'*1 + 'o'*10 + 'g'*100數據結構
2. data = [key, value] 咱們插入的數據是一個個列表,採用連接地址法app
class HashTable(object): def __init__(self): self.table_size = 10007 self.table = [0] * self.table_size # 這個魔法方法是用來實現 in not in 語法的 def __contains__(self, item): return self.has_key(item) def has_key(self, key): """ 檢查一個 key 是否存在, 時間很短, 是 O(1) 若是用 list 來存儲, 須要遍歷, 時間是 O(n) """ index = self._index(key) # 取元素 v = self.table[index] if isinstance(v, list): # 檢查是否包含咱們要找的 key for kv in v: if kv[0] == key: return True return False def _insert_at_index(self, index, key, value): # 檢查下標處是不是第一次插入數據 v = self.table[index] data = [key, value] if isinstance(v, int): self.table[index] = [data] else: # 若是不是, 獲得的會是 list, 直接 append self.table[index].append([data]) def add(self, key, value): """ add 函數往 hashtable 中加入一對元素 咱們先只支持字符串當 key """ # 先計算出下標 index = self._index(key) # 在下標處插入元素 self._insert_at_index(index, key, value) def get(self, key, default_value=None): """ 這個和 dict 的 get 函數同樣 """ index = self._index(key) # 取元素 v = self.table[index] if isinstance(v, list): # 檢查是否包含咱們要找的 key for kv in v: if kv[0] == key: return kv[1] return default_value def _index(self, key): # 先計算出下標 return self._hash(key) % self.table_size def _hash(self, s): n = 1 f = 1 for i in s: n += ord(i) * f f *= 10 return n def test(): import uuid names = [ 'web', 'python', ] ht = HashTable() for key in names: value = uuid.uuid4() ht.add(key, value) print('add 元素', key, value) for key in names: v = ht.get(key) print('get 元素', key, v) if __name__ == '__main__': test()