数据结构实验--------二叉树(Binary Tree)

        使用的教材是电子工业出版社出版的《Data Structures and Algorithm Analysis in C++  》(《数据结构与算法分析(C++)》(第三版)),作者是【美】Clifford A Shaffer,译者是张铭、刘晓丹等。所以,有些代码与书上给出来的几近相同,如有侵权,请联系本人(741239458@qq.com),我会删除文章的。


        实验内容:1、编写递归函数search, 传入参数为一棵二叉树和一个值k, 如果值k 出现在树中则返回true, 否则返回flase。

                             2、编写递归函数返回二叉树的高度。

                             3、编写递归函数计算二叉树的叶结点数。

                            4、按广度优先搜索树。

------------BSTNode.h----------------

//节点的ADT
template<typename E>
class BinNode
{
public:
    virtual ~BinNode() {}
	virtual E& element() =0 ;
	virtual void setElement(const E& ) =0;
	virtual BinNode* left() const = 0;
	virtual void setLeft(BinNode* ) =0;
	virtual BinNode* right() const =0;
	virtual void setRight(BinNode*) = 0;
	virtual bool isLeaf() = 0;
};

template<typename E>
class BSTNode:public BinNode<E>
{
private:
    char k;
	E it;
	BSTNode* lc;
	BSTNode* rc;
	
public:
    BSTNode()
	{
	    lc = rc = NULL;
	}
	
	BSTNode(char temp, E e, BSTNode* l=NULL, BSTNode* r=NULL)
	{
	    k = temp;
		it = e;
		lc = l;
		rc = r;
	}
	
	~BSTNode()  {}
	
	E& element()
	{
	    return it;
	}
	
	void setElement(const E& e)
	{
	    it = e;
	}
	
	char& key() { return k; }
	
	void setKey(const char& temp )  
	{
	    k = temp;
	}
	
	inline BSTNode* left() const
	{
	    return lc;
	}
	
	void setLeft(BinNode<E>* b)
	{
	    lc = (BSTNode*) b;
	}
	
	inline BSTNode* right() const
	{
	    return rc;
	}
	
	void setRight(BinNode<E>* b)
	{
	    rc = (BSTNode*) b;
	}
	
	bool isLeaf()
	{
	    return lc==NULL&&rc==NULL;
	}
};

//队列的ADT
template<typename E>
class Queue
{
	void operator = (const Queue&) {}
	Queue(const Queue&) {}
public:
	Queue() {}
	virtual ~Queue() {}
	virtual void clear() = 0;
	virtual void enqueue(BSTNode<E>*) = 0;
	virtual BSTNode<E>* dequeue() = 0;
	virtual const BSTNode<E>* frontValue() const = 0;
	virtual  int length() const = 0;
};


template<typename E>
class Knot
{
public:
	BSTNode<E>* value;
	Knot* next;
	Knot( BSTNode<E>* v = NULL ,Knot* n = NULL)
	{
		value = v;
		next = n;
	}
};

template<typename E>
class Lqueue : public Queue<E>
{
	int size;
	Knot<E>* front;
	Knot<E>* rear;

public :
	Lqueue()  { front = rear = new Knot<E>(); size = 0; }
	~Lqueue() { clear();  delete front; }
	void clear()
	{
		while(front->next != NULL)
		{
			rear = front->next;
			delete rear;
		}
		rear = front;
		size = 0;
	}
	void enqueue( BSTNode<E>* it)
	{
		rear->next = new Knot<E>(it,NULL);
		rear = rear->next;
		size++;
	}
	BSTNode<E>* dequeue()
	{
		if(size==0)
			return NULL;
		BSTNode<E>* it = front->next->value;
		Knot<E>* temp = front->next;
		front->next = temp->next;
		size--;
		if(size==0)
			rear = front;
		delete temp;
		temp = NULL;
		return it;
	}

	const BSTNode<E>* frontValue() const
	{
		return front->next->value;
	}

	int length() const
	{
		return size;
	}
};


------------Main.cpp----------

#include<iostream>
#include"BSTNode.h"
using namespace std;

//二叉树进行搜索
template<typename E>
bool search(BSTNode<E>* root,char temp)
{
    if(root==NULL) return false;
	if(root->key()==temp)
	    return true;
	if(search(root->left(),temp))
	    return true;
	return search(root->right(),temp);
}

//返回树的高度
template<typename E>
int getHight(BSTNode<E>* root)
{
	int h = 0,h1 = 0;
	if(root == NULL)
		return h;
	else
	{
		h1 = h = 1;
	}
	h += getHight(root->left());
	h1 += getHight(root->right());
	return h > h1? h : h1;
}

//返回叶子数
template<typename E>
int getNumOfLeaves(BSTNode<E>* root)
{
	if(root == NULL)
		return 1;
	return getNumOfLeaves(root->left())+getNumOfLeaves(root->right());
}

//build the tree
/*         A(0)
     -----/  \-----
	 |            |
	 B(1)         C(2)
----/\-----   ----/\----
|         |   |       |
D(3)     E(4) F(5)    G(6)  */
template<typename E>
void asign(BSTNode<E>*& root)
{
	root = new BSTNode<int>('A',0);
	root->setLeft(new BSTNode<int>('B',1));
	root->setRight(new BSTNode<int>('C',2));
	BSTNode<E>* temp = root->left();
	temp->setLeft(new BSTNode<int>('D',3));
	temp->setRight(new BSTNode<int>('E',4));
	temp = root->right();
	temp->setLeft(new BSTNode<int>('F',5));
	temp->setRight(new BSTNode<int>('G',6));
}

//广度搜索二叉树
template<typename E>
void breadthFirstSearch(BSTNode<E>*& root)
{
	Lqueue<E> lq;
	lq.enqueue(root);

	//记录树遍历的层数,用于决定什么时候换行
	int h = getHight(root);
	int* level = new int[h];
	int i = 0;
	for(;i<h;i++)
		level[i] = 0;
	i = 0;
	level[i]++;

	while(lq.length()>0)
	{
		BSTNode<E>* temp = lq.dequeue();
		level[i]--;
		if(temp->left()!=NULL)
		{
			lq.enqueue(temp->left());
			level[i+1]++;
		}
		if(temp->right()!=NULL)
		{
			lq.enqueue(temp->right());
			level[i+1]++;
		}
		cout<<temp->key()<<"     ";
		if(level[i]==0)
		{
			i++;
			cout<<endl;
		}
	}
}

int main()
{
	BSTNode<int>* root = NULL;
	asign<int>(root);
	cout<<"search 'E'  is :";
	if(search(root, 'E'))
		cout<<"true"<<endl;
	else
		cout<<"false"<<endl;
	
	cout<<"search 'J'  is :";
	if(search(root, 'J'))
		cout<<"true"<<endl;
	else
		cout<<"false"<<endl;

	cout<<"The hight of the tree is :"<<getHight<int>(root)<<endl;

	cout<<"The count of the leaf is :"<<getNumOfLeaves<int>(root)<<endl;

	cout<<"Breadth first search tree is:\n";
	breadthFirstSearch<int>(root);

	system("PAUSE");
}


          运行截图:
       


       



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值