# 二叉树
class Tree(object):
def __init__(self, element=None):
self.element = element
self.left = None
self.right = None
def traversal(self):
"""
树的遍历, 是一个递归操作
"""
print(self.element)
if self.left is not None:
self.left.traversal()
if self.right is not None:
self.right.traversal()
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()
# hash表
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 v == 0:
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):
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
# 链表
class Node(object):
def __init__(self, element=-1):
self.element = element
self.next = None
class LinkedList(object):
def __init__(self):
self.head = None
def is_empty(self):
return self.head is None
def length(self):
index = 0
node = self.head
while node is not None:
index += 1
node = node.next
return index
def find(self, element):
node = self.head
while node is not None:
if node.element == element:
break
node = node.next
return node
def _node_at_index(self, index):
i = 0
node = self.head
while node is not None:
if i == index:
return node
node = node.next
i += 1
return None
def element_at_index(self, index):
node = self._node_at_index(index)
return node.element
# 队列
class Node():
def __init__(self, element=None, next=None):
self.element = element
self.next = next
def __repr__(self):
return str(self.element)
class Queue():
def __init__(self):
self.head = Node()
self.tail = self.head
def empty(self):
return self.head.next is None
def enqueue(self, element):
n = Node(element)
self.tail.next = n
self.tail = n
def dequeue(self):
node = self.head.next
if not self.empty():
self.head.next = node.next
return node
# 栈
class Node():
def __init__(self, element=None, next=None):
self.element = element
self.next = next
def __repr__(self):
return str(self.element)
class Stack():
def __init__(self):
self.head = Node()
def is_empty(self):
return self.head.next is None
def push(self, element):
self.head.next = Node(element, self.head.next)
# 取出head.next指向的元素,如果栈不是空的,就让head.next指向node.next,这样node就不在栈中了
def pop(self):
node = self.head.next
if not self.is_empty():
self.head.next = node.next
return node
# head.next就是栈里面第一个元素
def top(self):
return self.head.next
利用python实现常见的数据结构
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Ios开发听起来非常高大上, 有不少iOS 开发者从别的语言自学转过来,也有不少人想跨行试水ios开发,那么,iO...