(2011.08.13)二叉查找树类例子

  一共买了两本数据结构的书,之前那本的二叉查找树的例子看得不太懂,今天,一看这本书的例子,忽然恍然大悟,原来这个知识也不是很难,只是看你适不适应用递归。原来啊,两本书还是结合着用好,有些例子这本书写得好,有些就是那本书写得好。
  下面是二叉查找树类的例子,有些注释自己加上去了,总算是弄懂了。


// 416 二叉查找树类 Binary Search Tree
template <typename Compareable>
class BinarySearchTree
{
public:
	BinarySearchTree();
	BinarySearchTree(const BinarySearchTree & rhs);
	~BinarySearchTree();

	const Comparable & findMin() const;
	const Comparable & findMax() const;
	bool contains(const Comparable & x) const;
	bool isempty() const;
	void printTree() const;

	void makeEmpty();
	void insert( const Comparable & x);
	void remove(const Comparable& x);

	const BinarySearchTree & operator = ( const BinarySearchTree & rhs);

private:
	struct BinaryNode
	{
		Comparable element;
		BinaryNode *left;
		BinaryNode *right;

		BinaryNode(const Comparable & theElement, BinaryNode * lt, BinaryNode *rt)
			: element(theElement), left(lt), right(rt){}
	};

	BinaryNode *root;

	void insert (const comparable & x, BinaryNode * & t) const;
	void remove(const Comparable & x, BinaryNode * & t)const;
	BinaryNode * findMax(BinaryNode *t) const;
	BinaryNode * findMin(BinaryNode *t) const;
	bool contains (const Comparable & x, BinaryNode * t) const;
	void makeEmpty(BinaryNode * & t);
	void printTree(BinaryNode * t) const;
	BinaryNode * clone(Binary * t) const;
};

// 以下定义全部省略 BinarySearchTree <Compareable>::


// Returns true if x is found in the tree.
bool contains (const Comparable & x) const
{
	return contains(x, root);
}
/**
 * Internal method to test if an item is in a subtree.
 * x is item to search for. 
 * t is the node that roots the subtree.
 */
 bool contains(const Comparable & x, BinaryNode * t) const
 {
	 if ( t == NULL)					// 空表
		 return false;
	 else if ( x < t -> element)	
		 return contains(x, t -> left);
	 else if ( t -> element < x)
		 return contains(x, t -> right);
	 else 
		 return true;				// Match
 }


// 这里的findMin使用递归实现,而findMax使用循环实现

/**
 * Internal method to find the smallest item in a subtree t.
 * Return node containing the smallest item.
 */
BinaryNode * findMin(BinaryNode * t) const
{
	if (t == NULL)							// 空表
		return NULL;
	if ( t -> left == NULL)	
		return t;
	return findMin(t -> left);
}

/**
 * Internal method to find the largest item in a subtree t
 * Return node containing the largest item.
 */
BinaryNode * findMax(BinaryNode * t) const
{
	if ( t != NULL)
		while ( t -> right != NULL)
			t = t - > right;
	return t;
}

// Insert x into the tree; duplicates are ignored.
void insert(const Comparable & x)
{
	insert(x, root);
}
/**
 * Internal method to insert into a subtree.
 * x is the item to insert.
 * t is the node that roots the subtree
 * Set the new root of the subtree.
 */ 
 void insert( const Comparable & x, BinartyTreeNode * & t)
 {
	 if ( t == NULL)						// 基准条件,找到位置后插入
		 t =  new BinaryNode( x, NULL, NULL);
	 else if (x < t-> element)	// 比较大小,找到合适的位置
		 insert(x, t -> left);
	 else if (t -> element < x)	
		 insert(x, t -> right);
	 else
		 ;	// Duplicate; do nothing
 }
 
// Remove x from the tree. Nothing is done if x is not found. 
void remove(const Comparable & x)
{
	remove(x, root);
}
// 同很多数据结构一样,最困难的操作是删除
/**
 * Internal method to remove from a subtree.
 * x is the item to remove.
 * t is the node that roots the subtree.
 * Set the new root fo the subtree.
 */
void remove (const Comparable & x, BinaryNode * & t)
{
	if (t == NULL)	// 空表 
		return ;			// Item not found; do nothing
	if ( x < t -> element)
		remove (x, t -> left);
	else if ( t -> element < x)
		remove (x, t -> right);
	// 以下的这两个情况,都是已经找到跟x相等的元素的了
	else if ( t -> left != NULL && t -> right != NULL)	// two children
	{
		// 找出在x元素的结点以下的那些结点较小的一个作为元素替换
		t -> element  = findMax(t -> right) -> element;
		// 替换工作完成后,可以继续实行递归,对其结点进行删除
		// 这里将t指向的元素,替换后的元素作为实参
		remove( t -> element, t -> right);
		// 将t -> right指针移至最大值的位置,下次调用函数时,t即为最大值的指针
	}
	else
	{
		BinaryNode * oldNode = t;
		t = ( ( t -> left != NULL) ? t -> left : t -> right) ; 
		delete oldNode;
	}
}

/**
 * Destructor for the tree
 */
~BinarySearchTree()
{
	makeEmpty();
}
/**
 * Internal method to make subtree empty.
 */
void makeEmpty(BinaryNode * & t)
{
	if ( t != NULL );
	{
		makeEmpty( t -> left);
		makeEmpty( t -> right);
		delete t;
	}
	t = NULL;
}

/**
 * Deep copy. 
 */
 const BinarySearchTree & operator = ( const BinarySearchTree & rhs)
 {
	 if (this != &rhs)
	 {
		 makeEmpty();
		 root = clone (rhs.root);
	 }
	 return *this;
 }
/**
 * Internal method to clone subtree.
 */
 BinaryNode * clone( BinaryNode *t ) const
 {
	 if ( t == NULL)
		 return NULL;

	return new BinaryNode(t -> element, clone (t -> left), clone (t -> right) );
 }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值