最大堆—链表实现

本文介绍了如何使用链表实现最大堆。通过遍历非叶节点并存储在栈中,然后进行调整以满足最大堆的性质。代码示例展示了如何在完全二叉树基础上构建并调整最大堆。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  在上一篇文章中提到了二叉树的三叉链表实现,在本文中我们实现了最大推(最小堆算法类似)。在最大堆中要注意的问题是,最大堆是在完全二叉树的基础上实现的。由于上一篇中我们已经实现了二叉树,所以在此我们就不给你二叉树的代码。

#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());
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值