您的位置:首页 > Web前端 > Node.js

销毁一颗二叉树-->Destroy(Node* root)

2017-07-23 22:04 477 查看
销毁一颗二叉树-->Destroy(Node* root)

二叉树的很多问题都可用递归实现

//销毁一颗二叉树-->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:

二叉树的问题都放到该类中去实现
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐