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

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

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

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

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

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


#include 
#include 
using namespace std;

template
struct BTNode   //结点类
{
	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;
	}
	T element;
	BTNode *lChild, *rChild;
};

template         //循环队列类
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 *&x)const;
	bool EnQueue(BTNode *x);
	bool DeQueue();
	void Clear(){front = rear = 0;}
private:
	int front, rear;
	int maxSize;
	BTNode **q;
};

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

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

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

template
void BinaryTree::MakeTree(const T&x,BinaryTree&left,BinaryTree& right)   //建树
{
	if(root || &left == &right) return;
	root = new BTNode(x,left.root,right.root);
	left.root = right.root = NULL;
}

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

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

template
int BinaryTree::High(BTNode*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
int BinaryTree::Node_num(BTNode*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
BTNode*BinaryTree::Copy(BTNode*t)   //复制
{
	if(!t) return NULL;
	BTNode*q = new BTNode(t -> element);
	q -> lChild = Copy(t -> lChild);
	q -> rChild = Copy(t -> rChild);
	return q;
}
template
void BinaryTree::Exchange(BTNode*&t)    //左右子树交换
{
	if(t)
	{
		BTNode*q = t -> lChild;
		t -> lChild = t->rChild;
		t -> rChild = q;
		Exchange(t -> lChild);
		Exchange(t -> rChild);
	}
}

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

template
void BinaryTree::Level_traversal(void(*Visit)(T&x),BTNode*t)   //层次遍历
{
	BTNode *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()
{
	BinaryTreet[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;
}




你可能感兴趣的:(南邮课程实验)