您的位置:首页 > 其它

回溯法学习---求集合的幂集

2010-06-03 22:59 281 查看
回溯法也是设计递归过程的一种重要方法,它的求解过程实际上是先序遍历一棵“状态树”的过程,只是这棵树不是遍历前预先建立的,而是隐含在遍历过程中。

如果意识到这一点,很多递归过程的设计也就迎刃而解了。

《数据结构》书中有道题:求集合A={1,2,3}的幂集。

//-----------------------------------------------------------------------------
//	Copyright (c) 2010 eryar All rights reserved.
//
//		File	:	List.h
//		Author	:	eryar@163.com
//		Date	:	2010-6-1 23:05
//		Version	:	1.0v
//
//	Description	:	Use class template to implemente the Link list.
//
//=============================================================================

#ifndef _LIST_H_
#define _LIST_H_

#include <iostream>
using namespace std;

template<typename T>
struct Node{
T	data;
Node*	pNext;
};

// class CList declaration
template<typename T>
class	CList{
public:
CList();
int	length();
bool	insert(int iPos, T elem);
bool	remove(int iPos, T& elem);
bool	get(int iPos, T& elem);
bool	assign(int iPos, T& elem);
bool	isEmpty();
void	output();
~CList();
private:
Node<T>*	head;
};

// class CList definition
template<typename T>
CList<T>::CList() {
head		=	new	Node<T>;
head->pNext	=	NULL;

//cout<<"[class template class constructor]"<<endl;
} // default constructor

template<typename T>
bool	CList<T>::insert(int iPos, T elem) {
int	i		=	0;
Node<T>*	p	=	head;
Node<T>*	q	=	new	Node<T>;

// search for the destination position
while(p && i < iPos - 1) {
p	=	p->pNext;
++i;
}

// less than or more than list length
if (!p || i > iPos - 1) {
return false;
}

// insert the element to the position
q->data		=	elem;
q->pNext	=	p->pNext;
p->pNext	=	q;

return true;
} // insert an element

template<typename T>
bool	CList<T>::remove(int iPos, T& elem) {
int	i	=	0;
Node<T>*	p	=	head;
Node<T>*	q	=	new	Node<T>;

// search for the destination position
while(p->pNext && i < iPos - 1) {
p	=	p->pNext;
++i;
}

// the position is not right
if (!p->pNext || i > iPos - 1) {
return false;
}

// do delete action
q		=	p->pNext;
p->pNext	=	q->pNext;
elem		=	q->data;

return true;
} // delete an element

template <typename T>
bool	CList<T>::assign(int iPos, T& elem) {
return true;
} // assign a value

template <typename T>
bool	CList<T>::get(int iPos, T& elem) {
int	i		=	0;
Node<T>*	p	=	head->pNext;

if (iPos < 1 || iPos > length()) {
// wrong position in get() function...
return false;
}

// move to the position
while (p && i < iPos - 1) {
p	=	p->pNext;
++i;
}

if (!p || i > iPos - 1) {
// less than or more than list length...
return false;
}

elem	=	p->data;

return true;
} // get an element form iPos

template <typename T>
int	CList<T>::length() {
Node<T>*	p	=	head->pNext;
int		iLen	=	0;

while (p) {
p	=	p->pNext;
++iLen;
}

return iLen;
} // get list length

template <typename T>
bool	CList<T>::isEmpty() {
if (length()) {
return false;
}
else {
return true;
}

} // if list is empty return true

template <typename T>
void	CList<T>::output() {
Node<T>*	p	=	head->pNext;

while(p) {
cout<<p->data;

p	=	p->pNext;
}

cout<<endl;

} //

template<typename T>
CList<T>::~CList() {
//delete	head;

//cout<<"[class template class destructor ]"<<endl;
} // default destructor

#endif // _LIST_H_


//-----------------------------------------------------------------------------
//	Copyright (c) 2010 eryar All rights reserved.
//
//		File	:	main.cpp
//		Author	:	eryar@163.com
//		Date	:	2010-6-1 22:41
//		Version	:	1.0v
//
//	Description	:	用回溯法求集合的幂集。A={1,2,3}
//
//=============================================================================

#include "List.h"

void	GetPowerSet(int i, CList<int> set, CList<int>& list);

int main(int argc, char *argv[])
{
int		i;
CList<int>	set;
CList<int>	list;

for (i = 3; i > 0; --i) {
set.insert(1, i);
}

GetPowerSet(1, set, list);

return 0;
}

//
void	GetPowerSet(int i, CList<int> set, CList<int>& list) {
int	k;
int	elem;

if (i > set.length()) {
list.output();
}
else {
set.get(i, elem);
k	=	list.length();

// add
list.insert(k+1, elem);
GetPowerSet(i+1, set, list);

// remove
list.remove(k+1, elem);
GetPowerSet(i+1, set, list);
}

} // GetPowerSet


测试结果:

123
12
13
1
23
2
3

Press any key to continue
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: