-
二叉树宽度
2015-03-30 20:52:41求二叉树的宽度,就是求二叉树某一层上的节点数最多的数。 public static int getHeight(BiNode head) { int deep = 0; if(head != null) { int left = getHeight(head.left); int right = ...求二叉树的宽度,就是求二叉树某一层上的节点数最多的数。
public static int getHeight(BiNode head) { int deep = 0; if(head != null) { int left = getHeight(head.left); int right = getHeight(head.right); deep = (left>=right)?(left+1):(right+1); } return deep; } public static int getWidth(BiNode head) { if(head == null) { return 0; } int nWidth = 0; int nLastLevelWidth = 0; int nTempLastLevelWidth = 0; int nCurLevelWidth = 0; Queue<BiNode> myQueue = new LinkedList<Demo.BiNode>(); myQueue.add(head); nLastLevelWidth = 1; nWidth = 1; while(!myQueue.isEmpty()) { nTempLastLevelWidth = nLastLevelWidth; BiNode tmp = null; while(nTempLastLevelWidth != 0) { tmp = myQueue.peek(); myQueue.poll(); if(tmp.left != null) { myQueue.add(tmp.left); } if(tmp.right != null) { myQueue.add(tmp.right); } nTempLastLevelWidth--; } nCurLevelWidth = myQueue.size(); nWidth = nCurLevelWidth>nWidth?nCurLevelWidth:nWidth; nLastLevelWidth = nCurLevelWidth; } return nWidth; }
参考:http://zhidao.baidu.com/link?url=K1xVilT0OyF_e_FbYr9FVycdq4MBgacNU-1iCOWtGvFJqwb28q1e5j1EJ4QefSy6Qe15ZHD9qV10Bi439AWbuqhttp://zhidao.baidu.com/link?url=K1xVilT0OyF_e_FbYr9FVycdq4MBgacNU-1iCOWtGvFJqwb28q1e5j1EJ4QefSy6Qe15ZHD9qV10Bi439AWbuq
-
求二叉树宽度
2015-05-08 17:29:36求二叉树宽度算法 void width(Tree T) //求二叉树宽度 { int w = 0, wmax = 1, last = -1; Tree p; queue q; q.front = q.rear = -1; if(T == NULL) printf("该二叉树宽度为0!\n"); else { q.data[++q....求二叉树宽度算法
void width(Tree T) //求二叉树宽度 { int w = 0, wmax = 1, last = -1; Tree p; queue q; q.front = q.rear = -1; if(T == NULL) printf("该二叉树宽度为0!\n"); else { q.data[++q.rear] = T; last = q.rear; while(q.front < q.rear) { p = q.data[++q.front]; if(p->lchild != NULL) q.data[++q.rear] = p->lchild; if(p->rchild != NULL) q.data[++q.rear] = p->rchild; w++; if(q.front == last) { if(w > wmax) wmax = w; w = 0; last = q.rear; } } printf("该二叉树宽度为%d!\n", wmax); } }
-
求二叉树宽度的递归算法
2019-07-05 08:07:06所谓二叉树宽度,就是至每一层节点数多的那一层的节点数 我的算法大致思路是: 开辟一个数组count[二叉树高度],遍历每一个节点,然后根据当前节点所在层次i,则执行count[i]++; 最后遍历完求出最大的count即为二叉树...所谓二叉树宽度,就是至每一层节点数多的那一层的节点数
我的算法大致思路是:开辟一个数组count[二叉树高度],遍历每一个节点,然后根据当前节点所在层次i,则执行count[i]++;
最后遍历完求出最大的count即为二叉树宽度,代码很简单如下
int count[100];
int MAX=-1;
void FindWidth(BitNode T,int k)
{
if(T==NULL) return;
count[k]++;
if(MAX<count[k]) MAX=count[k];
FindWidth(T->lchild,k+1);
FindWidth(T->rchild,k+1);
}
---------------------
作者:FightingBa
来源:CSDN
原文:https://blog.csdn.net/mcb199175mcb/article/details/12031217
版权声明:本文为博主原创文章,转载请附上博文链接! -
二叉树宽度优先搜索
2014-10-09 01:16:28二叉树宽度优先搜索( Breadth - first Search )使用du二叉树宽度优先搜索( Breadth - first Search )使用队列来进行进行。
算法步骤:
1. 从根节点开始,使根节点入队;
2. 当队列不为空,使队头元素出队,打印其结点的数据域,若其有左右孩子,使其左右孩子按顺序入队;
3. 队列为空时,已经按照宽度遍历整个二叉树;
实现代码:
struct Tnode; typedef struct TNode *SearchTree; struct TNode { ElementType Element; SearchTree Left; SearchTree Right; }; void bfs( SearchTree T ) { Queue Q; Q.Enqueue( T ); // Q存储指针,指针指向结点 while ( !Q.IsEmpty() ) { SearchTree TmpCell = Q.Dequeue; if ( TmpCell->Left != NULL ) Q.Enqueue( TmpCell->Left ); if ( TmpCell->Right != NULL ) Q.Eequeue( TmpCell->Right ); // 对结点Q进行打印等操作 } }
-
求二叉树宽度和深度
2017-09-18 12:00:34//二叉树宽度 int widthOfBinaryTree(Node *root) { if(root == NULL) return 0; queue nodeQueue; int maxWidth = i; nodeQueue.push(root); while(true) { in -
求二叉树中的结点个数、叶子结点个数、某结点层次和二叉树宽度
2019-01-25 15:41:00* 求二叉树中的结点个数、叶子结点个数、某结点层次和二叉树宽度 * 实验目的: * 掌握二叉树遍历算法的应用,熟练使用先序、中序、后序三种递归 * 遍历算法和层次遍历算法进行二叉树问题求解。 * 实验内容: * 设计... -
二叉树宽度的计算
2017-04-19 20:04:00二叉树的宽度定义为具有最多结点数的层中包括的结点数。试计算一二叉树的宽度。 #include "stdafx.h" #include<iostream> #include<vector> using namespace std; struct BiNOde { int ... -
二叉树宽度和高度
2013-11-11 01:22:31求二叉树的深度和宽度 // 求二叉树的深度和宽度.cpp : 定义控制台应用程序的入口点。 #include "stdafx.h" #include #include using namespace std; struct BTNode { char m_value; BTNode *m_left; BTNode *... -
树与二叉树之层次遍历、求二叉树宽度
2018-12-26 11:19:121.层次遍历 算法思想:建立一个循环队列,先将二叉树头结点如队列,然后出队列,访问该结点,若该结点有左子树则左结点队列,如果有右子树入队列,然后出队访问,直到为空 void level(BTNode *p) ... -
二叉树宽度(非递归)天勤
2020-08-23 16:50:15求二叉树的宽度函数 typedef struct { BTNode *p; int lno; }St; int maxNode(BTNode *b) { St que[maxSize]; int front,rear; int Lno=0,i,j,n,max=0; front=rear=0; BTNode *q; if(b!=NULL) { ++rear;... -
二叉树宽度优先遍历
2013-06-06 08:59:03算法竞赛入门经典 6.3.2节,二叉树的宽度优先遍历 运用结构体变量。注意在定义函数的时候是环环相扣的。最开始先定义节点的数据类型,然后定义newnode,然后再定义addnode(addnode里面要用到newnode),然后在定义... -
递归算法--二叉树宽度
2019-10-06 23:00:23思路:实际上是在先序遍历二叉树。递归一次,说明深入了一层。所以,在每次进入递归之时该层节点数++。 int count[MaxSize];//全局数组 int max = -1;全局变量 void width(BitNode T, int k){ if(T==null) return; ... -
[经典面试题]二叉树宽度
2015-03-07 10:51:17(1)二叉树最大宽度 /*--------------------------------------------- * 日期:2015-03-07 * 作者:SJF0115 * 题目: 二叉树的最大宽度 * 来源:经典面试题 * 博客: --------------------------------------... -
半递归求二叉树宽度
2018-12-11 17:29:21int height(Node&amp;lt;int&amp;gt; * root){ if(root ==NULL)return 0; int l = 0;int r = 0; if(root-&amp;gt;L == NULL &amp;amp;&amp;...gt -
计算二叉树宽度(BFS)
2020-08-27 19:13:34#define MaxSize 15 typedef struct BTNode{ char data; struct BTNode* left; struct BTNode* right; }BTNode; //BFS 模板代码 void level(BTNode* p){ int front,rear; BTNode* que[MaxSize];...= NULL) { -
求解二叉树宽度的递归与非递归算法
2020-09-14 10:56:36//在对二叉树进行遍历结束后,在对数组进行处理,便可以获得,相应的二叉树的宽度 //子啊该方法的实现机制中,利用了辅助函数 //方法2:关于非递归的算法,就是利用队列的原理,这一原理在求树的深度那一篇博文... -
判断二叉树是否是完全二叉树,求二叉树宽度
2012-08-06 22:32:00算法1:若无左子女则不应该有右子女 #include "stdafx.h" #include<iostream> #include<queue> using namespace std; typedef struct BTreeNode { int data; struct BTreeNode *...}BT...
-
基于bs的企业考勤管理系统
-
MindSpore数据集mindspore::dataset
-
云计算基础-Linux系统管理员
-
text-overflow.html
-
excel批量生成word.rar
-
ZigBee开发相关软件.zip
-
转行做IT-第7章 数组
-
宾得Kx使用手册.pdf
-
计算机专业文献综述格式示例.doc
-
【数据分析-随到随学】机器学习模型及应用
-
Linux部署环境搭建(JAVA项目).doc
-
C++异步串口通信
-
python办公自动化技巧
-
Qt项目实战之基于Redis的网络聊天室
-
【2021】UI自动化测试框架(Selenium3)
-
【数据分析-随到随学】Mysql数据库
-
我写的PE格式查看软件 仿PEiD.zip
-
中国矿业大学程序设计综合实践代码.zip
-
MFC开发简单聊天程序
-
OpenMP详细用法,OMP太牛B了能够发辉多核CPU100的性能