二叉树的实现和基本操作

二叉树的实现基本操作

#include<iostream>
#include<stack>
#include<queue>
using namespace std;

template<class Elem>
struct BinNode{
	Elem data;
	BinNode<Elem>* left;
	BinNode<Elem>* right;
	BinNode(Elem x) {
		data = x;
		left = right = NULL;
	}
};

template<class Elem>
class BinTree {
protected:
	BinNode<Elem> *root;
	
	void rpreprint(BinNode<Elem> *r) {
		if (r == NULL)
			return;
		cout << r->data << ' ';
		rpreprint(r->left);
		rpreprint(r->right);
	}

	void ipreprint(BinNode<Elem>* e) {
		stack<BinNode<Elem>*> st;
		if (e == NULL)
			return;
		while (e) {
			cout << e->data << " ";
			st.push(e);
			e = e->left;
			while (e == NULL && !st.empty()) {
				e = st.top();
				st.pop();
				e = e->right;
			}
		}
	}

	void rinprint(BinNode<Elem>* r) {
		if (r == NULL)
			return;
		rinprint(r->left);
		cout << r->data << ' ';
		rinprint(r->right);
	}

	void irinprint(BinNode<Elem>* e) {
		stack<BinNode<Elem>*> st;
		if (e == NULL)
			return;
		while (e) {
			st.push(e);
			e = e->left;
			while (e == NULL && !st.empty()) {
				e = st.top();
				st.pop();
				cout << e->data << " ";
				e = e->right;
			}
		}
	}

	void rreprint(BinNode<Elem>* r) {
		if (r == NULL)
			return;
		rreprint(r->left);
		rreprint(r->right);
		cout << r->data << ' ';
	}
	

	void rlevelprint(BinNode<Elem>* e) {//层次遍历,利用队列实现 
		if (e == NULL)
			return;

		queue<BinNode<Elem>* > que;//构造一个树结点指针的队列
		que.push(e);

		while (!que.empty()) {

			if ((que.front())->left != NULL){//que.front()拿到最前结点
				que.push((que.front())->left);
			}

			if ((que.front())->right != NULL) {
				que.push((que.front())->right);
			}

			cout << (que.front())->data << " ";
			que.pop();
		}
		cout << endl;
	}

	BinNode<Elem>* rfindx(Elem x, BinNode<Elem>* r) {
		if (!r)
			return NULL;
		if (r->data == x)
			return r;
		BinNode<Elem>* found;
		found = rfindx(x,r->left);
		return found ? found : rfindx(x, r->right);
	}
	void rprint(BinNode<Elem>* r, int depth) {
		for (int i = 0; i < depth; i++)
			cout << "  ";
		if (!r){
				cout << "[/]" << endl;
		}
		else {
			cout << r->data << endl;
			rprint(r->left, depth + 1);
			rprint(r->right, depth + 1);
		}
	}



public:
	BinTree() {
		root = NULL;//空树
	}
	BinTree(Elem e) {
		root = new BinNode<Elem>(e);
	}
	~BinTree(){
		//释放结点
	}

	void preprint() {//前序遍历
		//rpreprint(root);
		ipreprint(root);
		cout << endl;
	}

	void inprint() {//中序
		//rinprint(root);
		irinprint(root);
		cout << endl;
	}

	void reprint() {//后序
		rreprint(root);
		cout << endl;
	}

	void levelprint() {//层序遍历
		rlevelprint(root);
		cout << endl;
	}

	void print() {
		rprint(root, 0);//打印(形
	}

	BinNode<Elem> *findX(Elem x) {//查找有没有x存在
		return rfindx(x, root);
	}

	bool insert(Elem p, int LorR, Elem x) {
		//找p,在看看能不能插入
		BinNode<Elem>* found;
		found = findX(p);
		if (!found)
			return false;
		if (LorR == 0) {
			if (found->left)
				return false;
			found->left = new BinNode<Elem>(x);
		}
		else {
			if (found->right)
				return false;
			found->right = new BinNode<Elem>(x);
		}
		return true;
	}

};

int main() {
	BinTree<int> bt(11);  
	
	bt.insert(11, 0, 22);//插入结点左0右1
	bt.insert(11, 1, 33);//插入结点
	bt.insert(33, 0, 44);//插入结点

	bt.preprint();
	bt.inprint();
	bt.reprint();
	bt.levelprint();

	return 0;
}

posted @ 2020-04-05 00:09  akatsukiya7  阅读(22)  评论(0)    收藏  举报