-
Notifications
You must be signed in to change notification settings - Fork 0
iedy2024/Min-heap-based-binary-tree
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Description This project builds a binary tree by repeatedly combining the two smallest nodes (by priority) from a min-heap, similar to constructing a Huffman-like tree. Each leaf represents a “satellite” with: - priority (integer) - name (string without spaces, max 15 characters) Internal (link) nodes are created with: - priority = sum of the two combined nodes' priorities - name = concatenation of the lower-priority node's name followed by the other node's name After building the tree, the program solves four tasks: - -c1: print the tree level by level - -c2: decode bit strings (0/1) to leaf names - -c3: encode a list of leaf names into a single 0/1 string - -c4: compute the Lowest Common Ancestor (LCA) for a set of leaves Build - make build Produces the tema2 executable - make run Runs the executable (no useful default arguments for this project) - make clean Removes the executable Usage ./tema2 <flag> <input> <output> Where <flag> ∈ {-c1, -c2, -c3, -c4} Input file format (common section) - First line: N — number of satellites (leaves) - Next N lines: priority name Example: 5 3 A 2 B 1 C 4 D 5 E Task-specific formats 1) -c1 — Level-order printing - No additional section in the input. - Output: for each level i of the tree, print on one line: "priority-name " for all nodes on that level, left→right order. Exactly N lines are printed (N = initial number of leaves). 2) -c2 — Decode 0/1 strings into leaves - After the satellites list: K (number of codes), followed by K lines with codes made of 0 and 1. - Traversal rules: 0 = left, 1 = right. Only leaf names are emitted; if a leaf is reached before the code ends, emit the leaf and restart from the root to continue. - Output: for each code, the decoded leaf names separated by spaces; one output line per code. 3) -c3 — Encode leaves into a 0/1 string - After the satellites list: M (number of leaves to encode), followed by M lines with leaf names existing in the tree. - Rules: 0 = left, 1 = right. For each name, find the unique path to the leaf; append its code to the result string. - Output: a single line containing the concatenated 0/1 string. 4) -c4 — Lowest Common Ancestor (LCA) - After the satellites list: P (number of leaves), followed by P lines of leaf names. - Compute the LCA for the first two leaves, then iteratively update the LCA with each subsequent leaf. - Output: the name of the node (leaf or internal) that is the final LCA. Run examples - Build and print levels: make build ./tema2 -c1 input.txt output.txt - Decode: ./tema2 -c2 input.txt output.txt - Encode: ./tema2 -c3 input.txt output.txt - LCA: ./tema2 -c4 input.txt output.txt Notes and constraints - Satellite names: no spaces, max 15 characters (internal buffer 16 including null terminator) - Sizes: - The heap auto-resizes (starts with capacity 40) - Code buffers for -c2/-c3 use 1001 characters - Ties (equal priority) are broken lexicographically by node name to keep heap order stable - The program frees all dynamically allocated memory (Valgrind clean in local tests) Implementation details - Structures: - ItemType: { prior: int, data: char* } - TreeNode: binary tree node (value, left, right) - PriQueue (min-heap) of pointers to TreeNode, ordered by priority then lexicographically by name - Tree construction: - Repeatedly extract the two smallest nodes from the heap and create a parent with summed priority and concatenated name - Insert the new node back into the heap; the last remaining node is the root - Tasks: - -c1: level-order printing by iterating depths 0..N-1 (printLevel) - -c2: decoding with restart from root upon hitting a leaf mid-code - -c3: DFS search (findNode) to get each leaf's path and concatenate codes - -c4: recursive LCA (lowestCommonAncestor) Complexity - Tree construction: O(N log N) due to heap operations - -c1: O(N + number_of_internal_nodes) - -c2: O(total_code_length) - -c3: worst-case O(M · tree_height) - -c4: O(P · number_of_nodes) in the current recursive approach Files - Makefile — targets: build, run, clean; executable: tema2 - main.h — data structures (tree, heap) and helper functions - main.c — I/O, tree construction, implementations for -c1..-c4 Tips - For easier testing, create separate input files for each task and keep distinct outputs for comparison.
About
No description, website, or topics provided.
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published