初学数据结构---判断是否为满二叉树
来源:互联网 发布:软件项目简介模板 编辑:程序博客网 时间:2024/05/21 04:23
// 判断满二叉树.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <queue>
using namespace std;
//节点的数据结构
class BTree
{
public:
int m_nValue;
BTree* m_nLeft;
BTree* m_nRight;
public:
BTree(int value)
{
m_nValue = value;
}
};
//二叉树的插入实现
void Insert(int value, BTree* &root)
{
if(root == NULL)
{
root = new BTree(value);
}
else if(value < root->m_nValue)
Insert(value, root->m_nLeft);
else if(value > root->m_nValue)
Insert(value, root->m_nRight);
else
;
}
bool IsCompleteBTree(BTree* pRoot)
{
if(pRoot == NULL)
return false;
queue<BTree *> q;
q.push(pRoot);
bool mustHaveNoChild = false;
bool result = true;
while(!q.empty())
{
BTree* pNode = q.front();
q.pop();
if(mustHaveNoChild)//如果一个节点没有子节点;只要出现了空子树的节点,后面出现的必须为叶子节点(左字树右子树必须为空)
{
if(pNode->m_nLeft != NULL || pNode->m_nRight != NULL)
{
result = false;
break;
}
}
else
{
if(pNode->m_nLeft != NULL && pNode->m_nRight != NULL)
{
q.push(pNode->m_nLeft);
q.push(pNode->m_nRight);
}
else if(pNode->m_nLeft != NULL && pNode->m_nRight == NULL)
{
mustHaveNoChild = true;
q.push(pNode->m_nLeft);
}
else if(pNode->m_nLeft == NULL && pNode->m_nRight != NULL)
{
result = false;
break;
}
else
{
mustHaveNoChild = true;
}
}
}
return result;
}
int main()
{
BTree* m_pRoot = new BTree(5);
Insert(6, m_pRoot);
Insert(3, m_pRoot);
Insert(4, m_pRoot);
Insert(2, m_pRoot);
cout << "是否是完全二叉树:" << IsCompleteBTree(m_pRoot) << endl;
system("pause");
return 0;
}
//
#include "stdafx.h"
#include <iostream>
#include <queue>
using namespace std;
//节点的数据结构
class BTree
{
public:
int m_nValue;
BTree* m_nLeft;
BTree* m_nRight;
public:
BTree(int value)
{
m_nValue = value;
}
};
//二叉树的插入实现
void Insert(int value, BTree* &root)
{
if(root == NULL)
{
root = new BTree(value);
}
else if(value < root->m_nValue)
Insert(value, root->m_nLeft);
else if(value > root->m_nValue)
Insert(value, root->m_nRight);
else
;
}
bool IsCompleteBTree(BTree* pRoot)
{
if(pRoot == NULL)
return false;
queue<BTree *> q;
q.push(pRoot);
bool mustHaveNoChild = false;
bool result = true;
while(!q.empty())
{
BTree* pNode = q.front();
q.pop();
if(mustHaveNoChild)//如果一个节点没有子节点;只要出现了空子树的节点,后面出现的必须为叶子节点(左字树右子树必须为空)
{
if(pNode->m_nLeft != NULL || pNode->m_nRight != NULL)
{
result = false;
break;
}
}
else
{
if(pNode->m_nLeft != NULL && pNode->m_nRight != NULL)
{
q.push(pNode->m_nLeft);
q.push(pNode->m_nRight);
}
else if(pNode->m_nLeft != NULL && pNode->m_nRight == NULL)
{
mustHaveNoChild = true;
q.push(pNode->m_nLeft);
}
else if(pNode->m_nLeft == NULL && pNode->m_nRight != NULL)
{
result = false;
break;
}
else
{
mustHaveNoChild = true;
}
}
}
return result;
}
int main()
{
BTree* m_pRoot = new BTree(5);
Insert(6, m_pRoot);
Insert(3, m_pRoot);
Insert(4, m_pRoot);
Insert(2, m_pRoot);
cout << "是否是完全二叉树:" << IsCompleteBTree(m_pRoot) << endl;
system("pause");
return 0;
}
阅读全文
0 0
- 初学数据结构---判断是否为满二叉树
- 数据结构—判断是否为完全二叉树
- 数据结构之判断一棵树是否为完全二叉树
- 【数据结构】中判断一棵树是否为平衡二叉树
- 【数据结构】判断一棵树是否为完全二叉树
- 判断一棵树是否为满二叉树
- 判断是否为满二叉树
- java数据结构-二叉树-判断一颗二叉树是否为平衡二叉树
- 如何判断二叉树是否为满二叉树?
- 判断一个二叉树是否为满二叉树
- 【数据结构】判断两个二叉树是否相等
- 【二叉树】判断二叉树是否为平衡二叉树
- 数据结构与算法问题 判断两序列是否为同一二叉搜索树序列
- 数据结构课程设计(C语言)——判断两序列是否为同一二叉搜索树
- 数据结构系统回顾与总结(3)(判断是否为同一棵二叉搜索树)
- 判断二叉树是否为平衡树
- 判断树是否为平衡二叉树
- 判断二叉树是否为平衡树
- hibernate uniqueResult方法(转载)
- http_build_query()—使用给出的关联(或下标)数组生成一个经过 URL-encode 的请求字符串
- 串口收发程序
- C#高级编程之委托、lambda表达式和事件
- dubbox开发rest+json指南
- 初学数据结构---判断是否为满二叉树
- Oracle查询,如果以0开头和字符串拼接会出现0不见的问题
- JS对象继承
- RxJava+Retrofit+OkHttp 深入浅出-终极封装一
- 关于setsid() 函数的说明
- TensorFlow 图像预处理(二) 图像翻转,图像色彩调整
- TDDL、Amoeba、Cobar、MyCAT架构比较
- MOOC清华《程序设计基础》第2章:猜数游戏(from 1 to 100)
- sublime text 3 前端程序员整理插件