基本操作
1.遍历:前序遍历、中序遍历、后序遍历、层次遍历
前三者使用递归实现、层次遍历使用队列实现
2.计算某节点的高度:递归
3.计算以某节点为根节点的二叉树的节点个数:递归
代码
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
template<class T>
struct binaryTreeNode//定义二叉树节点
{
T element;//当前节点数值
binaryTreeNode<T>* leftChild;//左子节点指针
binaryTreeNode<T>* rightChild;//右子节点指针
binaryTreeNode()
{
leftChild = NULL;
rightChild = NULL;
}
binaryTreeNode(const T& theElement)
{
element = theElement;
leftChild = NULL;
rightChild = NULL;
}
binaryTreeNode(const T& theElement, binaryTreeNode<T>* LEFT, binaryTreeNode<T>* RIGHT)
{
element = theElement;
leftChild = LEFT;
rightChild = RIGHT;
}
};
template<class T>
int height(binaryTreeNode<T>* node)//获得某一节点下二叉树的高度
{
if (node == nullptr)
return 0;
int leftHeight = height(node->leftChild);
int rightHeight = height(node->rightChild);
if (leftHeight > rightHeight)
return ++leftHeight;
else
return ++rightHeight;
}
template<class T>
int countNode(binaryTreeNode<T>* node)//获取某一节点下二叉树的节点个数
{
if (node == nullptr)
return 0;
int leftCount = countNode(node->leftChild);
int rightCount = countNode(node->rightChild);
return leftCount + rightCount + 1;
}
template<class T>
class binaryTree
{
public:
binaryTree()
{
root = new binaryTreeNode<T>(1);
treeSize = 1;
nodeArray.clear();
}
void createTree()//创建二叉树
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
binaryTreeNode<T>* temp = root;
queue<binaryTreeNode<T>*> treeQueue;
while (temp != nullptr&&temp->element!=i)
{
if (temp->leftChild != nullptr)
treeQueue.push(temp->leftChild);
if (temp->rightChild != nullptr)
treeQueue.push(temp->rightChild);
if (treeQueue.empty())
return;
temp = treeQueue.front();
treeQueue.pop();
}
T L, R;
cin >> L >> R;
if (L != -1)
{
temp->leftChild = new binaryTreeNode<T>(L);
treeSize++;
}
if (R != -1)
{
temp->rightChild = new binaryTreeNode<T>(R);
treeSize++;
}
}
nodeArray.clear();
for (int i = 0; i < treeSize; i++)
nodeArray.push_back(nullptr);
getArray(root);
}
void preOrder() { preOrder(root); }
void inOrder() { inOrder(root); }
void postOrder() { postOrder(root); }
void levelOrder() { levelOrder(root); }
void level_height() { level_height(root); }
void level_countNode() { level_countNode(root); }
private:
binaryTreeNode<T>* root;//根节点
int treeSize;//节点个数
vector<binaryTreeNode<T>*> nodeArray;//按顺序存放节点指针
void preOrder(binaryTreeNode<T>* node)//前序遍历
{
if (node != nullptr)
{
cout << node->element << " ";
preOrder(node->leftChild);
preOrder(node->rightChild);
}
}
void inOrder(binaryTreeNode<T>* node)//中序遍历
{
if (node != nullptr)
{
inOrder(node->leftChild);
cout << node->element << " ";
inOrder(node->rightChild);
}
}
void postOrder(binaryTreeNode<T>* node)//后序遍历
{
if (node != nullptr)
{
postOrder(node->leftChild);
postOrder(node->rightChild);
cout << node->element << " ";
}
}
void getArray(binaryTreeNode<T>* node)//前序遍历二叉树,把各个节点的指针按顺序存放在指针数组中
{
if (node != nullptr)
{
nodeArray[(int)node->element-1] = node;
getArray(node->leftChild);
getArray(node->rightChild);
}
}
void levelOrder(binaryTreeNode<T>* node)//层次遍历
{
queue<binaryTreeNode<T>*> treeQueue;
binaryTreeNode<T>* temp = node;
while (temp != nullptr)
{
cout << temp->element << " ";
if (temp->leftChild != nullptr)
treeQueue.push(temp->leftChild);
if (temp->rightChild != nullptr)
treeQueue.push(temp->rightChild);
if (treeQueue.empty())
return;
temp = treeQueue.front();
treeQueue.pop();
}
}
void level_height(binaryTreeNode<T>* node)//利用之前建立好的指针数组,可直接按顺序输出以各节点为根节点的二叉树的高度
{
for (int i = 0; i < treeSize; i++)
{
if (nodeArray[i] != nullptr)
cout << height(nodeArray[i]) << " ";
}
}
void level_countNode(binaryTreeNode<T>* node)//利用之前建立好的指针数组,可直接按顺序输出以各节点为根节点的二叉树的节点个数
{
for (int i = 0; i < treeSize; i++)
{
if (nodeArray[i] != nullptr)
cout << countNode(nodeArray[i]) << " ";
}
}
};
博客介绍了二叉树的基本操作,包括遍历(前序、中序、后序用递归,层次用队列)、计算某节点高度和以某节点为根的二叉树节点个数(均用递归),还提及了相关代码。
3万+

被折叠的 条评论
为什么被折叠?



