在上一篇文章中提到了二叉树的三叉链表实现,在本文中我们实现了最大推(最小堆算法类似)。在最大堆中要注意的问题是,最大堆是在完全二叉树的基础上实现的。由于上一篇中我们已经实现了二叉树,所以在此我们就不给你二叉树的代码。
#include<iostream>
#include"binaryTreeNode.h"
#include"binaryTree.h"
#include<stack>
#include<queue>
using namespace std;
void maxHeap(binaryTreeNode<int> * root, stack< binaryTreeNode<int> * > &s)// find all the non-leafnode and store them in the stack "s".
{
queue< binaryTreeNode<int> * > q;
if(NULL!=root)
q.push(root);
while(!q.empty())
{
root=q.front();
if(root->getLeftChild()!=NULL||root->getRightChild()!=NULL)
s.push(root);
q.pop();
if(root->getLeftChild())
q.push(root->getLeftChild());