diff --git a/place.c b/place.c index 19ba3365e0ab182d8784c15bc48a24ea013f7ba2..8b65c6eb516c23df4e084f4a8ddd68208d5efe68 100644 --- a/place.c +++ b/place.c @@ -1,3 +1,21 @@ +/** + * @file place.c + * @brief Implements the node placement logic for the TreeMap visualization. + * + * This file contains functions to create nodes, build the tree structure, place the nodes, + * and display the assigned bounding boxes for each node. + */ + + +/** + * @brief Creates a new TreeNode instance. + * + * Allocates memory for a new TreeNode and initializes its values. If data is provided, + * it sets the node sum to the value contained in the data. + * + * @param data Pointer to the data to be associated with the new node. + * @return TreeNode* Pointer to the newly created node. + */ TreeNode *create_node(Data *data) { TreeNode *node = malloc(sizeof(TreeNode)); node->sum = data ? data->number : 0.0; @@ -7,6 +25,18 @@ TreeNode *create_node(Data *data) { return node; } +/** + * @brief Builds a binary tree from an array of data elements. + * + * This function takes an array of data and recursively divides it to create a binary tree. + * Internal nodes have a sum of the values of their child nodes. + * + * @param array Pointer to the data array. + * @param start The starting index for the current segment. + * @param end The ending index for the current segment. + * @return TreeNode* Pointer to the root node of the created subtree. + */ + TreeNode *build_tree(Data *array, int start, int end) { if (start > end) return NULL; @@ -40,6 +70,16 @@ TreeNode *build_tree(Data *array, int start, int end) { return node; } +/** + * @brief Displays the tree structure with indentation to indicate depth. + * + * This function recursively traverses the tree and prints each node's details, + * including its sum and, if applicable, its data. + * + * @param node Pointer to the root of the subtree to display. + * @param depth The current depth for indentation. + */ + void display_tree(TreeNode *node, int depth) { if (node == NULL) return; @@ -61,6 +101,15 @@ void display_tree(TreeNode *node, int depth) { display_tree(node->children[i], depth + 1); } } +/** + * @brief Frees the memory allocated for a tree. + * + * This function recursively frees all nodes in a given tree, including + * any dynamically allocated child nodes. + * + * @param node Pointer to the root of the subtree to free. + */ + void free_tree(TreeNode *node) { if (node == NULL) return; @@ -74,6 +123,19 @@ void free_tree(TreeNode *node) { free(node); } + +/** + * @brief Places nodes in a treemap visualization by assigning bounding boxes. + * + * This function assigns bounding boxes to nodes in the tree based on the given direction sequence. + * Each child node is given a proportion of the parent's bounding box. + * + * @param node Pointer to the root of the subtree to place. + * @param directions A string containing direction instructions (e.g., "WNES"). + * @param depth The current depth in the tree (used for determining the direction). + */ + + void place(TreeNode *node, const char *directions, int depth) { if (!node || node->num_children == 0) return; @@ -124,6 +186,15 @@ void place(TreeNode *node, const char *directions, int depth) { } } +/** + * @brief Displays the bounding boxes of all nodes in the tree. + * + * This function traverses the tree and prints out the details of each node's assigned bounding box. + * + * @param node Pointer to the root of the subtree to display. + * @param depth The current depth for indentation. + */ + void display_boxes(TreeNode *node, int depth) { if (!node) return;