回溯法学习---求集合的幂集
2010-06-03 22:59
281 查看
回溯法也是设计递归过程的一种重要方法,它的求解过程实际上是先序遍历一棵“状态树”的过程,只是这棵树不是遍历前预先建立的,而是隐含在遍历过程中。
如果意识到这一点,很多递归过程的设计也就迎刃而解了。
《数据结构》书中有道题:求集合A={1,2,3}的幂集。
测试结果:
如果意识到这一点,很多递归过程的设计也就迎刃而解了。
《数据结构》书中有道题:求集合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
相关文章推荐
- C语言 回溯的应用 ---- 输出集合{1,2,...n}的幂集
- C语言 回溯---转非递归---- 输出集合{1,2,...n}的幂集
- 每日一省之———— 递归 + 回溯 求集合的幂集
- 回溯法之求n个集合的幂集
- 回溯法求集合的幂集
- nhibernate学习之集合组合依赖
- Kotlin学习(二)—— 基本语法,函数,变量,字符串模板,条件表达式,null,类型检测,for,while,when,区间,集合
- java学习第36天,list集合
- 深度学习论文资料集合
- 深入Java集合学习系列:HashMap的实现原理
- 黑马程序员--Java学习日记之集合(map集合和collections类)
- 深入Java集合学习系列:WeakHashMap的实现原理
- 深度学习与图像修复常用资源集合(整理中)
- 求集合的幂集【转】
- java基础学习笔记——集合框架
- 机器学习中,什么是子空间集合学习?
- C#3.0学习(2)---对象集合初始化器
- 我的安卓学习之路--java集合
- 学习kotlin第13天_具体化的类型参数、内联属性、解构声明、集合
- spring学习教程9-spring中关于集合的配置方式