二叉树是一种比较基础的数据结构,用C++中的结构体可以实现,以下的代码利用C++完成了以下功能:
小伙伴们需要自取,有帮助的话可以点个赞~
注:本文中提供的代码均为完整代码,放入C++工程后可直接运行。
用C++类(class)实现,共包含三个文件:
文件一 tree.h
#ifndef TREE_H
#define TREE_H
#include
#include
using namespace std;
struct Node
{
char value;
Node* lchild;
Node* rchild;
};
class Tree
{
public:
void preTraversal(Node*, vector<char>&); //前序遍历二叉树
void midTraversal(Node*, vector<char>&); //中序遍历二叉树
void postTraversal(Node*, vector<char>&); //后序遍历二叉树
void levelTraversal(Node*, vector<char>&, int&); //层次遍历二叉树
int getNumber(Node*); //得到二叉树的结点个数
void createTree(Node*&); //创建二叉树
int getWidth(Node*); //得到二叉树的宽度
Node* buildByPreAndMid(char*, char*, int); //通过前序和中序遍历生成二叉树
Node* buildByMidAndPost(char*, char*, int); //通过中序和后序遍历生成二叉树
};
#endif // !TREE_H
文件二 tree.cpp
#include"tree.h"
#include
#include
#include
using namespace std;
void Tree::preTraversal(Node *root, vector<char>& vec)
{
if (root == NULL)
{
return;
}
else
{
vec.push_back(root->value);
if (root->lchild != NULL)
preTraversal(root->lchild, vec);
if (root->rchild != NULL)
preTraversal(root->rchild, vec);
}
}
void Tree::midTraversal(Node *root, vector<char>& vec)
{
if (root == NULL)
{
return;
}
else
{
if (root->lchild != NULL)
midTraversal(root->lchild, vec);
vec.push_back(root->value);
if (root->rchild != NULL)
midTraversal(root->rchild, vec);
}
}
void Tree::postTraversal(Node *root, vector<char>& vec)
{
if (root == NULL)
{
return;
}
else
{
if (root->lchild != NULL)
postTraversal(root->lchild, vec);
if (root->rchild != NULL)
postTraversal(root->rchild, vec);
vec.push_back(root->value);
}
}
void Tree::levelTraversal(Node *root, vector<char>&vec, int &height)
{
if (root == NULL)
{
height = 0;
}
height = 0;
queue<Node*>que;
que.push(root);
Node*last = root;
while (!que.empty())
{
Node *temp = que.front();
if (temp != NULL)
{
vec.push_back(temp->value);
que.pop();
if (temp->lchild != NULL)
{
que.push(temp->lchild);
}
if (temp->rchild != NULL)
{
que.push(temp->rchild);
}
if (temp == last)
{
if (!que.empty())
{
last = que.back();
}
height++;
}
}
}
}
int Tree::getNumber(Node *root)
{
if (root == NULL)
return 0;
return 1 + getNumber(root->lchild) + getNumber(root->rchild);
}
void Tree::createTree(Node*&root)
{
char number;
cin >> number;
if (number == '0')
{
root = NULL;
}
else
{
root = new Node;
root->value = number;
createTree(root->lchild);
createTree(root->rchild);
}
}
int Tree::getWidth(Node*root)
{
if (root == NULL)
{
return 0;
}
queue<Node*>que;
que.push(root);
int width = 1;
int curWidth = 1;
int nextWidth = 0;
while (!que.empty())
{
while (curWidth != 0)
{
Node *temp = que.front();
que.pop();
curWidth--;
if (temp->lchild != NULL)
{
que.push(temp->lchild);
nextWidth++;
}
if (temp->rchild != NULL)
{
que.push(temp->rchild);
nextWidth++;
}
}
if (nextWidth > width)
{
width = nextWidth;
}
curWidth = nextWidth;
nextWidth = 0;
}
return width;
}
Node* Tree::buildByPreAndMid(char *pre,char *mid, int length)
{
if (length == 0)
return NULL;
Node *root = new Node;
root->value = *pre;
int i = 0;
for (; i < length; i++)
{
if (mid[i] == *pre)
break;
}
root->lchild = buildByPreAndMid(pre+1, mid, i); //从前序VLR+1开始对中序的0—k-1左子序列的k歌元素递归建立左子树
root->rchild = buildByPreAndMid(pre + i + 1, mid + i + 1, length - i - 1); //从前序VLR+1开始对中序的0—k-1左子序列的k个元素递归建立左子树
return root;
}
Node* Tree::buildByMidAndPost(char *mid, char *post, int length)
{
if (length == 0)
return NULL;
Node *root = new Node;
root->value = *(post + length - 1);
int i = 0;
for (; i < length; i++)
{
if (mid[i] == *(post + length - 1))
break;
}
root->lchild = buildByPreAndMid(mid, post, i); //从前序VLR+1开始对中序的0—k-1左子序列的k歌元素递归建立左子树
root->rchild = buildByPreAndMid(mid + i + 1, post + i, length - i - 1); //从前序VLR+1开始对中序的0—k-1左子序列的k个元素递归建立左子树
return root;
}
文件三 main.cpp
#pragma once
#include"tree.h"
#include
#include
using namespace std;
int main()
{
cout << "请输入二叉树节点,0代表NULL:" << endl;
Tree tree;
Node * root;
int height;
vector<char> vec;
tree.createTree(root);
tree.preTraversal(root, vec);
cout << "二叉树的前序遍历是: " << endl;
for (int i = 0; i < vec.size(); i++)
{
cout << vec[i] << " ";
}
cout << endl;
vec.clear();
tree.midTraversal(root, vec);
cout << "二叉树的中序遍历是: " << endl;
for (int i = 0; i < vec.size(); i++)
{
cout << vec[i] << " ";
}
cout << endl;
vec.clear();
tree.postTraversal(root, vec);
cout << "二叉树的后序遍历是: " << endl;
for (int i = 0; i < vec.size(); i++)
{
cout << vec[i] << " ";
}
cout << endl;
vec.clear();
tree.levelTraversal(root, vec, height);
cout << "二叉树的层次遍历是: " << endl;
for (int i = 0; i < vec.size(); i++)
{
cout << vec[i] << " ";
}
cout << endl;
vec.clear();
cout << "二叉树的节点数目是" << tree.getNumber(root) << endl;
cout << "二叉树的高度是" << height << endl;
cout << "二叉树的最大宽度是" << tree.getWidth(root) << endl;
tree.levelTraversal(root->lchild, vec, height);
cout << "二叉树的左子树高度是" << height << endl;
tree.levelTraversal(root->rchild, vec, height);
cout << "二叉树的右子树高度是" << height << endl;
char pre[10] = { 'a','b','d','h','i','e','l','c','f','g' };
char mid[10] = { 'b','d','h','i','e','l','a','c','f','g' };
char post[10] = { 'b','d','h','i','e','l','c','f','g','a' };
vector<char>preVec(pre, pre + 10);
vector<char>midVec(mid, mid + 10);
vector<char>postVec(post, post + 10);
vec.clear();
tree.postTraversal(tree.buildByPreAndMid(pre, mid, 10), vec);
cout << "二叉树的后序遍历是: " << endl;
for (int i = 0; i < vec.size(); i++)
{
cout << vec[i] << " ";
}
cout << endl;
vec.clear();
vec.clear();
tree.preTraversal(tree.buildByMidAndPost(mid, post, 10), vec);
cout << "二叉树的前序遍历是: " << endl;
for (int i = 0; i < vec.size(); i++)
{
cout << vec[i] << " ";
}
cout << endl;
system("pause");
return 0;
}