二叉树类模板完整代码-C++实现

//二叉树:链表实现
#ifndef OCTREE_H
#define OCTREE_H

#include"OCstack.h"
using OC::stack;
#include"OCqueue.h"
using OC::queue;
#include<iostream>
using std::cout;

#ifndef _OC_BEGIN
#define _OC_BEGIN namespace OC{
#endif

#ifndef _OC_END
#define _OC_END }
#endif

#ifndef NULL
#define NULL 0
#endif

_OC_BEGIN

template<class T>
class tree {
private:
	//树的结点
	struct TreeNode;
	typedef TreeNode*NodePtr;
	static T out_of_range;
protected:
	NodePtr _root;	//根结点
public:
	class iterator;			//迭代器
	class const_iterator;	//常迭代器
	tree();		//构造函数
	tree(tree const&object);//复制构造函数
	~tree();	//析构函数
	iterator root()const;			//返回指向根结点的迭代器
	const_iterator croot()const;	//返回指向根结点的常迭代器
	bool create(T init_element);	//创建树
	void clear();					//清空树
	bool empty()const;				//判断是否为空树
	void InvertChild(iterator it);	//交换左右子树
	T*find(T search_element)const;	//查找
	//先序遍历
	void PreOrderTraversal(iterator iter)const;
	void PreOrderTraversal(const_iterator iter)const;
	void PreOrderTraversal()const;
	//中序遍历
	void InOrderTraversal(iterator iter)const;
	void InOrderTraversal(const_iterator iter)const;
	void InOrderTraversal()const;
	//后序遍历
	void PostOrderTraversal(iterator iter)const;
	void PostOrderTraversal(const_iterator iter)const;
	void PostOrderTraversal()const;
	//层序遍历
	void LevelOrderTraversal(iterator iter)const;
	void LevelOrderTraversal(const_iterator iter)const;
	void LevelOrderTraversal()const;
	//输出叶结点
	void PrintLeaves(iterator iter)const;
	void PrintLeaves(const_iterator iter)const;
	void PrintLeaves()const;
	//求树的高度
	int height(iterator iter)const;
	int height(const_iterator iter)const;
	int height()const;
	//求树的结点数
	int size(iterator iter)const;
	int size(const_iterator)const;
	int size()const;
	//插入和删除
	bool insert_left(iterator it, T insert_element);	//左插入
	bool insert_right(iterator it, T insert_element);	//右插入
	void erase_left(iterator iter);		//删除左子树
	void erase_right(iterator iter);	//删除右子树
	void operator=(tree const&copy_tree);
	bool operator==(tree const&another)const;
	bool operator!=(tree const&another)const;
};

//_TREENODE_BEGIN

template<class T>
struct tree<T>::TreeNode {
	typedef TreeNode*NodePtr;
	T data;
	NodePtr left, right;
	TreeNode(T init_element);
};

template<class T>
tree<T>::TreeNode::TreeNode(T init_element) :data(init_element), left(NULL), right(NULL) {

}

//_TREENODE_END

template<class T>
T tree<T>::out_of_range;

//_ITERATOR_BEGIN

template<class T>
class tree<T>::iterator {
protected:
	NodePtr node_pointer;
public:
	friend class tree;	//友元声明
	iterator(NodePtr init_pointer);
	T&operator*()const;
	bool operator==(iterator it)const;
	bool operator!=(iterator it)const;
	iterator left()const;
	iterator right()const;
};

template<class T>
tree<T>::iterator::iterator(NodePtr init_pointer) :node_pointer(init_pointer) {

}

template<class T>
T&tree<T>::iterator::operator*()const {
	if (!node_pointer)return out_of_range;
	return node_pointer->data;
}

template<class T>
bool tree<T>::iterator::operator==(typename tree<T>::iterator it)const {
	if (this->node_pointer && !it.node_pointer ||
		!this->node_pointer&&it.node_pointer)return false;
	if (this->node_pointer->data != it.node_pointer->data)return false;
	if (this->node_pointer->left != it.node_pointer->left)return false;
	if (this->node_pointer->right != it.node_pointer->right)return false;
	return true;
}

template<class T>
bool tree<T>::iterator::operator!=(typename tree<T>::iterator it)const {
	return this->operator==(it) ? false : true;
}

template<class T>
typename tree<T>::iterator tree<T>::iterator::left()const {
	if (!node_pointer)return iterator(NULL);
	return iterator(node_pointer->left);
}

template<class T>
typename tree<T>::iterator tree<T>::iterator::right()const {
	if (!node_pointer)return iterator(NULL);
	return iterator(node_pointer->right);
}

//_ITERATOR_END

//_CONST_ITERATOR_BEGIN

template<class T>
class tree<T>::const_iterator {
protected:
	NodePtr node_pointer;
public:
	friend class tree;	//友元声明
	const_iterator(NodePtr init_pointer);
	T const&operator*()const;
	bool operator==(const_iterator it)const;
	bool operator!=(const_iterator it)const;
	const_iterator left()const;
	const_iterator right()const;
};

template<class T>
tree<T>::const_iterator::const_iterator(NodePtr init_pointer) :node_pointer(init_pointer) {

}

template<class T>
T const&tree<T>::const_iterator::operator*()const {
	if (!node_pointer)return out_of_range;
	return node_pointer->data;
}

template<class T>
bool tree<T>::const_iterator::operator==(typename tree<T>::const_iterator it)const {
	if (this->node_pointer && !it.node_pointer ||
		!this->node_pointer&&it.node_pointer)return false;
	if (this->node_pointer->data != it.node_pointer->data)return false;
	if (this->node_pointer->left != it.node_pointer->left)return false;
	if (this->node_pointer->right != it.node_pointer->right)return false;
	return true;
}

template<class T>
bool tree<T>::const_iterator::operator!=(typename tree<T>::const_iterator it)const {
	return this->operator!=(it) ? false : true;
}

template<class T>
typename tree<T>::const_iterator tree<T>::const_iterator::left()const {
	if (!node_pointer)return const_iterator(NULL);
	return const_iterator(node_pointer->left);
}

template<class T>
typename tree<T>::const_iterator tree<T>::const_iterator::right()const {
	if (!node_pointer)return const_iterator(NULL);
	return const_iterator(node_pointer->right);
}

//_CONST_ITERATOR_END

template<class T>
tree<T>::tree() :_root(NULL) {

}

template<class T>
tree<T>::tree(tree const&object) {
	this->operator=(object);
}

template<class T>
tree<T>::~tree() {
	clear();
}

template<class T>
typename tree<T>::iterator tree<T>::root()const {
	return iterator(_root);
}

template<class T>
typename tree<T>::const_iterator tree<T>::croot()const {
	return const_iterator(_root);
}

template<class T>
bool tree<T>::create(T init_element) {
	if (_root)return true;
	_root = new TreeNode(init_element);
	return false;
}

template<class T>
void tree<T>::clear() {
	if (!_root)return;
	stack<NodePtr>S;
	NodePtr it = _root;
	do {
		while ((it->left)) {
			S.push(it);
			it = it->left;
		}
		if (it->right) {
			S.push(it);
			it = it->right;
		}
		else while (!S.empty()) {
			while (it == S.top()->left && !S.top()->right ||
				it == S.top()->right && !S.top()->left) {
				delete it;
				it = S.top();
				S.pop();
				if (S.empty())break;
			}
			if (S.empty())break;
			if (it == S.top()->left) {
				delete it;
				it = S.top()->right;
				break;
			}
			else {
				delete it;
				it = S.top();
				S.pop();
				if (S.empty())delete it;
			}
		}
	} while (!S.empty());
	_root = NULL;
}

template<class T>
bool tree<T>::empty()const {
	return _root ? false : true;
}

template<class T>
void tree<T>::InvertChild(typename tree<T>::iterator it) {
	if (!it.node_pointer)return;
	NodePtr temp;
	temp = it.node_pointer->left;
	it.node_pointer->left = it.node_pointer->right;
	it.node_pointer->right = temp;
}

template<class T>
T*tree<T>::find(T search_element)const {
	if (this->empty())return NULL;
	stack<NodePtr>S;
	NodePtr it = _root;
	while (it || !S.empty()) {
		while (it) {
			if (it->data == search_element)return&it->data;
			S.push(it);
			it = it->left;
		}
		if (!S.empty()) {
			it = S.top()->right;
			S.pop();
		}
	}
	return NULL;
}

template<class T>
void tree<T>::PreOrderTraversal(iterator iter)const {
	if (!iter.node_pointer)return;
	stack<NodePtr>S;
	NodePtr it = iter.node_pointer;
	while (it || !S.empty()) {
		while (it) {
			cout << it->data;
			S.push(it);
			it = it->left;
		}
		if (!S.empty()) {
			it = S.top()->right;
			S.pop();
		}
	}
}

template<class T>
void tree<T>::PreOrderTraversal(typename tree<T>::const_iterator iter)const {
	PreOrderTraversal(iterator(iter.node_pointer));
}

template<class T>
void tree<T>::PreOrderTraversal()const {
	PreOrderTraversal(iterator(_root));
}

template<class T>
void tree<T>::InOrderTraversal(iterator iter)const {
	if (!iter.node_pointer)return;
	stack<NodePtr>S;
	NodePtr it = iter.node_pointer;
	while (it || !S.empty()) {
		while (it) {
			S.push(it);
			it = it->left;
		}
		if (!S.empty()) {
			it = S.top();
			S.pop();
			cout << it->data;
			it = it->right;
		}
	}
}

template<class T>
void tree<T>::InOrderTraversal(typename tree<T>::const_iterator iter)const {
	InOrderTraversal(iterator(iter.node_pointer));
}

template<class T>
void tree<T>::InOrderTraversal()const {
	InOrderTraversal(iterator(_root));
}

template<class T>
void tree<T>::PostOrderTraversal(iterator iter)const {
	if (!iter.node_pointer)return;
	stack<NodePtr>S;
	NodePtr it = iter.node_pointer;
	do {
		while (it->left) {
			S.push(it);
			it = it->left;
		}
		if (it->right) {
			S.push(it);
			it = it->right;
		}
		else while (!S.empty()) {
			while (it == S.top()->left && !S.top()->right ||
				it == S.top()->right && !S.top()->left) {
				cout << it->data;
				it = S.top();
				S.pop();
				if (S.empty())break;
			}
			cout << it->data;
			if (S.empty())break;
			if (it == S.top()->left) {
				it = S.top()->right;
				break;
			}
			else {
				it = S.top();
				S.pop();
				if (S.empty())cout << it->data;
			}
		}
	} while (!S.empty());
}

template<class T>
void tree<T>::PostOrderTraversal(typename tree<T>::const_iterator iter)const {
	PostOrderTraversal(iterator(iter.node_pointer));
}

template<class T>
void tree<T>::PostOrderTraversal()const {
	PostOrderTraversal(iterator(_root));
}

template<class T>
void tree<T>::LevelOrderTraversal(typename tree<T>::iterator iter)const {
	if (!iter.node_pointer)return;
	queue<NodePtr>Q;
	NodePtr it = iter.node_pointer;
	Q.push(it);
	while (!Q.empty()) {
		it = Q.front();
		Q.pop();
		cout << it->data;
		if (it->left)Q.push(it->left);
		if (it->right)Q.push(it->right);
	}
}

template<class T>
void tree<T>::LevelOrderTraversal(typename tree<T>::const_iterator iter)const {
	LevelOrderTraversal(iterator(iter.node_pointer));
}

template<class T>
void tree<T>::LevelOrderTraversal()const {
	LevelOrderTraversal(iterator(_root));
}

template<class T>
void tree<T>::PrintLeaves(typename tree<T>::iterator iter)const {
	if (!iter.node_pointer)return;
	stack<NodePtr>S;
	NodePtr it = iter.node_pointer;
	while (it || !S.empty()) {
		while (it) {
			if (!it->left && !it->right)
				cout << it->data;
			S.push(it);
			it = it->left;
		}
		if (!S.empty()) {
			it = S.top()->right;
			S.pop();
		}
	}
}

template<class T>
void tree<T>::PrintLeaves(typename tree<T>::const_iterator iter)const {
	PrintLeaves(iterator(iter.node_pointer));
}

template<class T>
void tree<T>::PrintLeaves()const {
	PrintLeaves(iterator(_root));
}

template<class T>
int tree<T>::height(typename tree<T>::iterator iter)const {
	if (!iter.node_pointer)return 0;
	stack<NodePtr>S;
	NodePtr it = iter.node_pointer;
	int path = 1, longest_path = 1;
	do {
		while (it->left) {
			S.push(it);
			it = it->left;
			path++;
		}
		if (it->right) {
			S.push(it);
			it = it->right;
			path++;
		}
		else {
			if (path > longest_path)longest_path = path;
			while (!S.empty()) {
				while (it == S.top()->left && !S.top()->right ||
					it == S.top()->right && !S.top()->left) {
					it = S.top();
					S.pop();
					path--;
					if (S.empty())break;
				}
				if (S.empty())break;
				if (it == S.top()->left) {
					it = S.top()->right;
					break;
				}
				else {
					it = S.top();
					S.pop();
					path--;
				}
			}
		}
	} while (!S.empty());
	return longest_path;
}

template<class T>
int tree<T>::height(typename tree<T>::const_iterator iter)const {
	return height(iterator(iter.node_pointer));
}

template<class T>
int tree<T>::height()const {
	return height(iterator(_root));
}

template<class T>
int tree<T>::size(typename tree<T>::iterator iter)const {
	if (!iter.node_pointer)return 0;
	stack<NodePtr>S;
	NodePtr it = iter.node_pointer;
	int count = 0;
	while (it || !S.empty()) {
		while (it) {
			count++;
			S.push(it);
			it = it->left;
		}
		if (!S.empty()) {
			it = S.top()->right;
			S.pop();
		}
	}
	return count;
}

template<class T>
int tree<T>::size(typename tree<T>::const_iterator iter)const {
	return size(iterator(iter.node_pointer));
}

template<class T>
int tree<T>::size()const {
	return size(this->root());
}

template<class T>
bool tree<T>::insert_left(typename tree<T>::iterator it, T insert_element) {
	if (it.node_pointer->left)return true;
	it.node_pointer->left = new TreeNode(insert_element);
	return false;
}

template<class T>
bool tree<T>::insert_right(typename tree<T>::iterator it, T insert_element) {
	if (it.node_pointer->right)return true;
	it.node_pointer->right = new TreeNode(insert_element);
	return false;
}

template<class T>
void tree<T>::erase_left(typename tree<T>::iterator iter) {
	NodePtr it = iter.node_pointer;
	if (!it)return;
	if (!it->left)return;
	it = it->left;
	stack<NodePtr>S;
	do {
		while ((it->left)) {
			S.push(it);
			it = it->left;
		}
		if (it->right) {
			S.push(it);
			it = it->right;
		}
		else while (!S.empty()) {
			while (it == S.top()->left && !S.top()->right ||
				it == S.top()->right && !S.top()->left) {
				delete it;
				it = S.top();
				S.pop();
				if (S.empty())break;
			}
			if (S.empty())break;
			if (it == S.top()->left) {
				delete it;
				it = S.top()->right;
				break;
			}
			else {
				delete it;
				it = S.top();
				S.pop();
			}
		}
	} while (!S.empty());
	delete iter.node_pointer->left;
	iter.node_pointer->left = NULL;
}

template<class T>
void tree<T>::erase_right(typename tree<T>::iterator iter) {
	NodePtr it = iter.node_pointer;
	if (!it)return;
	if (!it->right)return;
	it = it->right;
	stack<NodePtr>S;
	do {
		while ((it->left)) {
			S.push(it);
			it = it->left;
		}
		if (it->right) {
			S.push(it);
			it = it->right;
		}
		else while (!S.empty()) {
			while (it == S.top()->left && !S.top()->right ||
				it == S.top()->right && !S.top()->left) {
				delete it;
				it = S.top();
				S.pop();
				if (S.empty())break;
			}
			if (S.empty())break;
			if (it == S.top()->left) {
				delete it;
				it = S.top()->right;
				break;
			}
			else {
				delete it;
				it = S.top();
				S.pop();
			}
		}
	} while (!S.empty());
	delete iter.node_pointer->right;
	iter.node_pointer->right = NULL;
}

//_OPERATOR_BEGIN

template<class T>
void tree<T>::operator=(tree<T> const&copy_tree) {
	clear();
	if (copy_tree.empty())return;
	queue<NodePtr>Q1, Q2;
	NodePtr it1, it2;
	create(*copy_tree.root());
	Q2.push(copy_tree._root);
	Q1.push(this->_root);
	while (!Q2.empty()) {
		it2 = Q2.front();
		Q2.pop();
		it1 = Q1.front();
		Q1.pop();
		if (it2->left) {
			Q2.push(it2->left);
			insert_left(it1, it2->left->data);
			Q1.push(it1->left);
		}
		if (it2->right) {
			Q2.push(it2->right);
			insert_right(it1, it2->right->data);
			Q1.push(it1->right);
		}
	}
}

template<class T>
bool tree<T>::operator==(tree<T> const&another)const {
	if (this->empty() ^ another.empty())return false;
	stack<NodePtr>S1, S2;
	NodePtr it1 = this->_root, it2 = another._root;
	while (it1 || !S1.empty() || it2 || !S2.empty()) {
		if (S1.empty() ^ S2.empty())return false;
		while (it1 || it2) {
			if (!it1 || !it2)return false;
			if (it1->data != it2->data)return false;
			S1.push(it1);
			S2.push(it2);
			it1 = it1->left;
			it2 = it2->left;
		}
		if (!S1.empty() || !S2.empty()) {
			if (S1.empty() || S2.empty())return false;
			it1 = S1.top()->right;
			it2 = S2.top()->right;
			S1.pop();
			S2.pop();
		}
	}
	return true;
}

template<class T>
bool tree<T>::operator!=(tree<T> const&another)const {
	return this->operator==(another) ? false : true;
}

//_OPERATOR_END

_OC_END

#endif

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二叉树的顺序存储是将二叉树按照层次遍历的顺序存储在一个一维数组中,其中根节点存储在数组下标为1的位置,左子节点存储在2i的位置,右子节点存储在2i+1的位置(i为节点在数组中的下标)。 下面是一个使用类模板实现二叉树顺序存储的示例代码: ```c++ #include <iostream> #include <vector> using namespace std; template <typename T> class SeqBinaryTree { public: SeqBinaryTree() {} SeqBinaryTree(const vector<T>& v); ~SeqBinaryTree() {} bool empty() const { return data_.empty(); } int size() const { return data_.size() - 1; } T root() const { return data_.at(1); } T parent(int i) const { return data_.at(i / 2); } T left(int i) const { return data_.at(2 * i); } T right(int i) const { return data_.at(2 * i + 1); } void insert(const T& value); void remove(int i); void print() const; private: vector<T> data_; }; template <typename T> SeqBinaryTree<T>::SeqBinaryTree(const vector<T>& v) { data_.resize(v.size() + 1); for (int i = 0; i < v.size(); ++i) { data_[i + 1] = v[i]; } } template <typename T> void SeqBinaryTree<T>::insert(const T& value) { data_.push_back(value); } template <typename T> void SeqBinaryTree<T>::remove(int i) { if (i < 1 || i > size()) { return; } data_.erase(data_.begin() + i); } template <typename T> void SeqBinaryTree<T>::print() const { for (int i = 1; i <= size(); ++i) { cout << data_[i] << " "; } cout << endl; } int main() { vector<int> v = { 1, 2, 3, 4, 5, 6, 7 }; SeqBinaryTree<int> tree(v); tree.insert(8); tree.print(); // 1 2 3 4 5 6 7 8 tree.remove(2); tree.print(); // 1 3 4 5 6 7 8 return 0; } ``` 在上面的代码中,SeqBinaryTree类模板包含了二叉树的一些基本操作,包括构造函数、判断是否为空、获取节点数目、获取根节点、获取父节点、获取左子节点、获取右子节点、插入节点、删除节点和打印二叉树。其中构造函数使用vector初始化二叉树,插入节点和删除节点都是在vector中进行操作,打印二叉树则是按照顺序输出vector中的元素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值