#include "hafuman.h" #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream>
template <typename T> HAFUNode_t<T> CreatSubTree(T Left, T Right, T Root) { HAFUTree<T> *pH = (HAFUNode_t<T>)malloc(sizeof(struct HAFUTree<T>)); pH->data = Root;
pH->left = (HAFUNode_t<T>)malloc(sizeof(struct HAFUTree<T>)); pH->left->data = Left; pH->left->left = nullptr; pH->left->right = nullptr;
pH->right = (HAFUNode_t<T>)malloc(sizeof(struct HAFUTree<T>)); pH->right->data = Right; pH->right->left = nullptr; pH->right->right = nullptr; return pH; }
template <typename T> static HAFUNode_t<T> LeftJoin(HAFUNode_t<T> LEFT, HAFUNode_t<T> ROOT) { free(ROOT->left); ROOT->left = LEFT; return ROOT; }
template <typename T> static HAFUNode_t<T> RightJoin(HAFUNode_t<T> LEFT, HAFUNode_t<T> ROOT) { free(ROOT->right); ROOT->right = LEFT; return ROOT; }
template <typename T> void AutoJoin(HAFUNode_t<T> NEW, std::queue<HAFUNode_t<T>> &Q) { HAFUNode_t<T> temp = nullptr; if (Q.empty()) { Q.push(NEW); } else { if (Q.front()->data == NEW->left->data) { temp = LeftJoin(Q.front(), NEW); Q.pop(); Q.push(temp); } else if (Q.front()->data == NEW->right->data) { temp = RightJoin(Q.front(), NEW); Q.pop(); Q.push(temp); } else { Q.push(NEW); } } }
template <typename T> void FinalJoin(HAFUNode_t<T> ROOT, std::queue<HAFUNode_t<T>> &Q) { if (Q.front()->data == ROOT->left->data) { LeftJoin(Q.front(), ROOT); Q.pop(); } else if (Q.front()->data == ROOT->right->data) { RightJoin(Q.front(), ROOT); Q.pop(); } else { printf("insert error!\r\n"); } }
template <typename T> static int FindTreeHeight(HAFUNode_t<T> BT) { int rightlen, leftlen, maxlen; if (BT) { rightlen = FindTreeHeight(BT->right); leftlen = FindTreeHeight(BT->left); maxlen = leftlen > rightlen ? leftlen : rightlen; return maxlen + 1; } else { return 0; } }
template <typename T> void DrawTree(HAFUNode_t<T> BT) { int height = FindTreeHeight(BT); int cnt = 0;
std::queue<HAFUNode_t<T>> CurLevelqueue; std::queue<HAFUNode_t<T>> NextLevelqueue; std::queue<HAFUNode_t<T>> CurTempqueue; std::queue<HAFUNode_t<T>> NextTempqueue; HAFUNode_t<T> temp, lasttemp = NULL; int width = 0; CurLevelqueue.push(BT);
for (int i = 0; i < height; i++) { cnt = (int)pow(2, i); width = pow(2, height - i + 1); CurTempqueue = CurLevelqueue; while (cnt--) { temp = CurLevelqueue.front(); if (temp != NULL) { printf("%*d%*c", width, temp->data, width, ' '); NextLevelqueue.push(temp->left); NextLevelqueue.push(temp->right); } else { printf("%*c%*c", width, ' ', width, ' '); NextLevelqueue.push(nullptr); NextLevelqueue.push(nullptr); } CurLevelqueue.pop(); } printf("\n"); NextTempqueue = NextLevelqueue; if (i != height - 1) { int nextwidth = (int)pow(2, height - i); for (int k = 0; k < (int)pow(2, i + 1); k++) { if ((k % 2) == 0) { if (NextTempqueue.front() != nullptr) { printf("%*c", nextwidth, '-'); for (uint8_t z = 0; z < nextwidth; z++) { printf("-"); } } else { printf("%*c", nextwidth, ' '); for (uint8_t z = 0; z < nextwidth; z++) { printf(" "); } } NextTempqueue.pop(); } else { if (NextTempqueue.front() != nullptr) { for (uint8_t z = 0; z < nextwidth; z++) { printf("-"); } printf("%*c", nextwidth, ' '); } else { for (uint8_t z = 0; z < nextwidth; z++) { printf(" "); } printf("%*c", nextwidth, ' '); } NextTempqueue.pop(); } } printf("\n"); } printf("\r"); swap(CurLevelqueue, NextLevelqueue); } }
|