//定义最大堆:初始化,删除和插入操作
#include <iostream>
using namespace std;
template <class T>
class maxHeap
{
private:
T *heapArray;
int CurrentSize;
int maxSize;
public:
maxHeap(T *array, int num, int max)
{
heapArray = new T[max];
for (int i = 0; i < num; i++)
heapArray[i] = array[i];
CurrentSize = num;
maxSize = max;
}
~maxHeap()
{
delete[] heapArray;
}
//初始化
void BuildHeap()
{
int num;
cout << "数据的数量:";
cin >> num;
if (num <= maxSize)
{
T *array = new T[num];
cout << "请输入n个数据:" << endl;
for (int i = 0; i < num; i++)
{
cin >> array[i];
Insert(array[i]);
}
}
else
{
cout << "超出堆容量" << endl;
}
}
bool isLeaf(int pos) const
{
if (leftchild(pos) || rightchild(pos))
return false;
return true;
}
int leftchild(int pos) const
{
return pos * 2 + 1 < CurrentSize ? pos * 2 + 1 : -1;
}
int rightchild(int pos) const
{
return pos * 2 + 2 < CurrentSize ? pos * 2 + 2 : -1;
}
int parent(int pos) const
{
return pos == 0 ? -1 : (pos - 1) / 2;
}
//删除
bool Remove(int pos, T &node)
{
if (pos >= CurrentSize || pos < 0)
return false;
node = heapArray[pos];
heapArray[pos] = heapArray[--CurrentSize];
//向下
SiftDown(pos);
//向上
int par = parent(pos);
while (par > 0 && heapArray[pos] > heapArray[par])
{
T temp = heapArray[par];
heapArray[par] = heapArray[pos];
heapArray[pos] = temp;
pos = par;
par = parent(pos);
}
return true;
}
//筛选法
void SiftDown(int left)
{
int i = left;
int j = 2 * i + 1;
T temp = heapArray[i];
while (j < CurrentSize)
{
if ((j < CurrentSize - 1) && (heapArray[j] < heapArray[j + 1]))
j++;
if (temp < heapArray[j])
{
heapArray[i] = heapArray[j];
i = j;
j = 2 * j + 1;
}
else
break;
}
heapArray[i] = temp;
}
void SiftUp(int pos)
{
for (int i = pos; i >= 0; i--)
SiftDown(i);
}
//插入
bool Insert(const T &newNode)
{
if (CurrentSize == maxSize)
return false;
heapArray[CurrentSize++] = newNode;
int index = CurrentSize - 1;
while (heapArray[index] > heapArray[parent(index)])
{
T temp = heapArray[parent(index)];
heapArray[parent(index)] = heapArray[index];
heapArray[index] = temp;
index = parent(index);
}
return true;
}
void MoveMax()
{
heapArray[++CurrentSize - 2] = RemoveMax();
}
T &RemoveMax()
{
T max;
Remove(0, max);
return max;
}
int getCurrSize()
{
return CurrentSize;
}
void show() const
{
for (int i = 0; i < CurrentSize; i++)
cout << heapArray[i] << "\t";
}
};
int main()
{
int array[10] = {20, 12, 35, 15, 10, 80, 30, 17, 2, 1};
maxHeap<int> mH_0(NULL, 0, 10), mH_1(array, 10, 10);
//普通法初始化
mH_0.BuildHeap();
//筛选法初始化
mH_1.SiftUp(4);
cout << "普通法初始化:";
mH_0.show();
cout << endl;
cout << "筛选法初始化:";
mH_1.show();
cout << endl;
//插入操作见普通初始化
//删除操作
int node0, node1, node2;
mH_1.Remove(9, node0);
cout << "\n删除下标为9的元素后:\t";
mH_1.show();
cout << endl
<< "被删除的数据:" << node0 << endl;
mH_1.Remove(0, node1);
cout << "\n删除下标为0的元素后:\t";
mH_1.show();
cout << endl
<< "被删除的结数据:" << node1 << endl;
mH_1.Remove(5, node2);
cout << "\n删除下标为5的元素后:\t";
mH_1.show();
cout << endl
<< "被删除的数据:" << node2 << endl;
system("pause");
return 0;
}
//瀹炵幇鐢卞厛搴忋€佷腑搴忓簭鍒楁瀯閫犱簩鍙夋爲鐨勭畻娉�
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
//浜屽弶鏍戠粨鐐�
template <class T>
class BinaryTreeNode
{
private:
T element;
BinaryTreeNode<T> *leftChild;
BinaryTreeNode<T> *rightChild;
public:
BinaryTreeNode()
{
leftChild = rightChild = 0;
}
BinaryTreeNode(const T &e)
{
element = e;
leftChild = rightChild = 0;
}
BinaryTreeNode(const T &e, BinaryTreeNode<T> *l, BinaryTreeNode<T> *r)
{
element = e;
leftChild = l;
rightChild = r;
}
~BinaryTreeNode()
{
delete leftChild;
delete rightChild;
}
BinaryTreeNode<T> *getLeftChild() const
{
return leftChild;
}
BinaryTreeNode<T> *getRightChild() const
{
return rightChild;
}
void setLeftChild(BinaryTreeNode<T> *l)
{
if (leftChild == NULL)
{
leftChild = new BinaryTreeNode<T>;
}
leftChild->setValue(l->getValue());
}
void setRightChild(BinaryTreeNode<T> *r)
{
if (rightChild == NULL)
{
rightChild = new BinaryTreeNode<T>;
}
rightChild->setValue(r->getValue());
}
void createLeftChild()
{
leftChild = new BinaryTreeNode<T>;
}
void createRightChild()
{
rightChild = new BinaryTreeNode<T>;
}
T getValue() const
{
return element;
}
void setValue(const T &val)
{
element = val;
}
bool isLeaf() const
{
if (leftChild == NULL && rightChild == NULL)
return true;
return false;
}
};
//浜屽弶鏍�
template <class T>
class BinaryTree
{
private:
BinaryTreeNode<T> *root;
public:
BinaryTree()
{
root = NULL;
}
bool isEmpty() const
{
if (root == NULL)
return true;
return false;
}
void levelOrder(BinaryTreeNode<T> *root) //灞傚簭閬嶅巻
{
queue<BinaryTreeNode<T> *> nodeQueue;
BinaryTreeNode<T> *pointer = root;
if (pointer)
nodeQueue.push(pointer);
while (!nodeQueue.empty())
{
pointer = nodeQueue.front();
visit(pointer);
nodeQueue.pop();
if (pointer->getLeftChild() != NULL)
nodeQueue.push(pointer->getLeftChild());
if (pointer->getLeftChild() != NULL)
nodeQueue.push(pointer->getRightChild());
}
}
void preOrder(BinaryTreeNode<T> *root) //閫掑綊鍓嶅簭
{
if (root != NULL)
{
visit(root);
preOrder(root->getLeftChild());
preOrder(root->getRightChild());
}
}
void preOrderWithoutRecursion(BinaryTreeNode<T> *root) //闈為€掑綊鍓嶅簭
{
stack<BinaryTreeNode<T> *> nodeStack;
BinaryTreeNode<T> *pointer = root;
while (!nodeStack.empty() || pointer != NULL)
{
if (pointer)
{
visit(pointer);
if (pointer->getRightChild() != NULL)
nodeStack.push(pointer->getRightChild());
pointer = pointer->getLeftChild();
}
else
{
pointer = nodeStack.top();
nodeStack.pop();
}
}
}
void inOrder(BinaryTreeNode<T> *root)
{
if (root != NULL)
{
inOrder(root->getLeftChild());
visit(root);
inOrder(root->getRightChild());
}
}
void inOrderWithoutRecursion(BinaryTreeNode<T> *root)
{
stack<BinaryTreeNode<T> *> nodeStack;
BinaryTreeNode<T> *pointer = root;
while (!nodeStack.empty || pointer)
{
if (pointer)
{
nodeStack.push(pointer);
pointer = pointer->getLeftChild();
}
else
{
pointer = nodeStack.top();
visit(pointer);
pointer = pointer->getRightChild();
nodeStack.pop();
}
}
}
void postOrder(BinaryTreeNode<T> *root)
{
if (root != NULL)
{
postOrder(root->getLeftChild());
postOrder(root->getRightChild());
visit(root);
}
}
void postOrderWithoutRecursion(BinaryTreeNode<T> *root)
{
stack<BinaryTreeNode<T> *> nodeStack;
BinaryTreeNode<T> *pointer = root;
BinaryTreeNode<T> *pre = root;
while (pointer)
{
while (pointer->getLeftChild() != NULL)
{
nodeStack.push(pointer);
pointer = pointer->getLeftChild();
}
while (pointer != NULL && (pointer->getRightChild() == NULL || pointer->getRightChild() == pre))
{
visit(pointer);
pre = pointer;
if (nodeStack.empty())
return;
pointer = nodeStack.top();
nodeStack.pop();
}
nodeStack.push(pointer);
pointer = pointer->getRightChild();
}
}
void visit(BinaryTreeNode<T> *t)
{
cout << t->getValue() << "\t";
}
void preinCreateTree(BinaryTreeNode<T> *root, string pre, string in)
{
if (pre.size() != 0)
{
root->setValue(pre[0]);
int index = in.find(pre[0]);
if (index > 0)
{
root->createLeftChild();
string prel, inl;
prel.assign(pre, 1, index + 1);
inl.assign(in, 0, index);
preinCreateTree(root->getLeftChild(), prel, inl);
}
if (index < in.size() - 1)
{
root->createRightChild();
string prer, inr;
prer.assign(pre, index + 1, pre.size());
inr.assign(in, index + 1, in.size());
preinCreateTree(root->getRightChild(), prer, inr);
}
}
}
// void inpostCreateTree(BinaryTreeNode<T> *t, string in);
};
int main()
{
string str_pre("abdgcef"), str_in("dgbaecf");
BinaryTreeNode<char> *root = new BinaryTreeNode<char>;
BinaryTree<char> *t;
t->preinCreateTree(root, str_pre, str_in);
t->preOrder(root);
cout << endl;
t->inOrder(root);
cout << endl;
t->postOrder(root);
system("pause");
return 0;
}