Tìm hiểu về cấu trúc cây (tree)
Tree gom nhieu nut va moi nut gom nhieu nut con
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class Node:
def __init__(self, data):
self.children = []
self.data = data
def find_node(root, target_data):
if root is None:
return None
if root.data == target_data:
return root
for child in root.children:
result = find_node(child, target_data)
if result:
return result
return None
def find_path(root, target, path):
if root is None:
return False
path.append(root)
if root.data == target.data:
return True
for child in root.children:
if find_path(child, target, path):
return True
path.pop()
return False
def find_lca(root, node1, node2):
path_to_node1 = []
path_to_node2 = []
if not (find_path(root, node1, path_to_node1) and find_path(root, node2, path_to_node2)):
return None
i = 0
while i < len(path_to_node1) and i < len(path_to_node2):
if path_to_node1[i].data != path_to_node2[i].data:
break
i += 1
return path_to_node1[i - 1]
# Example usage
# Build the tree:
# 1
# /|\
# 2 3 4
# / \
# 5 6
root = Node(1)
root.children = [Node(2), Node(3), Node(4)]
root.children[0].children = [Node(5), Node(6)]
node1 = root.children[2] # Node 2
node2 = root.children[0].children[0] # Node 4
lca = find_lca(root, node1, node2)
if lca:
print("LCA of", node1.data, "and", node2.data, "is:", lca.data)
else:
print("LCA not found")
Tao cay nhi phan
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class TreeNode:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def insert(root, val):
if root is None:
return TreeNode(val)
if val < root.val:
root.left = insert(root.left, val)
else:
root.right = insert(root.right, val)
return root
def preorder_traversal(root):
if root:
print(root.val, end=" ")
preorder_traversal(root.left)
preorder_traversal(root.right)
# Example usage
num_array = [5, 2, 8, 1, 3, 6, 9]
root = None
for num in num_array:
root = insert(root, num)
print("Pre-order traversal:")
preorder_traversal(root)
'''
5
2 8
1 3 6 9
'''
Co ban ve segment tree
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
a = [0, 1, 3, 5, 2, 4, 6, 8, 7]
n = 8
t = [0] * 100
def build(id, l, r):
if l == r:
t[id] = a[l]
print(f"{id}, {l}, {r}, {t[id]}")
return
mid = (l + r) // 2
build(id * 2, l, mid)
build(id * 2 + 1, mid + 1, r)
t[id] = t[id * 2] + t[id * 2 + 1]
print(f"{id}, {l}, {r}, {t[id]}")
def get(id, l, r, u, v):
if r < u or v < l:
return 0
if u <= l and r <= v:
print(f"->{id}, {l}, {r}, {t[id]}")
return t[id]
mid = (l + r) // 2
t1 = get(id * 2, l, mid, u, v)
t2 = get(id * 2 + 1, mid + 1, r, u, v)
print(f"{id}, {l}, {r}, {t1 + t2}")
return t1 + t2
def update(id, l, r, pos, val):
if pos < l or r < pos:
return
if l == r:
t[id] = val
a[l] = val
return
mid = (l + r) // 2
update(id * 2, l, mid, pos, val)
update(id * 2 + 1, mid + 1, r, pos, val)
t[id] = t[id * 2] + t[id * 2 + 1]
build(1, 1, n)
print("====")
print(get(1, 1, n, 2, 5))
print("======")
update(1, 1, n, 4, 10)
print(get(1, 1, n, 2, 5))
Build function
Get function
Update function
Tài liệu tham khảo
Internet
Hết.