data structure 总结
data structure 总结
a=5
b=3
print(a + b) # Addition: 8
print(a - b) # Subtraction: 2
print(a * b) # Multiplication: 15
print(a / b) # Division: 1.6666666666666667
print(a // b) # Floor Division: 1 5/3 = 1.666, floor division use the nearest int not larger
1.66 -> 1
print(a % b) # Modulus: 2 5÷3=1 with a remainder of 2
print(a ** b) # Exponentiation: 125
Bitwise operation:
2. Bitwise OR (|)
● Operation: Each bit of the output is 1 if the corresponding bit of either operand is
1. If both are 0, the result is 0.
Example:
a = 12 # In binary: 1100
b = 10 # In binary: 1010
● print(result) # Output: 14
XOR stands for "Exclusive OR." It is a bitwise operation that compares two bits and
returns 1 if the bits are different and 0 if they are the same.
● print(result) # Output: 6
● Operation: This operation inverts all the bits of the operand. Each 1 becomes 0,
and each 0 becomes 1. It’s a unary operator (operates on a single operand).
● Note: In Python, the result of ~x is -x-1 because Python integers are signed.
Example:
python
Copy code
a = 12 # In binary: 00000000 00000000 00000000 00001100
● Operation: Shifts the bits of the number to the left by a specified number of
positions. Each left shift multiplies the number by 2.
Example:
python
Copy code
a = 5 # In binary: 0000 0101
● Operation: Shifts the bits of the number to the right by a specified number of
positions. Each right shift divides the number by 2 (integer division).
Example:
python
Copy code
a = 20 # In binary: 0001 0100
● print(result) # Output: 5
Mask in python:
mask = 0b00000101 # Set the 3rd and 1st bits from the right
Parity of a word:
1101101001111001011010110101010111100101011010111101101010101010
If count %% 2 == 1 return 1
== 0 return 0
Heap
import heapq
# Create a heap
heap = []
heapq.heapify(heap) # O(n)
# Add elements
heapq.heappush(heap, 3) # O(log n)
heapq.heappush(heap, 1)
heapq.heappush(heap, 4)
import heapq
heap = [3, 1, 4, 1, 5, 9, 2, 6]
heapq.heapify(heap)
# Creating a tuple
# Time complexity: O(n), where n is the number of elements
my_tuple = (1, 2, 3, 4, 5)
# Slicing a tuple
# Time complexity: O(k), where k is the size of the slice
slice = my_tuple[1:4] # Get elements from index 1 to 3
# Concatenation
# Time complexity: O(n + m), where n and m are the lengths of the tuples being
concatenated
new_tuple = my_tuple + (6, 7, 8)
# Length of a tuple
# Time complexity: O(1)
length = len(my_tuple)
# Checking membership
# Time complexity: O(n) - requires linear search
is_present = 3 in my_tuple
# Tuple unpacking
# Time complexity: O(n), where n is the number of elements being unpacked
a, b, c = (1, 2, 3)
# Iteration
# Time complexity: O(n), where n is the number of elements in the tuple
for item in my_tuple:
print(item)
# Comparing tuples
# Time complexity: O(n), where n is the number of elements to compare
comparison_result = (1, 2, 3) < (1, 2, 4)
# Note: Tuples are immutable, so operations like append, remove, or in-place sort are
not available.
BST binary search trees
BST definition: any left subtree node value < current node < right subtree node value
No duplication
container in python? no
which python library
how it is implemented node
does it have iterator? no
immutable? No, most likely implemented mutable
add time complx/insert Average logN, worst N
get time complx/search Average logN, worst N
remove complx Average logN, worst N
Don’t need to sort, do in order traversal
To maintain logN, need to do self balanced
sort tree like RED BLACK Tree or AVL tree
k largest Average logN, worst N
No, use node for implementation
is element in stored in continuous block of linked structure of individually allocated
memory nodes
什么时候用
Efficient search, remove, insert, O(log n),
good for database indexing, search, quick
advantage file system access
1. Not self-balanced, if unbalanced
become O(N) end up being a linked
list
2. Need extra memory for node,
memory overload
3. Bad cache performance, node
disadvantage structure use uncontigous memory