摘要: 我们在实现二叉搜索树的先中后序遍历是一般的都是递归的来实现的,现在我们要实现其非递归的版本,核心思想是利用栈来模拟递归中的函数调用,自己来实现模拟递归我们先来实现先序遍历————————————void PreOrderNotRecursion(BinarySearchTree bst)//二叉查找数的非递归版先序遍历{ std::stack s; s.push(bst); while(!s.empty()) { Position pos = s.top(); s.pop(); std::coutdatari... 阅读全文
posted @ 2013-08-01 22:07 老司机 阅读(729) 评论(0) 推荐(0) 编辑
摘要: 二叉平衡查找树即是一棵树中所有节点的左右子树高度差不超过1的查找树头文件——————————————————————————————#ifndef _AVLTREE_H_#define _AVLTREE_H_#include #include #include typedef struct AvlNode *Position;typedef Position AvlTree;#define Element intstruct AvlNode{ Element data; int height;//叶子节点高度定义为0,其父节点为1以此类推 AvlTree left; ... 阅读全文
posted @ 2013-07-29 09:57 老司机 阅读(489) 评论(0) 推荐(0) 编辑
摘要: 头文件——————————————————————————————#ifndef _BINARY_SEARCH_TREE_H_#define _BINARY_SEARCH_TREE_H_#include #include #include #include #include #define Element intstruct TreeNode{ Element data; struct TreeNode *left; struct TreeNode *right;};typedef struct TreeNode *Position;typedef ... 阅读全文
posted @ 2013-07-26 13:13 老司机 阅读(389) 评论(0) 推荐(0) 编辑
摘要: (1)使用set/multiset之前必须包含头文件:#include(2)namespace std{ template , class Allocator = allocator > class set; template , class Allocator = allocator > class multiset; }只要是assignable、copyable、comparable的型别T都可以成为set或multiset的元素型别。set/multiset的排序准则必须是st... 阅读全文
posted @ 2013-07-23 23:31 老司机 阅读(702) 评论(0) 推荐(1) 编辑
摘要: //求一个数列的最大子序列和#include #include int MaxSubsequenceSum(const std::vector& ivec){ int ThisSum = 0, MaxSum = 0; for(std::vector::const_iterator it = ivec.begin(); it != ivec.end(); ++it) { ThisSum += *it; if(ThisSum MaxSum) MaxSum = ThisSum; } ... 阅读全文
posted @ 2013-07-22 09:28 老司机 阅读(340) 评论(0) 推荐(0) 编辑
摘要: 头文件——————————————————————————————#ifndef _QUEUE_H_#define _QUEUE_H_#include #define Element intstruct node{ Element data; struct node *next;};typedef struct node *PtrToNode;typedef struct _Queue{ PtrToNode head; PtrToNode rear;} *Queue;//头进尾出int IsEmpty(Queue q);Queue CreateQueue();v... 阅读全文
posted @ 2013-07-20 20:07 老司机 阅读(892) 评论(0) 推荐(1) 编辑
摘要: 头文件——————————————————————————#ifndef _STACK_H_#define _STACK_H_#include #define Element intstruct node{ Element data; struct node *next;};typedef struct node *PtrToNode;typedef PtrToNode Stack;int IsEmpty(Stack s);Stack CreateStack();void DestroyStack(Stack s);void MakeEmpty(Stack s);void Pu... 阅读全文
posted @ 2013-07-20 18:40 老司机 阅读(285) 评论(0) 推荐(1) 编辑
摘要: /*题目:给定只包含正整数的数组,给出一个方法,将数组中的数拼接起来,得到的数是最大的。例如[4,94,9,14,1]拼接之后所得的最大数为:9944141*/#include #include using namespace std;void swap(int *a, int *b){ int tmp = *a; *a = *b; *b = tmp;}int get_digit(int x)//得到数x的位数,个位返回1,十位返回2,以此类推{ int n = 0; while(x % 10) { ++n; ... 阅读全文
posted @ 2013-07-19 22:21 老司机 阅读(361) 评论(0) 推荐(0) 编辑
摘要: 大小为N的数组A,其主要元素是一个出现次数超过N/2的元素(从而这样的元素只有一个或者不存在)算法:首先找出主要元素的一个候选元(难点)。这个候选元是唯一有可能是主要元素的元素。第二步确定是否这个候选元是主要元素。为了找出候选元,构造第二个数组B。比较A1和A2,如果它们相等则取其中之一加到数组B中;否则什么都不做;然后比较A3和A4,按同样的方式处理,其次类推直到读完这个数组,然后递归的寻找数组B中的候选元,它也是A的候选元。(PS:可以用A来代替B的作用从而避免使用附加数组)下面我的算法是使用了数组A来代替数组B//找出一个数组的主要元素的候选元,寻找过程中会改变数组的元素位置#inclu 阅读全文
posted @ 2013-07-18 13:22 老司机 阅读(818) 评论(2) 推荐(0) 编辑
摘要: (1)为了运用stack,你必须包含头文件:#include(2)在头文件中stack定义如下:namespace std{ template > class stack;}第一个template参数代表元素型别,带有默认值的第二个参数用来定义stack内部存放元素所用的实际容器,缺省采用deque。之所以采用deque而非vector,是因为deque移除元素时会释放内存并且不必在重新分配时复制全部元素。实际上stack只是很单纯的把各项操作转化为内部容器的对应调用,你可以使用任何序列式容器来支持stack,只要它们支持back(),push_back(),pop_back... 阅读全文
posted @ 2013-07-17 15:27 老司机 阅读(1382) 评论(0) 推荐(0) 编辑