南邮数据结构实验6.1二叉树的基本操作

内容和提示:

1.在二叉链表上实现二叉树运算

(1)设计递归算法,实现下列二叉树元算:

删除一颗二叉树,

求一颗二叉树的高度,

求一棵二叉树中叶子结点数,

复制一棵二叉树,

交换一棵二叉树的左右子树。


//btnode.h
#include<iostream>
using namespace std;
template<class T>
struct BTNode
{
	T element;
	BTNode<T> *lChild,*rChild;
	BTNode(){ lChild=rChild=NULL;}
	BTNode(const T &x){element=x; lChild=rChild=NULL;}
	BTNode(const T &x,BTNode *l,BTNode *r){element=x; lChild=l; rChild=r;}
};
//seqqueue.h
template <class T>
class SeqQueue
{
public:
	SeqQueue(int mSize);
	void InitiaQueue();
	~SeqQueue()	{delete []q;}
	bool IsEmpty()const{return front == rear;}
	bool IsFull()const{return (rear + 1) % maxSize == front;}
	bool Front(T &x)const;
	bool EnQueue(T x);
	bool DeQueue();
	void Clear(){front = rear = 0; }
	int GetFront(){return front;}
private:
	int front, rear;
	int maxSize;
	T *q;
};


template <class T>
SeqQueue<T>::SeqQueue(int mSize)
{
	maxSize = mSize;
	q = new T[maxSize];
	front = rear = 0;
}

template <class T>
void SeqQueue<T>::InitiaQueue()
{
	int i;  
	for ( i = 0; i < maxSize; i++ )  
	{  
		q[i] = 0;  // 初始值都为0   
		front = 0;   
		rear = 0;
	}
}

template <class T>
bool SeqQueue<T>::Front(T &x)const
{
	if (IsEmpty())
	{
		cout<<"Empty!"<<endl;
		return false;
	}
	x = q[(front + 1) % maxSize];
	return true;
}

template <class T>
bool SeqQueue<T>::EnQueue(T x)
{
	if (IsFull())
	{
		cout<<"Full!"<<endl;
		return false;
	}
	q[(rear = rear + 1) % maxSize] = x;  //rear++ OK?
	return true;
}

template <class T>
bool SeqQueue<T>::DeQueue()
{
	if (IsEmpty())
	{
		cout<<"Empty!"<<endl;
		return false;
	}
	front = (front + 1) % maxSize;
	return true;
}

//binarytree.h
#include"btnode.h"
#include"seqqueue.h"
template<class T>
class BinaryTree
{
	public:
		BinaryTree(){root=NULL;}
		~BinaryTree(){Clear();}
		void MakeTree(const T &x,BinaryTree<T> &left,BinaryTree<T> &right);
		void BreakTree(		T &x,BinaryTree<T> &left,BinaryTree<T> &right);
		void PreOrder(void (*Visit)(T &x));
		int Size();
		void Copy(BinaryTree &temp);
		int Height();
		void Exchange();
		int LeafNum();
		void Clear();
		void LevelOrder(void (*Visit)(T &x));

	protected:

		BTNode<T> *root;
	private:
		void PreOrder(void (*Visit)(T &x),BTNode<T> *t);
		int Size(BTNode *t);
		BTNode<T> * Copy(BTNode<T> * t);
		int Height(BTNode<T>* t);
		void Exchange(BTNode<T> *t);	
		int Size(BTNode<T> *t);
		int LeafNum(BTNode<T> *t);
		void Clear(BTNode<T> *x);
		//void LevelOrder (void (*Visit)(T& x),BTNode<T>* &t);
		void LevelOrder (BTNode<T>* t);
	
};
/*MakeTree*/
template<class T>
void BinaryTree<T>::MakeTree(const T &x,BinaryTree<T> &left,BinaryTree<T> &right)
{
	if(root||&left==&right) return;
	root=new BTNode<T>(x,left.root,right.root);
	left.root=right.root=NULL;
}
/*BreakTree*/
template<class T>
void BinaryTree<T>::BreakTree(T &x,BinaryTree<T> &left,BinaryTree<T> &right)
{
	if(!root||&left==&right||left.root||right.root) return;
	x=root->element;
	left.root=root->lChild;right.root=root->rChild;
	delete root;root=NULL;
}


/*PreOrder*/
template<class T>
void Visit(T &x)
{
	cout<<x<<" ";
}
template<class T>
void BinaryTree<T>::PreOrder(void (*Visit)(T &x))
{
	PreOrder(Visit,root);
}
template<class T>
void BinaryTree<T>::PreOrder(void (*Visit)(T &x),BTNode<T> *t)
{
	if(t)
	{
		Visit(t->element);
		PreOrder(Visit,t->lChild);
		PreOrder(Visit,t->rChild);
	}
}
/*Size*/
template<class T>
int BinaryTree<T>::Size()
{	
	return Size(root);
}

template<class T>
int BinaryTree<T>::Size(BTNode<T> *t)
{
	if(!t) return 0;
	
	static int num=0;
	if(t->lChild) num++;
	if(t->rChild) num++;
	Size(t->lChild);
	Size(t->rChild) ;
	return num+1;    
	
	//return Size(t->lChild)+Size(t->rChild)+1;
}

/*Copy*/
template<class T>
void  BinaryTree<T>::Copy(BinaryTree<T> &temp)
{
	 root=Copy(temp.root);
}
template
<class T>
BTNode<T> * BinaryTree<T>::Copy(BTNode<T> * t)
{
	if(!t) return NULL;
	BTNode<T> *q= new BTNode<T>(t->element);
	q->lChild=Copy(t->lChild);q->rChild=Copy(t->rChild);
	return q;
}



/*Height*/
template<class T>
int BinaryTree<T>::Height()
{
	return Height(root);
}

template<class T>
int BinaryTree<T>::Height(BTNode<T>* t)
{
	int ltemp=0;
	int rtemp=0;
	if(!t)
		return 0;
	ltemp=Height(t->lChild);
	rtemp=Height(t->rChild);
	if(ltemp++ > rtemp++)
		return ltemp;
	else
		return rtemp;
}

/*LeafNum*/
template<class T>
int BinaryTree<T>::LeafNum()
{
	return LeafNum(root);
}
template<class T>
int BinaryTree<T>::LeafNum(BTNode<T> *t)
{
	static int num=0;
	if(!t) return 0;
	if((!(t->lChild))&&(!(t->rChild))) num++;
	LeafNum(t->lChild);
	LeafNum(t->rChild);
	return num;
}

/*Exchange*/
template <class T>
void BinaryTree<T>::Exchange()
{
	Exchange(root);
}
template <class T>
void BinaryTree<T>::Exchange(BTNode<T> *t)
{
	if (!t) return;
	BTNode<T> *temp;
	temp = t->lChild;
	t->lChild = t->rChild;
	t->rChild = temp;
	Exchange(t->lChild);
	Exchange(t->rChild);
}


/*Clear*/
template<class T>
void BinaryTree<T>::Clear()
{
	Clear(root);
}
template <class T>
void BinaryTree<T>::Clear(BTNode<T> *x)

{
	if (!x) return; 
	BinaryTree<T> a,b;
	BTNode<T> *l=x->lChild,*r=x->rChild;       //指向root指向的结点的左右孩子
	BreakTree(x->element,a,b);
	Clear(l);
	Clear(r);
}

/*LevelOrder*/
template <class T>//存在小bug
void BinaryTree<T>::LevelOrder(void (*Visit)(T &x))
{
	LevelOrder(root);
}
template <class T>
void BinaryTree<T>::LevelOrder(BTNode<T> *t)
{
	SeqQueue<BTNode<T>*> Q(Size());
	if (!t)
	{
		cout<<"空树!"<<endl;
		return;
	}
	BTNode<T> * p;
	p = root;
	Q.EnQueue(p);
	Visit(p->element);
	if (p->lChild)
		Q.EnQueue(p->lChild);
	if (p->rChild)
		Q.EnQueue(p->rChild);
	while (!Q.IsEmpty())
	{
		Q.DeQueue();
		Q.Front(p);
		if (Q.GetFront() == 0)
			break;
		Visit(p->element);
		if (p->lChild)
			Q.EnQueue(p->lChild);
		if (p->rChild)
			Q.EnQueue(p->rChild);
	}
	Q.DeQueue();
	return;
}


//main
#ifndef btnode_h
#define btnode_h
#endif
#include"binary_tree.h"
void main()
{
	BinaryTree<char> a,b,x,y,z,newtree;
	y.MakeTree('Y',a,b);
	z.MakeTree('Z',a,b);
	a.MakeTree('A',y,z);
	x.MakeTree('X',a,b);cout<<"BinaryTree x:";x.PreOrder(Visit);cout<<endl;
	cout<<"BinaryTree x size:"<<x.Size()<<endl; 
	cout<<"newtree:";newtree.Copy(x);newtree.PreOrder(Visit);cout<<endl;
	cout<<"BinaryTree x height:"<<x.Height()<<endl;
	x.Exchange();cout<<"Exchanged BinaryTree x:";x.PreOrder(Visit);cout<<endl;
	cout<<"BinaryTree x LeafNum:"<<x.LeafNum()<<endl;
	cout<<"LevelOrder:"; x.LevelOrder(Visit);
	x.Clear();cout<<"BinaryTree x Clear :";x.PreOrder(Visit);cout<<endl;

}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值