统计二叉树各度数的结点的个数高度宽度结点

统计二叉树各度数的结点的个数高度宽度结点最大元素的值交换结点的左孩子和右孩子删除所有叶子节点

/*设一棵二叉树以二叉链表表示,试编写有关二叉树的递归算法:

1、统计二叉树中度为1的结点

2、统计二叉树中度为2的结点

3、统计二叉树中度为0的结点

4、统计二叉树的高度

5、统计二叉树的宽度,即在二叉树各层上具有节点数最多的那一层上结点总数

6、计算二叉树中个结点最大元素的值

7、交换每个结点的左孩子结点和右孩子结点

8从二叉树中删除所有叶子节点*/

#include <iostream>

#include <queue>

#include <stack>

#include <string>

using namespace std;

template <class T>

class btrnode

{

public:

T element; //结点的数据域

btrnode<T>* lchild; //结点的左孩子结点

btrnode<T>* rchild; //结点的右孩子节点

btrnode(){ lchild = NULL; rchild = NULL; } //默认构造函数

btrnode(T ele) //给定数值域的值得构造函数

{

element = ele;

lchild = NULL;

rchild = NULL;

}

};

//二叉树的抽象数据类型

template <class T>

class btr

{

private:

btrnode<T>* root;

int count;

public:

btr(){ count = 0; } //默认构造函数

btrnode<T>* pib(string preod, string inod); //前序中序构造二叉树的算法 int num0(btrnode<T>* root); //求度为0的结点的个数

int num1(btrnode<T>* root); //求度为1的结点的个数

int num2(btrnode<T>* root); //求度为2的结点的个数

int btrhigh(btrnode<T>* root); //求树的的高度

int btrwidth(btrnode<T>* root); //求树的宽度

btrnode<T>* max(btrnode<T>* root); //二叉树中个结点最大元素的值 void change(btrnode<T>* root); //交换每个结点的左右孩子

void del(btrnode<T>* &root); //删除所有叶子节点

void preorder(btrnode<T>* root);

void visit(btrnode<T>* cur);

};

//先序、中序构造二叉树递归算法

template <class T>

btrnode<T>* btr<T>::pib(string preod, string inod)// 是二 叉树结点个数 {

string p, q, m, n;

int i = 0;

btrnode<char>* tmp = new btrnode<char>; //????????为什么直接构造无法赋值成功?????????

tmp->element = preod[0];

for (; inod[i] != preod[0]; i++);

if (preod == "" || inod == "")

return NULL;

p = preod.substr(1, i);

q = inod.substr(0, i);

m = preod.substr(i + 1);

n = inod.substr(i + 1);

tmp->lchild = pib(p, q);

tmp->rchild = pib(m, n);

return tmp;

}

//求度为0的结点的个数

template <class T>

int btr<T>::num0(btrnode<T>* root)

{

int n ,m,sum=0 ;

btrnode<T>* pointer = root;

if (pointer)

{

if (pointer->lchild == NULL && pointer->rchild == NULL) sum++;

n = num0(pointer->lchild);

sum += n;

m = num0(pointer->rchild);

sum += m;

}

return sum;

}

//求度为1的结点的个数

template <class T>

int btr<T>::num1(btrnode<T>* root)

{

int n, m, sum = 0;

btrnode<T>* pointer = root;

if (pointer)

{

if ((pointer->lchild == NULL && pointer->rchild (pointer->rchild == NULL && pointer->lchild != NULL)) sum++;

n = num1(pointer->lchild);

sum += n;

m = num1(pointer->rchild);

sum += m;

}

return sum;

}

//求度为2的结点的个数

template <class T>

int btr<T>::num2(btrnode<T>* root)

{

int n, m, sum = 0;

btrnode<T>* pointer = root;

if (pointer) NULL) != ||

{

if (pointer->lchild != NULL && pointer->rchild != NULL) sum++;

n = num2(pointer->lchild);

sum += n;

m = num2(pointer->rchild);

sum += m;

}

return sum;

}

//求树的的高度

template <class T>

int btr<T>::btrhigh(btrnode<T>* root)

{

btrnode<T>* pointer = root;

int m, n, max;

if (pointer == NULL)

return 0;

m = btrhigh(pointer->lchild);

n = btrhigh(pointer->rchild);

max = 1 + (m > n ? m : n);

return max;

}

//求树的宽度

template <class T>

int btr<T>::btrwidth(btrnode<T>* root)

{

int cur, max = 0;

btrnode<T> *pointer = root;

queue<btrnode<T> *> nqueue1 , nqueue2;

if (pointer == NULL)

return 0;

nqueue1.push(pointer);

while (!nqueue1.empty())

{

cur = 0;

while (!nqueue1.empty())

{

cur++;

pointer = nqueue1.front();

nqueue1.pop();

if (pointer->lchild != NULL)

nqueue2.push(pointer->lchild);

if (pointer->rchild != NULL)

nqueue2.push(pointer->rchild);

}

max = (max > cur ? max : cur);

while (!nqueue2.empty())

{

nqueue1.push(nqueue2.front());

nqueue2.pop();

}

}

return max;

}

/*//求树的宽度

template <class T>

int btr<T>::btrwidth(btrnode<T>* root)

{

int n, m, sum = 0;

btrnode<T>* pointer = root;

if (pointer)

{

sum = 1;

n = btrwidth(pointer->lchild);

m = btrwidth(pointer->rchild);

}

return sum;

}*/

//二叉树中个结点最大元素的值

template <class T>

btrnode<T>* btr<T>::max(btrnode<T>* root)

{

btrnode<T>* pointer = root;

btrnode<T> *tmp, *tmpl, *tmpr;

if (pointer == NULL)

return NULL;

tmpl = max(pointer->lchild);

tmpr = max(pointer->rchild);

if (tmpl == NULL && tmpr == NULL)

tmp = pointer;

else

{

if (tmpl != NULL && tmpr == NULL)

tmp = tmpl;

else if (tmpl == NULL && tmpr != NULL)

tmp = tmpr;

else

tmp = (tmpl->element > tmpr->element ? tmpl : tmpr);

tmp = (tmp->element > pointer->element ? tmp : pointer);

}

return tmp;

}

//删除所有叶子节点

template <class T>

void btr<T>::del(btrnode<T>* &root)

{

if (root == NULL)

return ;

if (root->lchild == NULL && root->rchild == NULL){

delete root;

root = NULL;

return;

}

del(root->lchild);

del(root->rchild);

}

//交换每个结点的左右孩子

template <class T>

void btr<T>::change(btrnode<T>* root)

{

btrnode<T>* tmp;

btrnode<T>* pointer = root;

if (pointer)

{

tmp = pointer->lchild;

pointer->lchild = pointer->rchild;

pointer->rchild = tmp;

}

else

return;

change(pointer->l

child);

change(pointer->rchild);

}

//前序遍历

template <class T>

void btr<T>::preorder(btrnode<T>* root)

{

if (root != NULL)

{

visit(root); //处理根结点

preorder(root->lchild);

preorder(root->rchild);

}

}

template <class T>

void btr<T>::visit(btrnode<T>* cur)

{

cout << cur->element << " ";

}

int main()

{

string a, b;

btr<char> c;

btrnode<char>* root,*tmp;

int n;

cout << "分别输入先序序列、和中序序列:";

cin >> a;

cin >> b;

root = c.pib(a, b);

n = c.num0(root);

cout << "度为0的结点个数: " << n << endl; n = c.num1(root);

cout << "度为1的结点个数: " << n << endl; n = c.num2(root);

cout << "度为2的结点个数: " << n << endl; n = c.btrhigh(root);

cout << "树的高度为: " << n << endl;

n = c.btrwidth(root);

cout << "树的宽度为: " << n << endl;

tmp = c.max(root);

cout << "最大元素值为" << tmp->element << endl; c.change(root);

c.preorder(root);

cout << endl;

c.del(root);

c.preorder(root);

return 0;

}

统计二叉树各度数的结点的个数高度宽度结点最大元素的值交换结点的左孩子和右孩子删除所有叶子节点

/*设一棵二叉树以二叉链表表示,试编写有关二叉树的递归算法:

1、统计二叉树中度为1的结点

2、统计二叉树中度为2的结点

3、统计二叉树中度为0的结点

4、统计二叉树的高度

5、统计二叉树的宽度,即在二叉树各层上具有节点数最多的那一层上结点总数

6、计算二叉树中个结点最大元素的值

7、交换每个结点的左孩子结点和右孩子结点

8从二叉树中删除所有叶子节点*/

#include <iostream>

#include <queue>

#include <stack>

#include <string>

using namespace std;

template <class T>

class btrnode

{

public:

T element; //结点的数据域

btrnode<T>* lchild; //结点的左孩子结点

btrnode<T>* rchild; //结点的右孩子节点

btrnode(){ lchild = NULL; rchild = NULL; } //默认构造函数

btrnode(T ele) //给定数值域的值得构造函数

{

element = ele;

lchild = NULL;

rchild = NULL;

}

};

//二叉树的抽象数据类型

template <class T>

class btr

{

private:

btrnode<T>* root;

int count;

public:

btr(){ count = 0; } //默认构造函数

btrnode<T>* pib(string preod, string inod); //前序中序构造二叉树的算法 int num0(btrnode<T>* root); //求度为0的结点的个数

int num1(btrnode<T>* root); //求度为1的结点的个数

int num2(btrnode<T>* root); //求度为2的结点的个数

int btrhigh(btrnode<T>* root); //求树的的高度

int btrwidth(btrnode<T>* root); //求树的宽度

btrnode<T>* max(btrnode<T>* root); //二叉树中个结点最大元素的值 void change(btrnode<T>* root); //交换每个结点的左右孩子

void del(btrnode<T>* &root); //删除所有叶子节点

void preorder(btrnode<T>* root);

void visit(btrnode<T>* cur);

};

//先序、中序构造二叉树递归算法

template <class T>

btrnode<T>* btr<T>::pib(string preod, string inod)// 是二 叉树结点个数 {

string p, q, m, n;

int i = 0;

btrnode<char>* tmp = new btrnode<char>; //????????为什么直接构造无法赋值成功?????????

tmp->element = preod[0];

for (; inod[i] != preod[0]; i++);

if (preod == "" || inod == "")

return NULL;

p = preod.substr(1, i);

q = inod.substr(0, i);

m = preod.substr(i + 1);

n = inod.substr(i + 1);

tmp->lchild = pib(p, q);

tmp->rchild = pib(m, n);

return tmp;

}

//求度为0的结点的个数

template <class T>

int btr<T>::num0(btrnode<T>* root)

{

int n ,m,sum=0 ;

btrnode<T>* pointer = root;

if (pointer)

{

if (pointer->lchild == NULL && pointer->rchild == NULL) sum++;

n = num0(pointer->lchild);

sum += n;

m = num0(pointer->rchild);

sum += m;

}

return sum;

}

//求度为1的结点的个数

template <class T>

int btr<T>::num1(btrnode<T>* root)

{

int n, m, sum = 0;

btrnode<T>* pointer = root;

if (pointer)

{

if ((pointer->lchild == NULL && pointer->rchild (pointer->rchild == NULL && pointer->lchild != NULL)) sum++;

n = num1(pointer->lchild);

sum += n;

m = num1(pointer->rchild);

sum += m;

}

return sum;

}

//求度为2的结点的个数

template <class T>

int btr<T>::num2(btrnode<T>* root)

{

int n, m, sum = 0;

btrnode<T>* pointer = root;

if (pointer) NULL) != ||

{

if (pointer->lchild != NULL && pointer->rchild != NULL) sum++;

n = num2(pointer->lchild);

sum += n;

m = num2(pointer->rchild);

sum += m;

}

return sum;

}

//求树的的高度

template <class T>

int btr<T>::btrhigh(btrnode<T>* root)

{

btrnode<T>* pointer = root;

int m, n, max;

if (pointer == NULL)

return 0;

m = btrhigh(pointer->lchild);

n = btrhigh(pointer->rchild);

max = 1 + (m > n ? m : n);

return max;

}

//求树的宽度

template <class T>

int btr<T>::btrwidth(btrnode<T>* root)

{

int cur, max = 0;

btrnode<T> *pointer = root;

queue<btrnode<T> *> nqueue1 , nqueue2;

if (pointer == NULL)

return 0;

nqueue1.push(pointer);

while (!nqueue1.empty())

{

cur = 0;

while (!nqueue1.empty())

{

cur++;

pointer = nqueue1.front();

nqueue1.pop();

if (pointer->lchild != NULL)

nqueue2.push(pointer->lchild);

if (pointer->rchild != NULL)

nqueue2.push(pointer->rchild);

}

max = (max > cur ? max : cur);

while (!nqueue2.empty())

{

nqueue1.push(nqueue2.front());

nqueue2.pop();

}

}

return max;

}

/*//求树的宽度

template <class T>

int btr<T>::btrwidth(btrnode<T>* root)

{

int n, m, sum = 0;

btrnode<T>* pointer = root;

if (pointer)

{

sum = 1;

n = btrwidth(pointer->lchild);

m = btrwidth(pointer->rchild);

}

return sum;

}*/

//二叉树中个结点最大元素的值

template <class T>

btrnode<T>* btr<T>::max(btrnode<T>* root)

{

btrnode<T>* pointer = root;

btrnode<T> *tmp, *tmpl, *tmpr;

if (pointer == NULL)

return NULL;

tmpl = max(pointer->lchild);

tmpr = max(pointer->rchild);

if (tmpl == NULL && tmpr == NULL)

tmp = pointer;

else

{

if (tmpl != NULL && tmpr == NULL)

tmp = tmpl;

else if (tmpl == NULL && tmpr != NULL)

tmp = tmpr;

else

tmp = (tmpl->element > tmpr->element ? tmpl : tmpr);

tmp = (tmp->element > pointer->element ? tmp : pointer);

}

return tmp;

}

//删除所有叶子节点

template <class T>

void btr<T>::del(btrnode<T>* &root)

{

if (root == NULL)

return ;

if (root->lchild == NULL && root->rchild == NULL){

delete root;

root = NULL;

return;

}

del(root->lchild);

del(root->rchild);

}

//交换每个结点的左右孩子

template <class T>

void btr<T>::change(btrnode<T>* root)

{

btrnode<T>* tmp;

btrnode<T>* pointer = root;

if (pointer)

{

tmp = pointer->lchild;

pointer->lchild = pointer->rchild;

pointer->rchild = tmp;

}

else

return;

change(pointer->l

child);

change(pointer->rchild);

}

//前序遍历

template <class T>

void btr<T>::preorder(btrnode<T>* root)

{

if (root != NULL)

{

visit(root); //处理根结点

preorder(root->lchild);

preorder(root->rchild);

}

}

template <class T>

void btr<T>::visit(btrnode<T>* cur)

{

cout << cur->element << " ";

}

int main()

{

string a, b;

btr<char> c;

btrnode<char>* root,*tmp;

int n;

cout << "分别输入先序序列、和中序序列:";

cin >> a;

cin >> b;

root = c.pib(a, b);

n = c.num0(root);

cout << "度为0的结点个数: " << n << endl; n = c.num1(root);

cout << "度为1的结点个数: " << n << endl; n = c.num2(root);

cout << "度为2的结点个数: " << n << endl; n = c.btrhigh(root);

cout << "树的高度为: " << n << endl;

n = c.btrwidth(root);

cout << "树的宽度为: " << n << endl;

tmp = c.max(root);

cout << "最大元素值为" << tmp->element << endl; c.change(root);

c.preorder(root);

cout << endl;

c.del(root);

c.preorder(root);

return 0;

}


相关内容

  • 一笔画问题
  • 一笔画问题(one.pas) [问题描述] 编程对给定的一个图,判断能否一笔画出,若能请输出一笔画的先后顺序,否则输出 "No Solution!". [输入格式] 输入文件名 one.in,共 n+1 行,第 1 行为图的度 n,接下来的 n 行(每行 n 个数据)为 图的邻接 ...

  • 李春葆数据结构习题与解析(修订版)
  • 李春葆编著:数据结构(C 语言篇)――习题与解析(修订版) 清华大学出版社 一.绪论 选择题 1. 数据结构是一门研究非数值计算的程序设计问题中计算机的以及它们之间的 和运算等的学科. 1 A. 数据元素 B. 计算方法 C. 逻辑存储 D. 数据映像 2 A. 结构 B. 关系 C. 运算 D. ...

  • 离散数学第二版答案(6-7章)
  • 第六章 代数系统 6.1第129页 1. 证明: 任取x , y ∈I ,g (y , x ) =算*是可交换的: 任取x , y , z ∈I , y *x =y +x -yx =x +y -xy =g (x , y ) ,因此,二元运 g (x , g (y , z )) =x *(y *z ) ...

  • 题库 数据结构
  • 第1章 绪论 一.选择题 1. 算法的计算量的大小称为计算的( B ). A .效率 B. 复杂性 C. 现实性 D. 难度 2. 算法的时间复杂度取决于(C ) A .问题的规模 B. 待处理数据的初态 C. A和B 3. 计算机算法指的是(1),它必须具备(2) 这三个特性. (1) A.计算方 ...

  • 数据结构知识点(含算法)
  • 概念总结 第一章 概论 1. 数据结构描述的是按照一定逻辑关系组织起来的待处理数据元素的表示及相关操作,涉及数据的逻辑结构.存储结构和运算 2. 数据的逻辑结构是从具体问题抽象出来的数学模型,反映了事物的组成结构及事物之间的逻辑关系 可以用一组数据(结点集合K )以及这些数据之间的 一组二元关系(关 ...

  • 数据结构题集及答案
  • 判断题 1. 数据的逻辑结构与数据元素本身的内容和形式无关.(√) 2. 一个数据结构是由一个逻辑结构和这个逻辑结构上的一个基本运算集构成的整体.(√) 3. 数据元素是数据的最小单位.(√) 4. 数据的逻辑结构和数据的存储结构是相同的.(×) 5. 程序和算法原则上是没有区别的,所以在讨论数据结 ...

  • 数据结构与算法知识点必备
  • 数据结构与方法 1. 算法的基本特征:可行性.确定性.有穷性.拥有足够的情报 2. 算法的基本运算和操作:算术运算.逻辑运算.关系运算.数据传输 3. 算法的基本控制结构:顺序结构.选择结构.循环(重复)结构 4. 算法设计的基本方法:列举法.归纳法.递推.递归.减半递推技术.回溯法 5. 算法的复 ...

  • [数据结构与算法]期末考试试题及答案
  • 一. 选择题 A .94,32,40,90,80,46,21,69 1. 在逻辑上可以把数据结构分A.P->NEXT=Q->NEXT;FREE(Q); B .32,40,21,46,69,94,90,80 成( A ) B.Q->NEXT=P; FREE(Q); C 21,32,4 ...

  • 中国邮递员问题各种算法的对比分析
  • 附录2 <图论>课程专题论文 论文题目: 中国邮递员问题各种算法的对比分析 班 级: 2008级数学与应用数学 组 长:马利巍 2011年 12 月 27 日 论文评价指标与鉴定意见 摘要 本文基于无向图的传统中国邮递员问题,给出了相应的显式整数规划模型,进一步讨论了一类基于有向图的广义 ...