2023-01-11 00:48:48 +08:00
|
|
|
/**
|
|
|
|
* File: print_util.h
|
|
|
|
* Created Time: 2022-12-21
|
2023-10-27 01:04:17 +08:00
|
|
|
* Author: MolDum (moldum@163.com), Reanon (793584285@qq.com)
|
2023-01-11 00:48:48 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef PRINT_UTIL_H
|
|
|
|
#define PRINT_UTIL_H
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2023-04-17 21:13:15 +08:00
|
|
|
#include <string.h>
|
2023-01-11 00:48:48 +08:00
|
|
|
|
|
|
|
#include "list_node.h"
|
|
|
|
#include "tree_node.h"
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2023-04-17 22:15:06 +08:00
|
|
|
/* Print an Array */
|
2023-11-17 00:29:54 +08:00
|
|
|
void printArray(int arr[], int size) {
|
|
|
|
if (arr == NULL || size == 0) {
|
|
|
|
printf("[]");
|
|
|
|
return;
|
|
|
|
}
|
2023-01-11 00:48:48 +08:00
|
|
|
printf("[");
|
2023-11-17 00:29:54 +08:00
|
|
|
for (int i = 0; i < size - 1; i++) {
|
|
|
|
printf("%d, ", arr[i]);
|
2023-01-11 00:48:48 +08:00
|
|
|
}
|
2023-11-17 00:29:54 +08:00
|
|
|
printf("%d]\n", arr[size - 1]);
|
2023-01-11 00:48:48 +08:00
|
|
|
}
|
|
|
|
|
2023-06-02 02:51:35 +08:00
|
|
|
/* Print an Array */
|
2023-11-17 00:29:54 +08:00
|
|
|
void printArrayFloat(float arr[], int size) {
|
|
|
|
if (arr == NULL || size == 0) {
|
|
|
|
printf("[]");
|
|
|
|
return;
|
|
|
|
}
|
2023-06-02 02:51:35 +08:00
|
|
|
printf("[");
|
2023-11-17 00:29:54 +08:00
|
|
|
for (int i = 0; i < size - 1; i++) {
|
|
|
|
printf("%.2f, ", arr[i]);
|
2023-06-02 02:51:35 +08:00
|
|
|
}
|
2023-11-17 00:29:54 +08:00
|
|
|
printf("%.2f]\n", arr[size - 1]);
|
2023-06-02 02:51:35 +08:00
|
|
|
}
|
|
|
|
|
2023-04-17 22:15:06 +08:00
|
|
|
/* Print a linked list */
|
2023-11-17 00:29:54 +08:00
|
|
|
void printLinkedList(ListNode *node) {
|
2023-01-11 00:48:48 +08:00
|
|
|
if (node == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (node->next != NULL) {
|
|
|
|
printf("%d -> ", node->val);
|
|
|
|
node = node->next;
|
|
|
|
}
|
|
|
|
printf("%d\n", node->val);
|
|
|
|
}
|
|
|
|
|
2023-10-18 02:16:26 +08:00
|
|
|
typedef struct Trunk {
|
2023-01-11 00:48:48 +08:00
|
|
|
struct Trunk *prev;
|
|
|
|
char *str;
|
2023-10-18 02:16:26 +08:00
|
|
|
} Trunk;
|
2023-01-11 00:48:48 +08:00
|
|
|
|
|
|
|
Trunk *newTrunk(Trunk *prev, char *str) {
|
2023-04-17 21:13:15 +08:00
|
|
|
Trunk *trunk = (Trunk *)malloc(sizeof(Trunk));
|
2023-01-11 00:48:48 +08:00
|
|
|
trunk->prev = prev;
|
2023-04-17 21:13:15 +08:00
|
|
|
trunk->str = (char *)malloc(sizeof(char) * 10);
|
2023-01-11 00:48:48 +08:00
|
|
|
strcpy(trunk->str, str);
|
|
|
|
return trunk;
|
|
|
|
}
|
|
|
|
|
2023-04-17 22:15:06 +08:00
|
|
|
/* Helper function to print branches of the binary tree */
|
2023-01-11 00:48:48 +08:00
|
|
|
void showTrunks(Trunk *trunk) {
|
|
|
|
if (trunk == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
showTrunks(trunk->prev);
|
|
|
|
printf("%s", trunk->str);
|
|
|
|
}
|
|
|
|
|
2023-04-17 22:15:06 +08:00
|
|
|
/* Help to print a binary tree, hide more details */
|
2023-11-17 00:29:54 +08:00
|
|
|
void printTreeHelper(TreeNode *node, Trunk *prev, bool isRight) {
|
2023-01-11 00:48:48 +08:00
|
|
|
if (node == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
char *prev_str = " ";
|
|
|
|
Trunk *trunk = newTrunk(prev, prev_str);
|
|
|
|
printTreeHelper(node->right, trunk, true);
|
|
|
|
if (prev == NULL) {
|
|
|
|
trunk->str = "———";
|
2023-11-01 05:14:22 +08:00
|
|
|
} else if (isRight) {
|
2023-01-11 00:48:48 +08:00
|
|
|
trunk->str = "/———";
|
|
|
|
prev_str = " |";
|
|
|
|
} else {
|
|
|
|
trunk->str = "\\———";
|
|
|
|
prev->str = prev_str;
|
|
|
|
}
|
|
|
|
showTrunks(trunk);
|
|
|
|
printf("%d\n", node->val);
|
|
|
|
|
|
|
|
if (prev != NULL) {
|
|
|
|
prev->str = prev_str;
|
|
|
|
}
|
|
|
|
trunk->str = " |";
|
|
|
|
|
|
|
|
printTreeHelper(node->left, trunk, false);
|
|
|
|
}
|
|
|
|
|
2023-04-17 22:15:06 +08:00
|
|
|
/* Print a binary tree */
|
2023-11-17 00:29:54 +08:00
|
|
|
void printTree(TreeNode *root) {
|
2023-01-11 00:48:48 +08:00
|
|
|
printTreeHelper(root, NULL, false);
|
|
|
|
}
|
|
|
|
|
2023-04-17 22:15:06 +08:00
|
|
|
/* Print a Heap */
|
2023-11-17 00:29:54 +08:00
|
|
|
void printHeap(int arr[], int size) {
|
2023-03-30 02:00:17 +08:00
|
|
|
TreeNode *root;
|
2023-01-15 22:54:34 +08:00
|
|
|
printf("堆的数组表示:");
|
|
|
|
printArray(arr, size);
|
|
|
|
printf("堆的树状表示:\n");
|
2023-10-27 01:04:17 +08:00
|
|
|
root = arrayToTree(arr, size);
|
2023-01-15 22:54:34 +08:00
|
|
|
printTree(root);
|
|
|
|
}
|
|
|
|
|
2023-01-11 00:48:48 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif // PRINT_UTIL_H
|