C++实现二叉树建立(先序建表)及遍历实例

本代码主要利用C++实现二叉树先序建表,及先序、后序、中序三种遍历模式,重载了输出流运算符对树里面数据的输出。
下面是测试结果截图:
C++实现二叉树建立(先序建表)及遍历实例_第1张图片
BiTree.h文件:

#pragma once

#include "iostream"
#include "stdlib.h"
#include "exception"
#include "malloc.h"

using namespace std;

//节点类
template
class Node
{
public:
	T data;
	Node   *lchild, *rchild;
	Node()
	{
		this->lchild = NULL;
		this->rchild = NULL;
	}
	Node(T elem)
	{
		this->data = elem;
		this->lchild = NULL;
		this->rchild = NULL;
	}
	~Node() {};
};


//二叉树链式存储
template
class BiTree
{
public:
	Node* root;
	BiTree();
	BiTree(T elem[], int num);
	~BiTree();

	bool Empty();
	int GetLength();

	Node* CreatePreOrder(T elem[], int &i, int num);    //先序建立二叉树

	void PreOrder(Node* p);   //先序遍历
	void InOrder(Node* p);    //中序遍历
	void PostOrder(Node* p);  //后续遍历
	

	friend ostream& operator<< <>(ostream& output, BiTree tree);   //重载<<
private:
	Node node;
	int tree_len;
};
template
BiTree::BiTree()
{
	this->root = new Node();
	this->tree_len = 0;
}

template
BiTree::BiTree(T elem[], int num)
{
	int i = 0;
	this->root = this->CreatePreOrder(elem, i, num);
	this->tree_len = num;
}

template
Node* BiTree::CreatePreOrder(T elem[], int &i, int num)
{
	Node* p = NULL;
	if (i < num)
	{
		T m = *(elem + i);
		i++;
		if (m == 11)
		{
			p = new Node(m);
			p->lchild = NULL;
			p->rchild = NULL;
		}
		else
		{
			p = new Node(m);
			p->lchild = this->CreatePreOrder(elem, i, num);
			p->rchild = this->CreatePreOrder(elem, i, num);
		}
	}
	return p;
}

template
BiTree::~BiTree()
{
	delete this->root;  
}

template
bool BiTree::Empty()
{
	if (this->tree_len = 0)
		return 1;
	else
		return 0;
}

template
int BiTree::GetLength()
{
	return this->tree_len;
}

template
void BiTree::PreOrder(Node* p)
{
	if (p != NULL)
	{
		if (p->data != 11)
		{
			cout << p->data <<"   ";
			PreOrder(p->lchild);
			PreOrder(p->rchild);
		}
		else
		{
			PreOrder(p->lchild);
			PreOrder(p->rchild);
		}
	}
}

template
void BiTree::InOrder(Node* p)
{
	if (p!= NULL)
	{
		if (p->data != 11)
		{
			InOrder(p->lchild);
			cout << p->data << "    ";
			InOrder(p->rchild);
		}
		else
		{
			InOrder(p->lchild);
			InOrder(p->rchild);
		}
	}
}

template
void BiTree::PostOrder(Node* p)
{
	if (p != NULL)
	{
		if (p->data != 11)
		{
			PostOrder(p->lchild);
			PostOrder(p->rchild);
			cout << p->data << "    ";
		}
		else
		{
			PostOrder(p->lchild);
			PostOrder(p->rchild);
		}
	}
}


template
ostream& operator<< <>(ostream& output, BiTree tree)
{
	output << "输出:";
	Node* p = NULL;
	p = tree.root;
	if (p != NULL)
	{
		if (p->data != 11)
		{
			output << p->data << "   ";
			tree.PreOrder(p->lchild);
			tree.PreOrder(p->rchild);
		}
		else
		{
			tree.PreOrder(p->lchild);
			tree.PreOrder(p->rchild);
		}
	}
	output << endl;
	return output;
}

test.cpp文件

#include "BiTree.h"

int main()
{
	int num = 11;
	int elem[] = { 1,2,4,11,11,5,11,11,3,11,11};
	BiTree tree(elem, num);
	cout << "先序遍历输出:";
	tree.PreOrder(tree.root);
	cout << endl;
	cout << "中序遍历输出:" ;
	tree.InOrder(tree.root);
	cout << endl;
	cout << "中序遍历输出:";
	tree.PreOrder(tree.root);
	cout << endl;
	cout << tree;
	system("pause");

	return 0;
}

建树的时候注意下初始化数组的大小和二叉树中元素的大小是不一致的!本人新手上路,还望大家多多指教!

你可能感兴趣的:(C++,数据结构)