温故而知新 -> 数据结构 -> 二叉树 -> 链式存储 ->程序实现2_利用类
本篇博客是基于 温故而知新 -> 数据结构 ->树 -> 二叉树 -> 链式存储 中实现链式存储 的理论知识进行程序实现!
其中实现了 二叉树 的 前序、中序、后序三种遍历,获得树的高度、节点个数等操作!并附带了相关实例以及对应的运行结果!
注意:由于使用递归更好实现,所以采用了递归的方式,这就导致类中函数的调用有点怪,且其中代码有很大冗余,未考虑性能最优,读者有兴趣可进一步精简!
以下程序针对目标为下图所示二叉树
其中 #
代表空结点!
具体内容如下:
(1)tree.h
#pragma once
#include<iostream>
using namespace std;
typedef char TDataType;
//二叉树节点 二叉链
typedef struct BNode
{
BNode *_left;
BNode *_right;
TDataType _data;
};
class BTree
{
public:
BTree()
:_root(nullptr)
{}
//创建一个二叉树,数组的顺序为前序遍历
//比如前序遍历的数组“ABD##E#H##CF##G##”,其中 # 代指空树
BTree(TDataType arr[], int *idx)
{
_root = createBTree(arr, idx);
}
BNode *createBTree(TDataType arr[], int *idx)
{
if (arr[*idx] == '#')
{
(*idx)++;
return NULL;
}
BNode *root = (BNode*)malloc(sizeof(BNode));
root->_data = arr[*idx];
(*idx)++;
root->_left = createBTree(arr, idx);
root->_right = createBTree(arr, idx);
return root;
}
~BTree()
{
if (_root)
BTreeDestory(&_root);
}
// 返回根节点
BNode *BTreeRoot();
// 前序遍历
void preOrder(BNode *root);
// 中序遍历
void inOrder(BNode *root);
// 后序遍历
void postOrder(BNode *root);
// 节点个数
int BTreeSize(BNode *root);
// 树高
int BTreeHeight(BNode *root);
// 叶子节点的个数
int BTreeLeafSize(BNode *root);
// 第k层节点个数 = 左右子树第 k-1 层节点个数之和
int BTreeKSize(BNode *root,int k);
// 查找节点
BNode *BTreeFind(BNode *root, TDataType ch);
// 销毁
void BTreeDestory(BNode **root);
private:
BNode *_root;//二叉树的根节点
};
(2)main.cpp
#include"tree.h"
// 返回根节点
BNode *BTree::BTreeRoot()
{
return _root;
}
// 前序遍历 中左右
void BTree::preOrder(BNode *root)
{
if (root)
{
cout << root->_data << " ";
preOrder(root->_left);
preOrder(root->_right);
}
}
// 中序遍历 左中右
void BTree::inOrder(BNode *root)
{
if (root)
{
inOrder(root->_left);
cout << root->_data << " ";
inOrder(root->_right);
}
}
// 后序遍历 左右中
void BTree::postOrder(BNode *root)
{
if (root)
{
postOrder(root->_left);
postOrder(root->_right);
cout << root->_data << " ";
}
}
// 节点个数
int BTree::BTreeSize(BNode *root)
{
if (root == NULL)
return 0;
return BTreeSize(root->_left) + BTreeSize(root->_right) + 1;
}
// 树高
int BTree::BTreeHeight(BNode *root)
{
if (root == NULL)
return 0;
int left = BTreeHeight(root->_left);
int right = BTreeHeight(root->_right);
return left > right ? left + 1 : right + 1;
}
// 叶子节点的个数
int BTree::BTreeLeafSize(BNode *root)
{
//空树
if (root == NULL)
return 0;
//叶子节点
if (root->_left == NULL&&root->_right == NULL)
return 1;
return BTreeLeafSize(root->_left) + BTreeLeafSize(root->_right);
}
// 第k层节点个数 = 左右子树第 k-1 层节点个数之和
int BTree::BTreeKSize(BNode *root, int k)
{
if (root == NULL)
return 0;
if (k == 1)
return 1;
return BTreeKSize(root->_left, k-1) + BTreeKSize(root->_right, k-1);
}
// 查找节点
BNode *BTree::BTreeFind(BNode *root, TDataType ch)
{
if (root)
{
BNode *node;
if (root->_data == ch)
return root;
node = BTreeFind(root->_left, ch);
if (node)
return node;
else
return BTreeFind(root->_right, ch);
}
return NULL;
}
// 销毁 用双重指针,则可以用指针去代替二叉树
//指针可以全部置空,没有野指针
//但如果参数直接用指针,则调用后指针没有置空,
//置空的是指针的拷贝,存在野指针
void BTree::BTreeDestory(BNode **root)
{
if (*root)
{
BTreeDestory(&((*root)->_left));
BTreeDestory(&((*root)->_right));
free(*root);
root = NULL;
}
}
void test()
{
char arr[] = "ABD##E#H##CF##G##";
int idx = 0;
BTree bt(arr, &idx);
cout << "前序遍历:"; bt.preOrder(bt.BTreeRoot()); cout << endl;
cout << "中序遍历:"; bt.inOrder(bt.BTreeRoot()); cout << endl;
cout << "后序遍历:"; bt.postOrder(bt.BTreeRoot()); cout << endl;
cout << "节点个数:" << bt.BTreeSize(bt.BTreeRoot()) << endl;
cout << "树的高度:" << bt.BTreeHeight(bt.BTreeRoot()) << endl;
cout << "第二层叶子节点个数:" << bt.BTreeKSize(bt.BTreeRoot(),2) << endl;
}
int main()
{
test();
system("pause");
return 0;
}
(3)运行结果
运行结果可以和博客开头的图进行对比分析!
以上就是本篇博客内容!
侵权删~