二叉树ADT,周游二叉树,递归非递归,求节点的父节点,兄弟节点

#pragma once
#include "BinaryTreeNode.h"
#include<stack>
enum Tags{Left,Right};

template<class T>
class StackElem{
public:
	BinaryTreeNode<T>* pointer;
	Tags tag;
};

template <class T>
class BinaryTree
{
public:
	BinaryTree(){root=NULL;};
	~BinaryTree(){};
	BinaryTreeNode<T>* Root(){return root;};
	void createTree(const T& info,BinaryTree<T>& left,BinaryTree<T>& right);
	bool isEmpty()const;
	//先序遍历递归
	void preOrder(BinaryTreeNode<T>* current);
	//中序遍历递归
	void inOrder(BinaryTreeNode<T>* current);
	//后序遍历递归
	void postOrder(BinaryTreeNode<T>* current);
	void Visit(T value){cout<<value;};

	//先序遍历非递归
	void preOrderWithoutRec(BinaryTreeNode<T>* current);
	//中序遍历非递归
	void inOrderWithoutRec(BinaryTreeNode<T>* current);
	//后序遍历非递归
	void postOrderWithoutRec(BinaryTreeNode<T>* current);

	//寻找父节点
	BinaryTreeNode<T>* parent(BinaryTreeNode<T>* current);
	//寻找左兄弟
	BinaryTreeNode<T>* leftSibling(BinaryTreeNode<T>* current);
	//寻找右兄弟
	BinaryTreeNode<T>* rightSibling(BinaryTreeNode<T>* current);

private:
	BinaryTreeNode<T> *root;
};

template<class T>
void BinaryTree<T>::createTree(const T& info,BinaryTree<T>& left,BinaryTree<T>& right){
	root=new BinaryTreeNode<T>(info,left.root,right.root);
	left.root=NULL;
	right.root=NULL;
}

template<class T>
bool BinaryTree<T>::isEmpty()const{
	return root?false:true;
}

template<class T>
void BinaryTree<T>::preOrder(BinaryTreeNode<T>* current){
	if(current != NULL){
		Visit(current->val());
		preOrder(current->leftChild());
		preOrder(current->rightChild());
	}
}

template<class T>
void BinaryTree<T>::inOrder(BinaryTreeNode<T>* current){
	if(current!=NULL){
		inOrder(current->leftChild());
		Visit(current->val());
		inOrder(current->rightChild());
	}
}

template<class T>
void BinaryTree<T>::postOrder(BinaryTreeNode<T>* current){
	if(current!=NULL){
		postOrder(current->leftChild());
		postOrder(current->rightChild());
		Visit(current->val());
	}
}
template<class T>
void BinaryTree<T>::preOrderWithoutRec(BinaryTreeNode<T>* current){
	BinaryTreeNode<T>* pointer = current;
	stack<BinaryTreeNode<T>*> astack;
	while(pointer || !astack.empty()){
		while(pointer){
			astack.push(pointer);
			Visit(pointer->val());
			pointer = pointer->leftChild();
		}
		pointer = astack.top();
		astack.pop();
		pointer=pointer->rightChild();
	}
}

template<class T>
void BinaryTree<T>::inOrderWithoutRec(BinaryTreeNode<T>* current){
	BinaryTreeNode<T>* pointer = current;
	stack<BinaryTreeNode<T>*> astack;
	while(pointer || !astack.empty()){
		while(pointer){
			astack.push(pointer);
			pointer = pointer->leftChild();
		}
		pointer = astack.top();
		astack.pop();
		Visit(pointer->val());
		pointer=pointer->rightChild();
	}
}

template<class T>
void BinaryTree<T>::postOrderWithoutRec(BinaryTreeNode<T>* current){
	StackElem<T> element;
	using std::stack;
	stack<StackElem<T>> astack;
	BinaryTreeNode<T>* pointer;
	if(current == NULL)
		return;
	pointer = current;
	while(pointer || !astack.empty()){
		while(pointer){
			element.pointer=pointer;
			element.tag=Left;
			astack.push(element);
			pointer=pointer->leftChild();
		}
		element=astack.top();
		astack.pop();
		pointer=element.pointer;
		if(element.tag==Left){
			element.tag=Right;
			astack.push(element);
			pointer=pointer->rightChild();
		}else{
			Visit(pointer->val());
			pointer=NULL;
		}
	}
}

template<class T>
BinaryTreeNode<T>* BinaryTree<T>::parent(BinaryTreeNode<T>* current){
	if(root==NULL || current==NULL)
		return NULL;
	if(current == root)
		return NULL;
	using std::stack;
	stack<BinaryTreeNode<T>*> astack;
	BinaryTreeNode<T>* pointer = root;
	while(pointer || !astack.empty()){
		if(pointer){
			if(current==pointer->leftChild() || current==pointer->rightChild())
				return pointer;
			astack.push(pointer);
			pointer=pointer->leftChild();
		}else{
			pointer = astack.top();
			astack.pop();
			pointer=pointer->rightChild();
		}
	}
}
template<class T>
BinaryTreeNode<T>* BinaryTree<T>::leftSibling(BinaryTreeNode<T>* current){
	if(current==NULL)
		return NULL;
	BinaryTreeNode<T>* par = this->parent();
	if(par==NULL || par->leftChild==current)
		return NULL;
	return par->leftChild();
}

template<class T>
BinaryTreeNode<T>* BinaryTree<T>::rightSibling(BinaryTreeNode<T>* current){
	if(current==NULL)
		return NULL;
	BinaryTreeNode<T>* par = this->parent();
	if(par==NULL || par->rightChild==current)
		return NULL;
	return par->rightChild();
}

Main

#include<iostream>
#include<string>
#include "BinaryTree.h"
using namespace std;
int *nextfun(string P){
	int m = P.length();
	int *ret = new int[m];
	ret[0] = 0;
	for(int i = 1 ; i < m ; i++){
		int k = ret[i-1];
		if(P[i] == P[k]){
			ret[i] = k + 1;
		}
		else{
			while(P[i]!=P[k] && k>0)
				k = ret[k-1];
			if(P[i] == P[k])
				ret[i] = k + 1;
			else
				ret[i] = 0;
		}
	}
	return ret;
}
int KMPStrMatch(string S,string P,int *N){
	int slen = S.length(),plen = P.length();
	if(slen < plen)
		return -1;
	int i,j=0;
	for(i = 0 ; i < slen ; i++){
		while(P[j] != S[i] && j > 0)
			j = N[j-1];
		if(P[j] == S[i])
			j++;
		cout<<"i="<<i<<";"<<"j="<<j<<endl;
		if(j == plen)
			return (i-j+1);
	}
	return -1;
}
int main(){
	/*string S="abaabababba";
	string P = "abaa";
	int* a = nextfun(P);
	for(int i = 0 ; i < 5 ; i++){
		cout<<a[i]<<" ";
	};
	cout<<endl;
	cout<<KMPStrMatch(S,P,a)<<endl;*/

	BinaryTree<char> a,b,c,d,e,f,g,h,i,nulltree;
	d.createTree('D', nulltree, nulltree);
	g.createTree('G', nulltree, nulltree);
	h.createTree('H', nulltree, nulltree);
	i.createTree('I', nulltree, nulltree);
	f.createTree('F', h, i);
	e.createTree('E', g, nulltree);
	b.createTree('B', d, e);
	c.createTree('C', nulltree, f);
	a.createTree('A', b, c);

	cout<<"pre order:"<<endl;
	a.preOrder(a.Root());
	cout<<endl<<"in order:"<<endl;
	a.inOrder(a.Root());
	cout<<endl<<"post order:"<<endl;
	a.postOrder(a.Root());
	cout<<endl;

	cout<<"preorder traverse without recusive:"<<endl;
	a.preOrderWithoutRec(a.Root());
	cout<<endl;

	cout<<"inorder traverse without recusive:"<<endl;
	a.inOrderWithoutRec(a.Root());
	cout<<endl;

	cout<<"postorder traverse without recusive:"<<endl;
	a.postOrderWithoutRec(a.Root());
	cout<<endl;


}



你可能感兴趣的:(二叉树ADT,周游二叉树,递归非递归,求节点的父节点,兄弟节点)