2013年4月24日
摘要: 二叉树非递归遍历,可以采用堆栈实现,也可以采用非堆栈用指针实现。感觉堆栈实现很像递归实现,只是要自己把节点压入堆栈,弹出堆栈。非堆栈实现也有两种方法,可以在节点结构中添加标志位以显示当前节点是否已经被遍历过,也可以不用标志位,只用指针表示。这里实现了采用堆栈方式和非堆栈非标志位两种方法。View Code 1 #include <iostream> 2 #include <stack> 3 #include <ctime> 4 #include <random> 5 6 using namespace std; 7 8 template<c 阅读全文
posted @ 2013-04-24 15:14 blue firmament 阅读(190) 评论(0) 推荐(0) 编辑
  2013年4月15日
摘要: 题目:输入n个整数,输出其中最小的k个。例如输入1,2,3,4,5,6,7和8这8个数字,则最小的4个数字为1,2,3和4。题目地址:http://blog.csdn.net/v_july_v/article/details/5934051View Code 1 #include <iostream> 2 #include <vector> 3 #include <ctime> 4 #include <random> 5 #include <algorithm> 6 7 using namespace std; 8 9 10 /*** 阅读全文
posted @ 2013-04-15 16:15 blue firmament 阅读(130) 评论(0) 推荐(0) 编辑
  2013年4月12日
摘要: 题目:输入一个整数和一棵二元树。从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。打印出和与输入整数相等的所有路径。例如 输入整数22和如下二元树 10 / / 5 12 / / 4 7则打印出两条路径:10, 12和10, 5, 7。题目来源:http://blog.csdn.net/v_july_v/article/details/5934051View Code 1 #include <iostream> 2 #include <stack> 3 #include <vector> 4 5 using namespace std; 6 阅读全文
posted @ 2013-04-12 16:26 blue firmament 阅读(181) 评论(0) 推荐(0) 编辑
  2013年4月10日
摘要: 输入一个整形数组,数组里有正数也有负数。数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。求所有子数组的和的最大值。要求时间复杂度为O(n)。 1 #include <iostream> 2 #include <ctime> 3 #include <random> 4 5 using namespace std; 6 7 /************************************************* 8 Function: // maxSubArray 9 Description: // 求整数数组中子数组的最大和。这个1 阅读全文
posted @ 2013-04-10 15:20 blue firmament 阅读(148) 评论(0) 推荐(0) 编辑
  2013年4月9日
摘要: 定义栈的数据结构,要求添加一个min函数,能够得到栈的最小元素。要求函数min、push以及pop的时间复杂度都是O(1)。在栈中包含另一个记录在各元素压入堆栈之后的最小元素下标的数组就可以实现。 1 #include <iostream> 2 3 using namespace std; 4 5 //栈功能实现类 6 template<class T, int capacity> 7 class min_stack 8 { 9 private: 10 T m_data[capacity]; 11 int m_index[capacity];//保存栈中该... 阅读全文
posted @ 2013-04-09 15:58 blue firmament 阅读(140) 评论(0) 推荐(0) 编辑
  2013年4月8日
摘要: 采用中序遍历,将左子树中根节点的前驱节点和右子树中根节点的后继节点与根节点连接。 1 #include <iostream> 2 3 using namespace std; 4 5 template<class T> 6 struct tree_node 7 { 8 T data; 9 tree_node * left_child; 10 tree_node * right_child; 11 }; 12 13 /************************************************* 14 Function: ... 阅读全文
posted @ 2013-04-08 15:21 blue firmament 阅读(197) 评论(0) 推荐(0) 编辑
  2013年4月1日
摘要: 归并排序的主要步骤是对子问题的合并,算法具体步骤请参考算法导论。 1 #ifndef MERGE_SORT_H 2 #define MERGE_SORT_H 3 4 #include <iostream> 5 6 using namespace std; 7 8 /************************************************* 9 Function: // merg_sort 10 Description: // 归并排序实现 11 Input: // array:输入待排序数组 12 Input:... 阅读全文
posted @ 2013-04-01 16:37 blue firmament 阅读(118) 评论(0) 推荐(0) 编辑
  2013年3月28日
摘要: 算法导论上,第10章的一个练习,用一个数组实现存储两个栈,使的每个栈的push和pop的时间都是O(1) 1 #ifndef DOUBLESTACK_H 2 #define DOUBLESTACK_H 3 4 #define TOTAL 10 5 6 template<class T> 7 class double_stack 8 { 9 T m_array[20]; 10 int m_top1, m_end1; 11 int m_top2, m_end2; 12 public: 13 double_stack(); 14 voi... 阅读全文
posted @ 2013-03-28 11:21 blue firmament 阅读(356) 评论(0) 推荐(0) 编辑
  2013年3月27日
摘要: 算法说明请参考算法导论 1 #ifndef COUNTSORT_H 2 #define COUNTSORT_H 3 4 #include <iostream> 5 6 using namespace std; 7 8 /************************************************* 9 Function: // count_sort10 Description: // 计数排序11 Input: // array_a: 输入数组12 // size: 数组元素个数13 ... 阅读全文
posted @ 2013-03-27 14:28 blue firmament 阅读(129) 评论(0) 推荐(0) 编辑
  2013年3月25日
摘要: 算法具体解释请参考算法导论 1 #ifndef QUICKSORT_H 2 #define QUICKSORT_H 3 4 #include <iostream> 5 6 using namespace std; 7 8 9 /*************************************************10 Function: // quick_sort11 Description: // 快速排序实现12 Input: // array: 输入数组13 // begin: 数组待排序前端元素下... 阅读全文
posted @ 2013-03-25 16:39 blue firmament 阅读(131) 评论(0) 推荐(0) 编辑