#include "bantree.h" #include <cmath> #include <iostream> #include <queue> #include <stack> #include <string> using namespace std;
AvlTree MakeEmpty(AvlTree T) {
if (T != nullptr) { MakeEmpty(T->Left); MakeEmpty(T->Right); delete T; } return NULL; }
static int Height(Position P) { if (P == nullptr) { return -1; } else { return P->Height; } }
static int FindTreeHeight(AvlTree 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; } }
static void ClearQueue(queue<AvlTree> &q) { queue<AvlTree> empty; swap(empty, q); }
void DrawTree(AvlTree BT) { int height = FindTreeHeight(BT); int cnt = 0;
queue<AvlTree> CurLevelqueue; queue<AvlTree> NextLevelqueue; queue<AvlTree> CurTempqueue; queue<AvlTree> NextTempqueue; AvlTree 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->Element, 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); } }
Position Find(ElementType X, AvlTree T) {
if (T == nullptr) { return nullptr; }
if (X < T->Element) { return Find(X, T->Left); } else if (X > T->Element) { return Find(X, T->Right); } else { return T; } } Position FindMin(AvlTree T) { if (T == nullptr) { return nullptr; } if (T->Left != nullptr) { return FindMin(T->Left); } else { return T; } } Position FindMax(AvlTree T) { if (T == nullptr) { return nullptr; } if (T->Right != nullptr) { return FindMin(T->Right); } else { return T; } }
AvlTree Insert(ElementType X, AvlTree T) {
if (T == nullptr) { T = new AvlNode; if (T == nullptr) { std::cout << "no mem!" << std::endl; } else { T->Element = X, T->Height = 0; T->Left = T->Right = nullptr; } } else if (X < T->Element) { T->Left = Insert(X, T->Left); if ((Height(T->Left) - Height(T->Right)) == 2) { if (X < T->Left->Element) { T = SingleRotateWithLeft(T); } else { T = DoubleRotateWithLeft(T); } } } else if (X > T->Element) { T->Right = Insert(X, T->Right); if ((Height(T->Right) - Height(T->Left)) == 2) { if (X > T->Right->Element) { T = SingleRotateWithRight(T); } else { T = DoubleRotateWithRight(T); } } } T->Height = max(Height(T->Left), Height(T->Right)) + 1; return T; }
AvlTree Delete(ElementType X, AvlTree T) { Position tmpCell; if (T == nullptr) { std::cout << "error" << std::endl; return nullptr; }
else if (X < T->Element) { T->Left = Delete(X, T->Left); } else if (X > T->Element) { T->Right = Delete(X, T->Right); }
else if (T->Left && T->Right) { tmpCell = FindMin(T->Right); T->Element = tmpCell->Element; T->Right = Delete(tmpCell->Element, T->Right); }
else { tmpCell = T; if (T->Left == nullptr) { T = T->Right; } else if (T->Right == nullptr) { T = T->Right; } delete tmpCell; } } ElementType Retrieve(Position P) {}
Position SingleRotateWithLeft(Position K2) { Position K1; K1 = K2->Left; K2->Left = K1->Right; K1->Right = K2; K2->Height = max(Height(K2->Left), Height(K2->Right)) + 1; K1->Height = max(Height(K1->Left), K2->Height) + 1; return K1; }
Position SingleRotateWithRight(Position K2) { Position K1; K1 = K2->Right; K2->Right = K1->Left; K1->Left = K2; K2->Height = max(Height(K2->Left), Height(K2->Right)) + 1; K1->Height = max(Height(K1->Left), K2->Height) + 1; return K1; }
Position DoubleRotateWithLeft(Position K3) { K3->Left = SingleRotateWithRight(K3->Left); return SingleRotateWithLeft(K3); }
Position DoubleRotateWithRight(Position K3) { K3->Right = SingleRotateWithLeft(K3->Right); return SingleRotateWithRight(K3); }
|