@@ -44,46 +44,46 @@ bintrees提供了丰富的API,涵盖了通常的多种应用。下面逐条说
44
44
- 引用
45
45
46
46
如果按照一般模块的思路,输入下面的命令引入上述模块
47
-
47
+
48
48
>>> import bintrees
49
-
49
+
50
50
错了,这是错的,出现如下警告:(×××不可用,用×××)
51
51
52
52
Warning: FastBinaryTree not available, using Python version BinaryTree.
53
53
Warning: FastAVLTree not available, using Python version AVLTree.
54
54
Warning: FastRBTree not available, using Python version RBTree.
55
55
56
56
正确的引入方式是:
57
-
57
+
58
58
>>> from bintrees import BinaryTree #只引入了BinartTree
59
59
>>> from bintrees import * #三个模块都引入了
60
-
60
+
61
61
- 实例化
62
-
62
+
63
63
>>> btree = BinaryTree()
64
64
>>> btree
65
65
BinaryTree({})
66
66
>>> type(btree)
67
67
<class 'bintrees.bintree.BinaryTree'>
68
-
68
+
69
69
- 逐个增加键值对:.__ setitem__ (k,v) .复杂度O(log(n))(后续说明中,都会有复杂度标示,为了简单,直接标明:O(log(n)).)
70
-
70
+
71
71
>>> btree.__ setitem__ ("Tom","headmaster")
72
72
>>> btree
73
73
BinaryTree({'Tom': 'headmaster'})
74
74
>>> btree.__ setitem__ ("blog","http://blog.csdn.net/qiwsir ")
75
75
>>> btree
76
76
BinaryTree({'Tom': 'headmaster', 'blog': 'http://blog.csdn.net/qiwsir'} )
77
-
77
+
78
78
- 批量添加:.update(E) E是dict/iterable,将E批量更新入btree. O(E* log(n))
79
-
79
+
80
80
>>> adict = [ (2,"phone"),(5,"tea"),(9,"scree"),(7,"computer")]
81
81
>>> btree.update(adict)
82
82
>>> btree
83
83
BinaryTree({2: 'phone', 5: 'tea', 7: 'computer', 9: 'scree', 'Tom': 'headmaster', 'blog': 'http://blog.csdn.net/qiwsir'} )
84
-
84
+
85
85
- 查找某个key是否存在:.__ contains__ (k) 如果含有键k,则返回True,否则返回False. O(log(n))
86
-
86
+
87
87
>>> btree
88
88
BinaryTree({2: 'phone', 5: 'tea', 7: 'computer', 9: 'scree', 'Tom': 'headmaster', 'blog': 'http://blog.csdn.net/qiwsir'} )
89
89
>>> btree.__ contains__ (5)
@@ -94,9 +94,9 @@ bintrees提供了丰富的API,涵盖了通常的多种应用。下面逐条说
94
94
False
95
95
>>> btree.__ contains__ (1)
96
96
False
97
-
97
+
98
98
- 根据key删除某个key-value:.__ delitem__ (key), O(log(n))
99
-
99
+
100
100
>>> btree
101
101
BinaryTree({2: 'phone', 5: 'tea', 7: 'computer', 9: 'scree', 'Tom': 'headmaster', 'blog': 'http://blog.csdn.net/qiwsir'} )
102
102
>>> btree.__ delitem__ (5) #删除key=5的key-value,即:5:'tea' 被删除.
0 commit comments