void heapsort (int *tab, int n) { int m = n; construct (tab, n); int i; for (i = m-1; i >= 1; i--) { tab[i] = deletemax(tab, &n); } n=m; }
template<typename T, typename KEY> void RBTree<T, KEY>::deletemax() { if (pRoot) // check if the tree is empty. { if (!isRed(pRoot->pLeft)) pRoot->color = RED; pRoot = deletemax(pRoot); if (pRoot) pRoot->color = BLACK; } }
template<typename T, typename KEY> RBTNode<T, KEY> *RBTree<T, KEY>::deletemax(RBTNode<T, KEY> *h) { if (isRed(h->pLeft)) h = RotateRight(h); if (h->pRight == NULL) { // be careful here hasn't check if the h is empty. //std::cout << "Delete max " << h->key << std::endl; delete h; return NULL; } if (!isRed(h->pRight) && !isRed(h->pRight->pLeft)) h = MoveRedRight(h); h->pRight = deletemax(h->pRight); if (isRed(h->pRight)) h = RotateLeft(h); return h; }
int main() { int i,num; scanf("%d",&num); for(i=1;i<=num;i++) scanf("%d",&h[i]); n=num; creat(); for(i=1;i<=num;i++) printf("%d ",deletemax()); getchar();getchar(); return 0; }