南邮数据结构实验2 (1)二叉树基本操作

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

1.设计递归算法,实现下列二叉树运算:删除一棵二叉树,求一棵二叉树的高度,求一棵二叉树中叶子结点数,复制一棵二叉树,交换一棵二叉树的左右子树。

2.设计算法,按自上到下,自左向右的次序,即按层次遍历一棵二叉树。

3.设计main函数,测试上述每个运算。

4.提示:二叉树的按层次遍历需要利用队列作为辅助的数据结构,队列的元素类型是指向二叉树中结点的指针类型。


#include <iostream>
#include <string>
using namespace std;

template<class T>
struct BTNode   //结点类
{
	BTNode(){lChild = rChild = NULL;}
	BTNode(const T& x)
	{
		element = x;
		lChild = rChild = NULL;
	}
	BTNode(const T&x, BTNode<T>*l, BTNode<T>*r)
	{
		element = x;
		lChild = l;
		rChild = r;
	}
	T element;
	BTNode<T> *lChild, *rChild;
};

template<class T>         //循环队列类
class SeqQueue
{
public:
	SeqQueue(int mSize);
	~SeqQueue(){delete []q;}
	bool IsEmpty() const{return front == rear;}
	bool IsFull() const{return (rear + 1) % maxSize == front;}
	bool Front(BTNode<T> *&x)const;
	bool EnQueue(BTNode<T> *x);
	bool DeQueue();
	void Clear(){front = rear = 0;}
private:
	int front, rear;
	int maxSize;
	BTNode<T> **q;
};

template<class T>
SeqQueue<T>::SeqQueue(int mSize)   //构造函数
{
	maxSize = mSize;
	q = new BTNode<T>*[maxSize];
	front = rear = 0;
}
template<class T>
bool SeqQueue<T>::Front(BTNode<T> *&x)const    //取队头元素
{
	if(IsEmpty())
		return false;
	x = q[(front+1) % maxSize];
	return true;
}

template<class T>
bool SeqQueue<T>::EnQueue(BTNode<T> *x)    //在队尾插入x
{
	if(IsFull())
	{
		cout << "Full" << endl;
		return false;
	}
	q[rear = (rear+1) % maxSize] = x;
	return true;
}

template<class T>
bool SeqQueue<T>::DeQueue()   //删除队头元素
{
	if(IsEmpty())
	{
		cout << "Underflow" << endl;
		return false;
	}
	front = (front+1) % maxSize;
	return true;
}
template<class T>      //Visit函数
void Visit(T&x)
{
    cout << x << " ";
}
template<class T>   //二叉树类
class BinaryTree
{
public:
	BinaryTree():s(100){root = NULL;}
	~BinaryTree(){delete []root;}
	bool Clear();
	void MakeTree(const T&x,BinaryTree<T>&left,BinaryTree<T>& right);
	int High(BTNode<T>*p);
	int Node_num(BTNode<T>*p);
    BTNode<T>*Copy (BTNode<T>*t);
	void Exchange(BTNode<T> *&t);
	void Level_traversal(void(*Visit)(T&x));
	BTNode<T>*root;
protected:
	SeqQueue<T> s;
private:
	void Clear(BTNode<T>* &t);
	void Level_traversal(void(*Visit)(T&x),BTNode<T>*t);
};

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;
}

template<class T>
bool BinaryTree<T>::Clear()   //删除
{
	Clear(root);
	if(root == NULL)
		return true;
	else
		return false;
}

template<class T>
void BinaryTree<T>::Clear(BTNode<T>*& tmp)  //删除
{
	if(tmp)
	{
		Clear(tmp -> lChild);
		Clear(tmp -> rChild);
		delete tmp;
		tmp = NULL;
	}
}

template<class T>
int BinaryTree<T>::High(BTNode<T>*p)  //高度
{
	if(p == NULL)
        return 0;
	else if(p -> lChild == NULL && p -> rChild == NULL)
        return 1;
	else
        return (High(p -> lChild) > High(p -> rChild) ? High(p -> lChild) + 1 : High(p -> rChild) + 1);
}

template<class T>
int BinaryTree<T>::Node_num(BTNode<T>*p)   //叶子结点
{
	if(p)
	{
		if(p -> lChild == NULL && p -> rChild == NULL)
		    return 1;
	    else
			return Node_num(p -> lChild) + Node_num(p -> rChild);
	}
	else
        return 0;
}

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;
}
template<class T>
void BinaryTree<T>::Exchange(BTNode<T>*&t)    //左右子树交换
{
	if(t)
	{
		BTNode<T>*q = t -> lChild;
		t -> lChild = t->rChild;
		t -> rChild = q;
		Exchange(t -> lChild);
		Exchange(t -> rChild);
	}
}

template<class T>
void BinaryTree<T>::Level_traversal(void(*Visit)(T&x))    //层次遍历
{
	Level_traversal(Visit, root);
	cout << endl;
}

template<class T>
void BinaryTree<T>::Level_traversal(void(*Visit)(T&x),BTNode<T>*t)   //层次遍历
{
	BTNode<T> *a;
	Visit(t -> element);
	if(t -> lChild)
		s.EnQueue(t -> lChild);
	if(t -> rChild)
		s.EnQueue(t -> rChild);
	while(s.Front(a) == true)
	{
		if(a -> lChild)
		    s.EnQueue(a -> lChild);
	    if(a -> rChild)
		    s.EnQueue(a -> rChild);
		Visit(a -> element);
		s.DeQueue();
	}
}
int main()
{
	BinaryTree<char>t[100], a, b, temp;
	int num = 0, high = 0;
	t[7].MakeTree('H', a, b);
	t[8].MakeTree('I', a, b);
	t[3].MakeTree('D', t[7], t[8]);
	t[4].MakeTree('E', a, b);
	t[5].MakeTree('F', a, b);
	t[6].MakeTree('G', a, b);
	t[1].MakeTree('B', t[3], t[4]);
	t[2].MakeTree('C', t[5], t[6]);
	t[0].MakeTree('A', t[1], t[2]);
	cout << "二叉树z的层次遍历结果:\n";
	t[0].Level_traversal(Visit);
	temp.root = temp.Copy(t[0].root);
	cout<<"由二叉树z复制的二叉树temp的层次遍历结果:\n";
	temp.Level_traversal(Visit);
	t[0].Exchange(t[0].root);
	cout<<"交换左右子树后的二叉树z的层次遍历结果:\n";
	t[0].Level_traversal(Visit);
	num = t[0].Node_num(t[0].root);
	cout<<"二叉树z的叶子结点数为:\n"<< num << "\n";
	high = t[0].High(t[0].root);
	cout<<"二叉树z的高度为:\n" << high << "\n";
	cout<<"删除二叉树z: 成功输出1, 失败输出0\n" << t[0].Clear() << "\n";
	return 0;
}




  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值