销毁一颗二叉树-->Destroy(Node* root)
2017-07-23 22:04
477 查看
销毁一颗二叉树-->Destroy(Node* root)
二叉树的很多问题都可用递归实现
具体代码实现(包含了建树,遍历及一些常见问题)
PS:
二叉树的问题都放到该类中去实现
二叉树的很多问题都可用递归实现
//销毁一颗二叉树-->Destroy(Node* root) void _Destory(Node* root) { if (root != NULL) { _Destory(root->_left); _Destory(root->_right); delete root; } }
具体代码实现(包含了建树,遍历及一些常见问题)
#include <iostream>
#include <assert.h>
#include <queue>
#include <Windows.h>
using namespace std;
template<class T>
struct BinaryTreeNode //构建二叉树的节点,及左右子树的指针
{
T _data; //值
BinaryTreeNode<T>* _left; //左子树
BinaryTreeNode<T>* _right; //右子树
BinaryTreeNode(const T& data) //构造
:_data(data)
, _left(NULL)
, _right(NULL)
{}
};
template<class T>
class BinaryTree //实现二叉树
{
typedef BinaryTreeNode<T> Node; //节点
protected:
Node* _root; //指向根节点的指针,里面保存根节点
Node* CreateTree(T* a, size_t n, const T& invalid, size_t& index) //递归
{
Node* root = NULL;
if (index < n && a[index] != invalid) //a不为非法值
{
//先序遍历建树
root = new Node(a[index]); //根
root->_left = CreateTree(a, n, invalid, ++index); //左
root->_right = CreateTree(a, n, invalid, ++index); //右
}
return root;
}
void _PreOrder(Node* root) //前序遍历 根 左 右
{
if (root == NULL)
{
return;
}
cout << root->_data << " ";
_PreOrder(root->_left);
_PreOrder(root->_right);
}
void _InOrder(Node* root) //中序遍历 左 根 右
{
if (root == NULL)
{
return;
}
_InOrder(root->_left);
cout << root->_data << " ";
_InOrder(root->_right);
}
void _PostOrder(Node* root) //后序遍历 左 右 根
{
if (root == NULL)
{
return;
}
_PostOrder(root->_left);
_PostOrder(root->_right);
cout << root->_data << " ";
}
void _LevelOrder(Node* root) //层序遍历 利用队列的特性依次遍历
{
if (root == NULL)
{
return;
}
queue<Node*> tty;
tty.push(root);
while (!tty.empty())
{
if (tty.front()->_left != NULL)
{
tty.push(tty.front()->_left);
}
if (tty.front()->_right != NULL)
{
tty.push(tty.front()->_right);
}
cout << tty.front()->_data << " ";
tty.pop();
}
cout << endl;
}
int _GetNodeyezi(Node* root) //求二叉树叶子节点的个数
{
if (root == NULL)
{
return 0;
}
if ((root->_left == NULL) && (root->_right == NULL))
{
return 1;
}
return _GetNodeyezi(root->_left) + _GetNodeyezi(root->_right); //左子树叶子节点+右子树叶子节点
}
int _GetNodeK(Node* root, size_t k) //求二叉树第K层节点个数
{
if (root == NULL || k <= 0)
{
return 0;
}
if (root != NULL && k == 1)
{
return 1;
}
return _GetNodeK(root->_left, k - 1) + _GetNodeK(root->_right, k - 1);
}
int _TreeDepth(Node* root) //求二叉树的深度
{
if (root == NULL)
{
return 0;
}
int leftDepth = _TreeDepth(root->_left);
int rightDepth = _TreeDepth(root->_right);
return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;
}
//输入一颗二叉树的根节点,判断该树是不是平衡二叉树
bool _IsBalanceTree(Node* root)
{
if (root == NULL) //空树是平衡的二叉树
{
return true;
}
int leftDepth = _TreeDepth(root->_left);
int rightDepth = _TreeDepth(root->_right);
int dif = leftDepth - rightDepth;
if (dif <= 1 || dif >= -1)
{
return true;
}
return _IsBalanceTree(root->_left) && _IsBalanceTree(root->_right);
}
//销毁一颗二叉树-->Destroy(Node* root) void _Destory(Node* root) { if (root != NULL) { _Destory(root->_left); _Destory(root->_right); delete root; } }
public:
BinaryTree()
:_root(NULL)
{}
BinaryTree(T* a, size_t n, const T& invalid = T()) //构建二叉树,数组存放
{
size_t index = 0;
_root = CreateTree(a, n, invalid, index);
}
void PreOrder() //前序
{
_PreOrder(_root);
}
void InOrder() //中序
{
_InOrder(_root);
}
void PostOrder() //后序
{
_PostOrder(_root);
}
void LevelOrder() //层序
{
_LevelOrder(_root);
}
int GetNodeyezi()
{
return _GetNodeyezi(_root);
}
int GetNodeK(size_t k)
{
return _GetNodeK(_root, k);
}
int TreeDepth()
{
return _TreeDepth(_root);
}
bool IsBalanceTree()
{
return _IsBalanceTree(_root);
}
void Destory()
{
_Destory(_root);
}
};
#include "源.h" void test() { int array[10] = { 1, 2, 3, '#', '#', 4, '#', '#', 5, 6 }; BinaryTree<int> p(array, sizeof(array) / sizeof(array[0]), '#'); //构建二叉树时是用前序建的树 cout << "前序: " << ""; p.PreOrder(); cout << endl; cout << "中序: " << ""; p.InOrder(); cout << endl; cout << "后序: " << ""; p.PostOrder(); cout << endl; cout << "层序: " << ""; p.LevelOrder(); cout << p.GetNodeyezi() << endl; cout << p.GetNodeK(4) << endl; cout << p.TreeDepth() << endl; cout << p.IsBalanceTree() << endl; p.Destory(); p.InOrder(); } int main() { test(); system("pause"); return 0; }
PS:
二叉树的问题都放到该类中去实现
相关文章推荐
- 求二叉树的高度/销毁一颗二叉树-->Destroy(Node* root)
- /* 创建二叉树 ----> 装入数据,---->遍历---> 显示 --->销毁*/
- ::递归实现——创建二叉树 ----> 装入数据--->遍历---> 显示 --->销毁
- 给定一颗二叉树,以及其中的两个node(地址均非空),要求给出这两个node的一个公共父节点,使得这个父节点与两个节点的路径之和最小。
- 求二叉树的高度/销毁一颗二叉树
- 数据结构面试题/求二叉树的高度/销毁一棵二叉树-->
- 求二叉树的高度和销毁一颗二叉树——题集九
- 求二叉树的高度/销毁一颗二叉树【递归思想】
- 二叉树--求二叉树的高度/销毁一颗二叉树
- day11之求二叉树的高度+销毁一颗二叉树+链表每K个节点之间逆序
- 求二叉树的高度/销毁一颗二叉树【每日一题】
- 从根开始按层次(第0层->第1层->第2层)遍历一颗二叉树,需要使用什么辅助数据结构?
- 将一颗二叉树转换成它的镜像
- 搜索二叉树实现->迭代器实现->转化为双向链表
- node.js源码分析 <一>
- Common Schema Error: a TypeName that collides with the root node name
- 二叉树的操作(建树,先序遍历,交换左右子树,销毁
- datanode----nitialization failed for Block pool <registering> (Datanode Uuid unassigned) service to
- 打开jmeter.bat,提示:Could not open/create prefs root node Software\JavaSoft
- “因以下文件损坏〈window root>/system32/hal.dll 损坏无法正常启动请安装以上的拷贝文件”解决