本文实现了二叉树的基本功能,(搜索二叉树)节点添加,前中后序遍历,层次遍历
#include <iostream>
#include <queue>
using namespace std;
struct tree
{
/* data */
int data;
tree *left, *right;
};
class Btree
{
static int leafNode; //叶子节点数
static int oneDegreeNode; //度数为1的节点数
public:tree *root;
Btree()//构造函数
{
root = NULL;
}
~Btree()//析构函数
{
}
void addNewNode(int);//插入新节点
void preOrder(tree *);//先序遍历
void inOrder(tree *);//中序遍历
void postOrder(tree *);//后序遍历
int count(tree *);//计算二叉树的个数
int findLeaf(tree *);//求叶子节点的个数
int findNode(tree *);//求二叉树中度数为1的节点数量
void visualBorder(tree *, int); //后序遍历可视化
void levelTraversal(tree *);//层次遍历
};
int Btree::leafNode = 0;
int Btree::oneDegreeNode = 0;
/**
* @brief 插入新节点 创建的是搜索二叉树
*
* @param x 节点值
*/
void Btree::addNewNode(int x)
{
tree *newNode = new tree;
newNode->data = x;
newNode->right = newNode->left = NULL;
if (root == NULL)
{
root = newNode;
}
else
{
tree *back;
tree *current = root;
while (NULL != current)
{
back = current;
if (current->data > x)
{
current = current->left;
}
else
{
current = current->right;
}
}
if (back->data > x)
{
back->left = newNode;
}
else
{
back->right = newNode;
}
}
}
/**
* @brief 计算树节点数
*
* @param p 树的根节点
* @return int
*/
int Btree::count(tree *p)
{
if (NULL == p)
{
return 0;
}
else
{
return count(p->left) + count(p->right) + 1;
}
}
/**
* @brief 先序遍历二叉树
*
* @param temp 树的根节点
*/
void Btree::preOrder(tree *temp)
{
if (temp)
{
cout << temp->data << " ";
preOrder(temp->left);
preOrder(temp->right);
}
}
/**
* @brief 中序遍历
*
* @param temp 树的根节点
*/
void Btree::inOrder(tree *temp)
{
if (temp)
{
inOrder(temp->left);
cout << temp->data << " ";
inOrder(temp->right);
}
}
/**
* @brief 后序遍历
*
* @param temp 树的根节点
*/
void Btree::postOrder(tree *temp)
{
if (temp)
{
postOrder(temp->left);
postOrder(temp->right);
cout << temp->data << " ";
}
}
/**
* @brief 叶子数量
*
* @param temp 树的根节点
*/
int Btree::findLeaf(tree *temp)
{
if (NULL == temp)
{
return 0;
}
else
{
if (temp->left == NULL && temp->right == NULL)//左右都为空时 此节点为叶子节点
{
return leafNode += 1;
}
else
{
findLeaf(temp->left);
findLeaf(temp->right);
}
}
return leafNode;
}
/**
* @brief 求度数为1的节点数
*
* @param temp 树的根节点
*/
int Btree::findNode(tree *temp)
{
if (NULL == temp)
{
return 0;
}
else
{
if (temp->left != NULL && temp->right != NULL)
{
findNode(temp->left);
findNode(temp->right);
}
else if (temp->left == NULL && temp->right != NULL)
{
oneDegreeNode += 1;
findNode(temp->right);
}
else if (temp->left != NULL && temp->right == NULL)
{
oneDegreeNode += 1;
findNode(temp->left);
}
}
return oneDegreeNode;
}
/**
* @brief 后序遍历可视化
*
* @param temp 树根节点
* @param nplayer 节点深度
*/
void visualBorder(tree * temp, int nplayer)
{
if (NULL == temp)
{
return;
}
else
{
visualBorder(temp->right, nplayer + 1);
for (int i = 1; i <= nplayer; i++)
{
if (i == nplayer)
{
cout << " |--";
}
else
{
cout << " ";
}
}
cout << temp->data << endl;
visualBorder(temp->left, nplayer + 1);
}
}
/**
* @brief 层次遍历
*
* @param temp 树根节点
*/
void Btree::levelTraversal(tree * temp)
{
queue<tree *> treeQueue;
tree * t = NULL;
if (NULL == temp)
{
return;
}
else
{
treeQueue.push(temp);
while(treeQueue.size())
{
t = treeQueue.front();
if (t->left)
{
treeQueue.push(t->left);
}
if (t->right)
{
treeQueue.push(t->right);
}
cout << t->data << " ";
treeQueue.pop();
}
}
}
int main()
{
Btree bTree;//创建二叉树对象
int array[] = {8, 45, 75, 41, 5, 4, 2, 3, 6, 5, 45, 99, 66, 55, 49};
int space = sizeof(array)/sizeof(array[0]);
cout << "Build a sorted binary tree: ";
for (int i = 0; i < space; i++)
{
cout << array[i] << " ";
bTree.addNewNode(array[i]);
}
cout << endl << endl;
cout << "Number of nodes in a binary tree : " << bTree.count(bTree.root) << endl << endl;
cout << "Number of leaf nodes in a binary tree : " << bTree.findLeaf(bTree.root) << endl << endl;
cout << "Number of one degree nodes in a binary tree : " << bTree.findNode(bTree.root) << endl << endl;
cout << "first : ";
bTree.preOrder(bTree.root);
cout << endl << endl;
cout << "middle : ";
bTree.inOrder(bTree.root);
cout << endl << endl;
cout << "post : ";
bTree.postOrder(bTree.root);
cout << endl << endl;
cout << "level : ";
bTree.levelTraversal(bTree.root);
cout << endl;
visualBorder(bTree.root, 1);
return 0;
}