【C++编程基础】#009 二叉树操作:创建、前序中序后序层次遍历、得到树高、宽、节点数量、只根据前序中序或中序后序生成二叉树。

问题描述

二叉树是一种比较基础的数据结构,用C++中的结构体可以实现,以下的代码利用C++完成了以下功能:

  1. 二叉树的创建
  2. 二叉树的前序遍历
  3. 二叉树的中序遍历
  4. 二叉树的后序遍历
  5. 二叉树的层次遍历
  6. 得到二叉树的高度
  7. 得到二叉树的结点个数
  8. 通过前序和中序遍历生成二叉树
  9. 通过中序和后序遍历生成二叉树

小伙伴们需要自取,有帮助的话可以点个赞~
注:本文中提供的代码均为完整代码,放入C++工程后可直接运行。

代码实现

用C++类(class)实现,共包含三个文件:

  1. tree.h 函数体声明
  2. tree.cpp 函数实现
  3. main.cpp 测试代码

文件一 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;
}

你可能感兴趣的:(C++基础编程,二叉树,c++,数据结构,算法)