二叉树的构造与遍历
二叉树是指一个树结点最多只有两个子结点的数据结构,分别称为左子结点,右子结点。抽象来看每个结点拥有左子树、右子树。本文所记录的二叉树的构造与遍历均为递归定义。
1.定义结点类
struct TreeNode
{
int data;
TreeNode *lchild, *rchild;
TreeNode(int x = '#')
:data(x) {}
};
2.定义二叉树类
class BinTree
{
public:
BinTree();
~BinTree();
void creatTree(TreeNode *&root);
void clearTree(TreeNode *&root);
bool TreeEmpty();
int TreeDepth(TreeNode *root);
TreeNode *rootNode();
int nodeValue(TreeNode *node);
void assignNode(TreeNode *node,int val);
TreeNode *parentNode(TreeNode *root, TreeNode *node);
TreeNode *lchildNode(TreeNode *node);
TreeNode *rchildNode(TreeNode *node);
void preTraverse(TreeNode *root);
void midTraverse(TreeNode *root);
void postTraverse(TreeNode *root);
void setFactors(std::vector<int> nums);
void showFactors();
int getIndex();
private:
TreeNode *root;
std::vector<int> factors;
int index;
};
3.方法实现
#include "BiTreeLink.h"
BinTree::BinTree()
{
// this->root = new TreeNode
this->root = nullptr;
this->index = 0;
}
BinTree::~BinTree()
{
clearTree(this->root);
}
void BinTree::creatTree(TreeNode *&root)
{
/*if (this->index >= this->factors.size())
{
return;
}*/
auto p = (this->index)++;
if (this->factors.at(p) == '#')
{
// return;
// TreeNode *root = new TreeNode();
root = nullptr;
//root = new TreeNode();
}
else
{
root = new TreeNode(factors.at(p));
creatTree(root->lchild);
creatTree(root->rchild);
}
}
void BinTree::clearTree(TreeNode *&root)
{
if (root)
{
if (root->lchild)
{
clearTree(root->lchild);
}
if (root->rchild)
{
clearTree(root->rchild);
}
delete root;
root = nullptr;
}
}
bool BinTree::TreeEmpty()
{
return this->root == nullptr;
}
int BinTree::TreeDepth(TreeNode *root)
{
int i, j;
if (!root)
{
return 0;
}
if (root->lchild)
{
i = TreeDepth(root->lchild);
}
else
{
i = 0;
}
if (root->rchild)
{
j = TreeDepth(root->rchild);
}
else
{
j = 0;
}
return i > j ? i + 1 : j + 1;
}
TreeNode * BinTree::rootNode()
{
return this->root;
}
int BinTree::nodeValue(TreeNode *node)
{
if (!TreeEmpty())
{
std::cout << node->data << std::endl;
}
return node->data;
}
void BinTree::assignNode(TreeNode *node,int val)
{
if (val != 0xFFFF && node)
{
node->data = val;
}
}
TreeNode * BinTree::parentNode(TreeNode *root, TreeNode *node)
{
if (root && node)
{
if (root->lchild == node || root->rchild == node)
{
return root;
}
else
{
parentNode(root->lchild, node);
parentNode(root->rchild, node);
}
}
}
TreeNode * BinTree::lchildNode(TreeNode *node)
{
return node->lchild;
}
TreeNode * BinTree::rchildNode(TreeNode *node)
{
return node->rchild;
}
void BinTree::preTraverse(TreeNode *root)
{
if (!root)
{
return;
}
std::cout << root->data << std::endl;
preTraverse(root->lchild);
preTraverse(root->rchild);
}
void BinTree::midTraverse(TreeNode *root)
{
if (!root)
{
return;
}
midTraverse(root->lchild);
std::cout << root->data << std::endl;
midTraverse(root->rchild);
}
void BinTree::postTraverse(TreeNode *root)
{
if (!root)
{
return;
}
postTraverse(root->lchild);
postTraverse(root->rchild);
std::cout << root->data << std::endl;
}
void BinTree::setFactors(std::vector<int> nums)
{
for (auto &n : nums)
{
this->factors.push_back(n);
}
}
void BinTree::showFactors()
{
for (auto &f : this->factors)
{
std::cout << f << "=>";
}
std::cout << "END" << std::endl;
}
int BinTree::getIndex()
{
return this->index;
}