优先级队列的实现 和 层次遍历建树

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

class priority_queue
{
private:
    vector<int> data;

public:
    void push(int t){
        data.push_back(t);
        push_heap(data.begin(), data.end());
    }

    void pop(){
        pop_heap(data.begin(), data.end());
        data.pop_back();
    }

    int top() { return data.front(); }
    int size() { return data.size(); }
    bool empty() { return data.empty(); }
};

int main()
{
    priority_queue test;
    test.push(3);
    test.push(5);
    test.push(2);
    test.push(4);

    while (!test.empty()){
        cout << test.top() << endl;
        test.pop();
    }

    return 0;

}

层次遍历建树

按层建树是按照给定的数据数组来建立完全二叉树的过程。其中涉及到的基础知识有结构体的创建重命名以及使用、链表的创建和数组遍历。

#include <stdio.h>
#include <stdlib.h>
#define N 10
#define MAX 100
typedef struct node{
    int data;
    struct node *left;
    struct node *right;
}BTnode;
BTnode *deal(int a[],int n)
{
    int i;
    BTnode *root;
    BTnode *queue[11];
    int front=0,rear=0;//按层,使用队列
  for(i=0;i<n;i++){
    /*初始化新节点*/
  BTnode *t=(BTnode *)malloc(sizeof(BTnode));
  t->left=t->right=NULL;
  t->data=a[i];
   /*入队*/
   queue[++rear]=t;
  if(i==0){
    root=t;
  }else{
     if(!queue[rear/2]->left){
        queue[rear/2]->left=t;
     }else{
        queue[rear/2]->right=t;
        front++;
     }
  }
  }
  return root;
}
/*按层输出二叉树*/
void PrintTree(BTnode *root)
{
     BTnode *t=NULL;
     BTnode *queue[MAX];
     int front=0,rear=0;
     /*入队*/
     queue[++rear]=root;
     /*出队*/
     while(front!=rear){
     t=queue[++front];
     printf("%d",t->data);
     if(t->left) queue[++rear]=t->left;
     if(t->right) queue[++rear]=t->right;
     }
}
int main(void)
{
    int a
={1,3,5,7,9,2,4,6,8,10};
    BTnode *root=NULL;
    root=deal(a,N);
    PrintTree(root);
    return 0;
}

时间: 2024-08-02 07:01:34

优先级队列的实现 和 层次遍历建树的相关文章

数据结构——链队列实现二叉树的层次遍历

在二叉树的遍历这篇博客中https://www.cnblogs.com/wkfvawl/p/9901462.html 对于二叉树的层次遍历我只是给出了基于C++ STL的代码,这里我使用数据结构的链表,构建一个链队列来实现.这也算是我第一次使用链队列来完成某个任务,链队列代码还是来自课本,因为之前使用C++ STL时,queue中的某些函数是有返回值的列如Q.front(),而有些却没有返回值像Q.push(p),Q.pop(),就连有没有参数也是不同的,在这里我没有改动课本上的代码来适应自己的

数据结构二叉树——建立二叉树、中序递归遍历、非递归遍历、层次遍历

数据结构二叉树-- 编写函数实现:建立二叉树.中序递归遍历.借助栈实现中序非递归遍历.借助队列实现层次遍历.求高度.结点数.叶子数及交换左右子树. ("."表示空子树) #include<stdio.h> #include<stdlib.h> //***********二叉树链表节点结构 typedef char DataType; typedef struct Node {  DataType data;  struct Node*LChild;  struc

树的层次遍历(Java代码实现)

与树的前中后序遍历的DFS思想不同,层次遍历用到的是BFS思想.一般DFS用递归去实现(也可以用栈实现),BFS需要用队列去实现. 层次遍历的步骤是: 1.对于不为空的结点,先把该结点加入到队列中 2.从队中拿出结点,如果该结点的左右结点不为空,就分别把左右结点加入到队列中 3.重复以上操作直到队列为空 1 public class Solution{ 2 class TreeNode { 3 int val; 4 TreeNode left; 5 TreeNode right; 6 TreeN

数据结构开发(21):树中属性操作与层次遍历

0.目录 1.树中属性操作的实现 2.树形结构的层次遍历 3.小结 1.树中属性操作的实现 树中结点的数目: 定义功能:count(node) 在 node 为根结点的树中统计结点数目 树结点数目的计算示例: count(A) = count(B) + count(C) + count(D) + 1 在GTree.h中实现统计结点数目: protected: int count(GTreeNode<T>* node) const { int ret = 0; if( node != NULL

数据结构开发(24):二叉树中属性操作、层次遍历与典型遍历

0.目录 1.二叉树中属性操作的实现 2.二叉树结构的层次遍历 3.二叉树的典型遍历方式 4.小结 1.二叉树中属性操作的实现 二叉树的属性操作: 二叉树中结点的数目: 定义功能:count(node) 在 node 为根结点的二叉树中统计结点数目 在BTree.h中实现统计结点数目: protected: int count(BTreeNode<T>* node) const { int ret = 0; if( node != NULL ) { ret = count(node->l

数据结构之 二叉树---求二叉树后序遍历和层次遍历(先建树,再遍历)

数据结构实验之求二叉树后序遍历和层次遍历 Time Limit: 1000MS Memory limit: 65536K 题目描述 已知一棵二叉树的前序遍历和中序遍历,求二叉树的后序遍历. 输入 输入数据有多组,第一行是一个整数t (t<1000),代表有t组测试数据.每组包括两个长度小于50 的字符串,第一个字符串表示二叉树的先序遍历序列,第二个字符串表示二叉树的中序遍历序列. 输出 每组第一行输出二叉树的后序遍历序列,第二行输出二叉树的层次遍历序列 示例输入 2 abdegcf dbgeaf

lintcode 二叉树的锯齿形层次遍历 (双端队列)

题目链接: http://www.lintcode.com/zh-cn/problem/binary-tree-zigzag-level-order-traversal/ 二叉树的锯齿形层次遍历 给出一棵二叉树,返回其节点值的锯齿形层次遍历(先从左往右,下一层再从右往左,层与层之间交替进行) 样例 给出一棵二叉树 {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 返回其锯齿形的层次遍历为: [ [3], [20,9], [15,7] ] 思路: 我们用双端队列模拟一下这个过程

二叉树的层次遍历(队列) and 二叉搜索树的后序遍历序列

(一)从上往下打印出二叉树的每个节点,同一层的节点按照从左到右的顺序打印.[层次遍历] 从上到下打印二叉树的规律:每一次打印一个节点的时候,如果该节点有子节点,则把该节点的子节点放到一个队列的末尾.接下来到队列的头部取出最早进入队列的节点,重复前面的操作,直至队列中所有的节点都被打印出来为止. //二叉树的层次遍历#include<iostream>#include<queue>using namespace std; typedef int ElemType;typedef st

UVa 122 Trees on the level(建树,层次遍历)

题意  建树并层次遍历输出  (data,pos)  pos表示改节点位置  L代表左儿子  R代表右儿子 建树很简单  开始在根节点  遇到L往左走遇到R往右走 节点不存在就新建   走完了就保存改节点的值  输出直接bfs就行了了 #include<cstdio> #include<cctype> #include<cstring> using namespace std; const int maxn = 300; char s[maxn][maxn]; int