-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompression_tree.py
62 lines (50 loc) · 1.85 KB
/
compression_tree.py
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
class Node:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
# Pre-order traversal with markers
def serialize_tree(node):
if node is None:
return "#"
return f"{node.value} {serialize_tree(node.left)} {serialize_tree(node.right)}"
# Helper to serialize and compress tree
def compress_tree(root):
serialized_tree = serialize_tree(root)
# Split serialized tree into nodes and markers
elements = serialized_tree.split()
node_values = []
structure = []
# Build node values list and structure bit list
for el in elements:
if el == "#":
structure.append(0) # Null child
else:
node_values.append(el)
structure.append(1) # Real node
# Compress node values using Huffman encoding (or another method)
compressed_values = huffman_encode(node_values)
# Compress structure using RLE or other techniques
compressed_structure = rle_compress(structure)
return compressed_values, compressed_structure
# Example Huffman encoding and RLE compression functions
def huffman_encode(values):
# Placeholder: Implement Huffman coding
pass
def rle_compress(structure):
# Simple RLE compression: Count consecutive bits
compressed = []
count = 1
for i in range(1, len(structure)):
if structure[i] == structure[i - 1]:
count += 1
else:
compressed.append((structure[i - 1], count))
count = 1
compressed.append((structure[-1], count))
return compressed
# Example tree construction
root = Node("A", Node("B", Node("D"), Node("E")), Node("C"))
compressed_values, compressed_structure = compress_tree(root)
print("Compressed Node Values:", compressed_values)
print("Compressed Structure:", compressed_structure)