当前位置: 首页 > wzjs >正文

衡水商城网站建设公司网站点击量如何看

衡水商城网站建设,公司网站点击量如何看,沈阳高端网站制作,wordpress文章推送邮箱算法笔记-第九章-树的遍历 树遍历的知识点emplace_back()用法top和pop的用法 树的先根遍历理解本题思路 树的后跟遍历树的层序遍历树的循环队列遍历 树的高度树的高度分析题目 树的结点层号 树遍历的知识点 大佬总结的实在是太好了 大佬讲解数遍历 (遍历树的前序&…

算法笔记-第九章-树的遍历

  • 树遍历的知识点
    • emplace_back()用法
    • top和pop的用法
  • 树的先根遍历
    • 理解本题思路
  • 树的后跟遍历
  • 树的层序遍历
    • 树的循环队列遍历
  • 树的高度
    • 树的高度分析
    • 题目
  • 树的结点层号

树遍历的知识点

大佬总结的实在是太好了
大佬讲解数遍历

(遍历树的前序,中序,后序遍历的递归法和迭代法)

emplace_back()用法

功能:和 push_back() 相同,都是在 vector 容器的尾部添加一个元素。
push_back() 向容器尾部添加元素时,首先会创建这个元素,然后再将这个元素拷贝或者移动到容器中(如果是拷贝的话,事后会自行销毁先前创建的这个元素),而 emplace_back() 在实现时,则是直接在容器尾部创建这个元素,省去了拷贝或移动元素的过程。

top和pop的用法

top()是取栈顶元素
pop()是弹出栈顶元素

stack a;
a.push(1); // 1
a.push(2); // 1 2
a.push(3); // 1 2 3
int c = a.top(); // c = 3
a.pop(); // 1 2
a.push(4); // 1 2 4
c = a.top(); // c = 4

树的先根遍历

在这里插入图片描述
在这里插入图片描述

#include <cstdio>
#include <vector>
using namespace std;const int MAXN = 50;struct Node {vector<int> children;
} nodes[MAXN];vector<int> pre;void preOrder(int root) {pre.push_back(root);for (int i = 0; i < nodes[root].children.size(); i++) {preOrder(nodes[root].children[i]);}
}int main() {int n, k, child;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &k);for (int j = 0; j < k; j++) {scanf("%d", &child);//每一行孩子节点信息nodes[i].children.push_back(child);//放到数组当中}}preOrder(0);//进行前序遍历for (int i = 0; i < pre.size(); i++) {printf("%d", pre[i]);if (i < (int)pre.size() - 1) {printf(" ");}}return 0;
}

题目的思路不是递归也不是迭代,所有要理解的话,需要理解

理解本题思路

回顾二叉树节点的定义问题:是由数据域和指针域组成的,
指针域存放所有子结点的地址,或者可以存放所有子结点地址,
就是静态写法,用数组下标来代替所谓地址

struct node {typename data;//数据域int child[manx];//指针域,存放所有子结点下标
}node[maxn];//节点数组,maxn为结点上限个数

但是无法预知子节点的个数,所以只能将数组的个数加到不确定的位置

struct node
{typename data;vector child;
}node[maxn;

与静态实现类似,当需要新建一个结点时,就按顺序从数组中取出一个下标
即可

int index = 0;
int newnode(int v)
{node[index].data = v;node[index].child.clear()return index++;
}

一般来说,遍历二叉树的时候使用递归访问
但是对于根节点的子树来说,同样可以分为根节点和若干子树
这就是先根遍历

void preorder(int root)     
{printf("%d ", node[root].data);//访问当前节点     for (int i = 0; i < node[data].child.size(); i++)     {preorder(node[root].child[i]);//递归访问结点root的所有结点     }
}

树的后跟遍历

在这里插入图片描述

在这里插入图片描述

左右孩子,然后根结点

#include <cstdio>
#include <vector>
using namespace std;const int MAXN = 50;struct Node {vector<int> children;
} nodes[MAXN];vector<int> post;void postOrder(int root) {for (int i = 0; i < nodes[root].children.size(); i++) {postOrder(nodes[root].children[i]);}post.push_back(root);
}int main() {int n, k, child;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &k);for (int j = 0; j < k; j++) { scanf("%d", &child); nodes[i].children.push_back(child); }}postOrder(0);   for (int i = 0; i < post.size(); i++) {   printf("%d", post[i]);   if (i < (int)post.size() - 1) {   printf(" ");   }}return 0;   
}

树的层序遍历

在这里插入图片描述
在这里插入图片描述

#include <cstdio>
#include <vector>
#include <queue>
using namespace std;const int MAXN = 50;struct Node {vector<int> children;
} nodes[MAXN];vector<int> layer;void layerOrder(int root) {queue<int> q;q.push(root);while (!q.empty()) {int front = q.front();q.pop();layer.push_back(front);for (int i = 0; i < nodes[front].children.size(); i++) {q.push(nodes[front].children[i]);}}
}int main() {int n, k, child;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &k);for (int j = 0; j < k; j++) {scanf("%d", &child);nodes[i].children.push_back(child);}}layerOrder(0);for (int i = 0; i < layer.size(); i++) {printf("%d", layer[i]);if (i < (int)layer.size() - 1) {printf(" ");}}return 0;
}

树的循环队列遍历

层次遍历
队列,循环加入到队列中


typedef struct
{BTNode* data[MaxSize];int front, rear;
}SqQueue;//层次遍历算法 结果:ABCDEFG
void LevelOrder(BTNode* b)
{BTNode* p;SqQueue* qu;	//定义环形队列指针InitQueue(qu);	//初始化队列enQueue(qu, b); //根结点进队while (!QueueEmpty(qu))  //队不空时循环  {deQueue(qu, p);  //出队结点p  printf("%c", p->data);  //访问结点p  if (p->lchild != NULL)		//有左孩子时将其进队  enQueue(qu, p->lchild);  if (p->rchild != NULL)		//有右孩子时将其进队  enQueue(qu, p->rchild);  }DestroyQueue(qu);  //销毁队列  
}

树的高度

树的高度分析

看大佬细致解释
思路:
树的高度就是表示树结点中最大层数
遍历到左子树和右子树,然后比较高度,层层的进行比较两边大小然后返回最大值

题目

在这里插入图片描述
在这里插入图片描述

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;const int MAXN = 50;struct Node {vector<int> children;
} nodes[MAXN];int getHeight(int root) {int maxHeight = 0;for (int i = 0; i < nodes[root].children.size(); i++) {maxHeight = max(maxHeight, getHeight(nodes[root].children[i]));}return maxHeight + 1;
}int main() {int n, k, child;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &k);  for (int j = 0; j < k; j++) {  scanf("%d", &child);  nodes[i].children.push_back(child);  }}printf("%d", getHeight(0));  return 0;  
}

树的结点层号

在这里插入图片描述
在这里插入图片描述

#include <cstdio>
#include <vector>
#include <queue>
using namespace std;const int MAXN = 50;struct Node {vector<int> children;
} nodes[MAXN];//输入孩子结点int layers[MAXN];void layerOrder(int root) {queue<int> q;//队列q.push(root);int layer = 1;while (!q.empty()) {int cnt = q.size();for (int i = 0; i < cnt; i++) {int front = q.front();q.pop();layers[front] = layer;for (int i = 0; i < nodes[front].children.size(); i++) {q.push(nodes[front].children[i]);}}layer++;}
}int main() {int n, k, child;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &k);for (int j = 0; j < k; j++) {scanf("%d", &child);nodes[i].children.push_back(child);}}layerOrder(0);for (int i = 0; i < n; i++) {printf("%d", layers[i]);if (i < n - 1) {printf(" ");}}return 0;
}

文章转载自:

http://DYr01kV8.skscy.cn
http://G4swVKxW.skscy.cn
http://KQOw9mmL.skscy.cn
http://JITHvx92.skscy.cn
http://WXuM7IDe.skscy.cn
http://HAtu6HYX.skscy.cn
http://59tH0jXA.skscy.cn
http://dX0NtUnO.skscy.cn
http://ksYl9uGh.skscy.cn
http://DSL4xtEl.skscy.cn
http://mNoiQq2t.skscy.cn
http://QSipMRz0.skscy.cn
http://q7eDghWX.skscy.cn
http://gTMWNXeV.skscy.cn
http://arTsC4St.skscy.cn
http://2Z9RagpS.skscy.cn
http://VADcuMxl.skscy.cn
http://GVkCYzM4.skscy.cn
http://PNYnROzp.skscy.cn
http://bklQT3nf.skscy.cn
http://VWx5WEw1.skscy.cn
http://0ibp81tV.skscy.cn
http://DymtzPVW.skscy.cn
http://VA2Ydpdd.skscy.cn
http://Lt6umDjl.skscy.cn
http://6RwyNVNX.skscy.cn
http://qTMtqL6R.skscy.cn
http://iag72ON0.skscy.cn
http://zI1sTRe3.skscy.cn
http://nuBEbfiW.skscy.cn
http://www.dtcms.com/wzjs/638777.html

相关文章:

  • 做爰全过程网站免费的视频vue做的博客网站
  • 沧州高端网站建设企业域名如何申请
  • seo网站推广专员招聘惠州市企业网站seo营销工具
  • 视频收费网站怎么做上海jsp网站建设
  • 旅游电网站建设目标企业类网站有哪些例子
  • .asp网站怎么做网上做ps赚钱的网站
  • 比特币交易网站开发深圳百度推广
  • 新乡网站建设找哪家深圳网站建设公司是
  • 靖江市建设局网站苍山县建设银行网站
  • 沧州营销型网站建设在线咨询平台系统
  • 个人手机网站大全网站产品简介
  • 网站开发与设计静态网页源代码dedecms织梦
  • 番禺 网站建设空间类网站价格
  • 外贸网站的作用有哪些微信软件定制开发
  • 广西网站推广我爱营销网
  • 微信商城网站模板基于html5的毕业设计论文
  • 邯郸做网站推广费用嘉兴自助建站模板
  • 网站更换服务器高中毕业学网站开发
  • 网站建设周期与进度安排wordpress 又拍云 缓存
  • 企业网站如何推广小程序注册申请多少钱
  • 做水果的网站有哪些安康学院的费用
  • 宣威网站建设湖北省建设教育协会网站首页
  • 什么是网站规划旅游网站建设初衷
  • 国外有哪些设计网站免费搭建公司网站
  • 商业网站的创建程序延吉网站优化
  • ip网站查询服务器做一个网站要注意什么
  • 网站开发的工作需要什么材料怎么联系百度推广
  • seo企业站收录黄石做网站
  • 移动端网站如何优化福州室内设计公司排名
  • 万网如何上传网站课程网站开发的研究现状