finish leftist heap, all tests passed.

This commit is contained in:
Shine wOng
2019-09-15 20:08:57 +08:00
parent d64b3d41a1
commit 6f68edba95
3 changed files with 132 additions and 0 deletions

View File

@@ -0,0 +1,72 @@
#ifndef LEFTISTHEAP_H_
#define LEFTISTHEAP_H_
#include "PriorityQueue.h"
#include "../chp5/binTree.h"
#define NPL(X) (X == nullptr? 0: X->npl)
template <typename K, typename V>
class LeftHeap: public PriorityQueue<K, V>, public BinTree<entry<K, V>>{
public:
//constructor
LeftHeap() = default;
LeftHeap(entry<K, V>* src, int n) { for (int ix = 0; ix != n; ++ix) insert(src[ix]); } //brute force algorithm
//public interfaces
int size() { return BinTree<entry<K, V>>::size(); }
bool empty() { return size() == 0; }
entry<K, V> getMax() { return __root->data; }
entry<K, V> delMax();
void insert(entry<K, V> e);
void insert(K key, V value) { insert(entry<K, V>(key, value)); }
};
#define T entry<K, V>
template<typename K, typename V>
BinNodePosi(T) merge(BinNodePosi(T) a, BinNodePosi(T) b){
if (a == nullptr) return b;
if (b == nullptr) return a;
BinNodePosi(T) temp;
if (a->data < b->data){//swap a and b
temp = a;
a = b;
b = temp;
}
if (a->rightChild == nullptr) a->rightChild = b;
else a->rightChild = merge(a->rightChild, b);
a->rightChild->parent = a;
if (NPL(a->leftChild) < NPL(a->rightChild)) {// swap the right chlid and left child of a
temp = a->leftChild;
a->leftChild = a->rightChild;
a->rightChild = temp;
}
a->npl = NPL(a->rightChild) + 1;
return a;
}
//public interfaces
template <typename K, typename V>
void LeftHeap<K, V>::insert(entry<K, V> e){
BinNodePosi(T) newNode = new BinNode<T>(e);
newNode->npl = 1;
__root = merge(__root, newNode);
__root->parent = nullptr;
++__size;
}
template <typename K, typename V>
entry<K, V> LeftHeap<K, V>::delMax(){
entry<K, V> res = getMax();
__root = merge(__root->leftChild, __root->rightChild);
if (__root) __root->parent = nullptr;
--__size;
return res;
}
#undef T
#endif

View File

@@ -1,4 +1,5 @@
#include "CompleteBinaryHeap.h"
#include "LeftistHeap.h"
#include <iostream>
#include <cassert>
using std::cout;

View File

@@ -0,0 +1,59 @@
#include "LeftistHeap.h"
#include "../chp4/Queue.h"
#include <iostream>
#include <cassert>
using std::cout;
using std::endl;
void test_insert();
void test_max();
int main(){
cout << "Running tests" << endl;
test_insert();
test_max();
cout << "All tests passed." << endl;
system("pause");
return 0;
}
#define T entry<int, int>
void test_insert(){
int keys[] = { 5, 4, 7, 9, 1, 2, 8, 3, 6, 0 };
int levelOrder[] = { 9, 8, 7, 2, 3, 5, 6, 1, 4, 0 };
LeftHeap<int, int> heap;
for (int ix = 0; ix != 10; ++ix) {
assert(heap.size() == ix);
heap.insert(entry<int, int>(keys[ix], 0));
}
//level-order traversal
int ix = 0;
Queue<BinNodePosi(T)> q;
BinNodePosi(T) curr;
q.enqueue(heap.root());
while(!q.empty()){
curr = q.dequeue();
assert(curr->data.key == levelOrder[ix++]);
if (curr->leftChild) q.enqueue(curr->leftChild);
if (curr->rightChild) q.enqueue(curr->rightChild);
}
cout << "test_insert passed." << endl;
}
void test_max(){
int keys[] = { 5, 4, 7, 9, 1, 2, 8, 3, 6, 0 };
LeftHeap<int, int> heap;
for (int ix = 0; ix != 10; ++ix) {
assert(heap.size() == ix);
heap.insert(entry<int, int>(keys[ix], 0));
}
for(int ix = 0; ix != 10; ++ix){
assert(heap.getMax().key == 9 - ix);
assert(heap.delMax().key == 9 - ix);
assert(heap.size() == 9 - ix);
}
cout << "test_max passed." << endl;
}