二叉树 STL 图 哈希 详解
来源:互联网 发布:php微信分销开源系统 编辑:程序博客网 时间:2024/06/05 16:03
Ubuntu Pastebin
Paste from QWbin at Wed, 26 Jul 2017 10:36:33 +0000
Download as textDownload as text
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
已知 前序和中序遍历 求后序遍历序列struct node *creat(char *a, char *b, int n) { struct node *ptr; char *p; int count = 0; if (n <= 0) return NULL; ptr = (struct node *)malloc(sizeof(struct node)); ptr -> data = *a; for (p = &b[0]; p <= b + n - 1;p ++) { if (*p == *a) break; } count = p - b; ptr -> l = creat(a + 1, b, count); ptr -> r = creat(a + 1 + count, p + 1, n - 1 - count); return ptr; }已知中序和后序遍历 求前序遍历序列struct node *creat(char *a, char *b, int n) { struct node *ptr; if (n <= 0) return NULL; ptr = (struct node *)malloc(sizeof(struct node)); ptr -> data = b[n - 1]; int q = strchr(a, b[n - 1]) - a; ptr -> l = creat(a, b, q); ptr -> r = creat(a + q + 1, b + q, n - q - 1); return ptr; }-------------------------------------------------------------------------//搜索函数//————递归struct node *Find(int x, struct node *t) { if (t != NULL) return NULL; // 没有找到x if (x > t -> data) return Find(x, t -> right); //右子树寻找 else if (x < t -> data) return Find(x, t -> left); //左子树寻找 else if (x == t -> data) return t; //找到 x//搜索函数//——————迭代struct node *Find(int x, struct node *t) { while(t) { if (x > t -> data) t = t -> right; //右子树寻找 else if (x < t -> data) t = t -> left; //左子树寻找 else if (x == t -> data) return t; //找到 x } return NULL; // 没有找到x//查找最小元素//——————递归struct node *findmin(x, struct node *t) { if (t == NULL) return NULL; else if (t -> left == NULL) return t; else return findmin(x, t -> left); }//查找最小元素//——————迭代struct node *findmin(x, struct node *t) { if (t != BULL) { while(t -> left != NULL) t = t -> left; } return t; }//查找最大元素//————递归struct node *findmax(x, struct node *t) { if (t == NULL) return NULL; else if (t -> right == NULL) return t; else return findmin(x, t -> right); }//查找最大元素//————迭代struct node *findmin(x, struct node *t) { if (t != BULL) { while(t -> right != NULL) t = t -> right; } return t; }//插入函数//struct node *Insert (int x, struct node *t) { if (t == NULL) //进行插入操作// { t = (struct node *)malloc(sizeof(struct node)); t -> data = x; t -> left = NULL; t -> right = NULL; } else if (x < t -> data) t -> left = Insert(x, t -> left); else if (x > t -> data) t -> right = Insert(x, t -> right); return t; }-------------------------------------------------------------------------陆续的题目以及部分没有写的内容将会在近期补上,初学者请多指教~~直接进入正题吧STL主要分为:①非修改操作 ②非修改操作;主要的头文件有:#include<algorithm> // 算法头文件#include<functional>#include<vector>#include<queue>#include<set>#include<map>一、非修改操作: //find() 寻找x出现的位置 //count() 寻找出现次数 eg:int main() { int i; int a[5] = {1, 3 , 2 , 1 , 3}; //非修改操作 //find() 寻找 //count() 寻找出现次数 int ans = find(a, a + n, 3) - a; // 访问范围[0, n), 寻找5 返回地址 , 得到下标, 复杂度 O(n); int cnt = count(a, a + n, 3); // 访问范围【0, n) 寻找3出现的次数; printf("%d\n", ans); printf("%d\n", cnt); return 0; }二、修改操作 //swap() 交换两个相同类型的变量 //reverse() 反转数组 //unique() 数组去重 //fill()* 以指定值填充数组 //copy()* 拷贝数组 eg: int A = 5, B = -5; int a[5] = {1, 3 , 2 , 1 , 3}; swap(A, B); //交换A B reverse(a, a + 5); //反转 for (i = 0;i <= 4;i ++) { printf("%d ",a[i]); } printf("\n"); int newlen = unique(a, a + 5) - a; //去重 for (i = 0;i <= newlen - 1;i ++) { printf("%d ",a[i]); } printf("\n"); printf("%d %d\n", A, B);排序 //sort() 排序 //stable_sort* 稳定排序 ————保证输出顺序与输入顺序相同————添加functional头文件 eg: int newa[5] = {1, 3, 2, 1, 3}; sort(newa, newa + n); for (i = 0;i <= 4;i ++) { printf("%d ",newa[i]); } printf("\n"); stable_sort(newa, newa + n, greater<int>()); //int为数据类型 greater为排序要求 for (i = 0;i <= 4;i ++) { printf("%d ",newa[i]); } printf("\n");//二分查找 __先排序————一定要排序!!! //binary_search() 寻找是否出现———— bool型 复杂度log(n); //lower_bound() 返回第一个小于等于K的值 //upper_bound() 返回第一个大于等于K的值 sort(a, a + 5); printf("%ld\n",upper_bound(a, a + 5, 2) - a); printf("%ld\n",lower_bound(a, a + 5, 2) - a);//集合操作 * //merge() 合并 //set_union()* 求并集———————————————————————————//堆 // make_heap() // push_heap() // pop_heap() // sort_heap()——————————————————————————— //最值操作 //min() max() //min_element() 数组中的最小值 复杂度O(n) //max_element() 数组中的最大值 复杂度O(n);推荐题目: 模拟EXCEL排序 + 堆中的路径—————————————————————————— 《VECTOR》 //vector __动态数组 末尾插入 //头文件vector // 不定长数组,随加随用 // 适合不断想尾部追加元素 // push_back() 向尾部追加元素 // pop_back() 从尾部删除元素 // front() 返回第一个元素 // back() 返回最后一个元素 // insert() 插入元素 // erase() 删除元素 + 删除元素的下标 // clear() 清空vector vector<int>x; x.push_back(1); //插入 x.push_back(2); x.push_back(3); for (i = 0;i < x.size();i ++) //x.size()为长度 { printf("%d ",x[i]); } printf("\n"); x.pop_back(); for (i = 0;i < x.size();i ++) //x.size()为长度 { printf("%d ",x[i]); } printf("\n"); x.insert(x.begin() + 1, 5); //在a[1]的位置插入K for (i = 0;i < x.size();i ++) //x.size()为长度 { printf("%d ",x[i]); } printf("\n"); x.erase(x.begin() + 1); //删除a[1] for (i = 0;i < x.size();i ++) //x.size()为长度 { printf("%d ",x[i]); }推荐题目: 说反话 + 打印学生选课清单———————————————————————————/stack __栈 // 灵活方便 // push(); 入栈 // pop(); 将顶栈元素弹出 // top(); 返回栈顶 // empty(); 判断是否为空 // size(); 返回栈元素数量——————————————————————————— // //queue 队列 ———— queue // push(); 入栈 // pop(); 将顶栈元素弹出 // top(); 返回栈顶 // empty(); 判断是否为空 // size(); 返回队列元素数量 // front(); 返回对首元素// priority_queue 优先队列 ———— queue ————默认 大到小排序 // push(); 入栈 // pop(); 将顶栈元素弹出 // top(); 返回栈顶 // empty(); 判断是否为空 // size(); 返回队列元素数量priority_queue<int, vector<int>, greater<int> >q; //从小到大排序 q.push(3); q.push(1); q.push(5); while(!q.empty()) { printf("%d\n", q.top()); q.pop(); } printf("\n"); printf("\n"); priority_queue<int>p; //从大到小排序 p.push(3); p.push(1); p.push(5); while(!p.empty()) { printf("%d\n", p.top()); p.pop(); }—————————————————————————— //set ———— 集合 // 传统意义上的集合 // 元素不重复(自动去重) // insert() 插入元素到集合 // erase() 从集合删除元素 // count() 返回元素是否存在 // size() 返回集合元素个数 // clear() 清空set<int>s; s.insert(1); s.insert(2); s.insert(3); s.insert(2); set<int>::iterator it; for (it = s.begin(); it != s.end(); ++ it) //遍历 { printf("%d ", *it); }推荐题目 : 集合相似度 + 列车调度—————————————————————————— // map ———— 映射 ———— 哈希 ??字符串 ?? // 按照<键,值>的形式映射 // 直接按照数组的形式操作 // a[“abc”] 哈希 // b[123456789] 大数组 // insert()插入键值对 // count()判断键是否存在 // erase()删除键 // clear()清空mapmap<string, int>mpl; mpl["abc"] = 1; mpl["abb"] = 2; printf("%d\n", mpl["aaa"]); //不存在的 都为0 printf("\n"); printf("\n"); printf("\n"); map<long long , int>mp2; mp2[1234567890] = 3; printf("%d\n",mp2[1234567890]);推荐题目 :树种的统计 + 航空公司VIP客户查询;-------------------------------------------------------Problem Description 已知一个按先序序列输入的字符序列,如abc,,de,g,,f,,,(其中逗号表示空节点)。请建立二叉树并按中序和后序方式遍历二叉树,最后求出叶子节点个数和二叉树深度。Input 输入一个长度小于50个字符的字符串。 Output 输出共有7行: 第1行输出前序遍历序列; 第2行输出中序遍历序列; 第3行输出后序遍历序列; 第4行输出层序遍历序列; 第5行输出叶子节点个数; 第6行输出叶子节点(从上到下,从左到右); 第7行输出二叉树深度。Example Inputabc,,de,g,,f,,,Example Outputabcdegf cbegdfa cgefdba abcdefg 3 cfg 5建树struct node *creat(struct node *t) { char c; c = str[i ++]; if (c == ',') t = NULL; else { t = (struct node *)malloc(sizeof(struct node)); t -> data = c; t -> l = creat(t -> l); t -> r = creat(t -> r); } return t; }前序遍历void qianxu(struct node *t) { if (t != NULL) { printf("%c", t -> data); qianxu(t -> l); qianxu(t -> r); }}中序遍历 void zhonxu(struct node *t) { if (t != NULL) { zhonxu(t -> l); printf("%c",t -> data); zhonxu(t -> r); } }后序遍历 void houxu(struct node *t) { if (t != NULL) { houxu(t -> l); houxu(t -> r); printf("%c",t -> data); }}层序遍历void cengxu(struct node *t) { int in = 0, out = 0; struct node *a[1050]; a[in ++] = t; while(in > out) { if (a[out] != NULL) { printf("%c",a[out] -> data); a[in ++] = a[out] -> l; a[in ++] = a[out] -> r; } out ++; } }叶子个数void num(struct node *t) { if (t != NULL) { if (t -> l == NULL && t -> r == NULL) count ++; else { num(t -> l); num(t -> r); } } }叶子节点(从上到下,从左到右) 由层序遍历修改而得void cengxunum(struct node *t) { int in = 0; int out = 0; struct node *p[1050]; p[in ++] = t; while(in > out) { if (p[out] != NULL) { if (p[out] -> l == NULL &&p[out] -> r == NULL) { printf("%c",p[out] -> data); } else { p[in ++] = p[out] -> l; p[in ++] = p[out] -> r; } } out ++; } }二叉树深度int depth(struct node *t) { int d1, d2; if (t != NULL) { d1 = depth(t -> l); d2 = depth(t -> r); if (d1 > d2) return d1 + 1; else return d2 + 1; } return 0; }最终代码:#include<stdio.h>#include<string.h>#include<stdlib.h>#include<math.h> struct node { char data; struct node *l, *r; }tree; struct node *creat(struct node *t); void qianxu(struct node *t); void zhonxu(struct node *t); void houxu(struct node *t); void cengxu(struct node *t); void num(struct node *t); void cengxunum(struct node *t); int depth(struct node *t); char str[1050]; int i; int count = 0; int main() { while(scanf("%s",str)!=EOF) { i = 0; struct node *tree = NULL; tree = creat(tree); qianxu(tree); printf("\n"); zhonxu(tree); printf("\n"); houxu(tree); printf("\n"); cengxu(tree); printf("\n"); num(tree); printf("%d\n",count); cengxunum(tree); printf("\n"); int de; de = depth(tree); printf("%d\n",de); } return 0; } struct node *creat(struct node *t) { char c; c = str[i ++]; if (c == ',') t = NULL; else { t = (struct node *)malloc(sizeof(struct node)); t -> data = c; t -> l = creat(t -> l); t -> r = creat(t -> r); } return t; } void zhonxu(struct node *t) { if (t != NULL) { zhonxu(t -> l); printf("%c",t -> data); zhonxu(t -> r); } } void houxu(struct node *t) { if (t != NULL) { houxu(t -> l); houxu(t -> r); printf("%c",t -> data); }}void qianxu(struct node *t) { if (t != NULL) { printf("%c", t -> data); qianxu(t -> l); qianxu(t -> r); }}void cengxu(struct node *t) { int in = 0, out = 0; struct node *a[1050]; a[in ++] = t; while(in > out) { if (a[out] != NULL) { printf("%c",a[out] -> data); a[in ++] = a[out] -> l; a[in ++] = a[out] -> r; } out ++; } } void num(struct node *t) { if (t != NULL) { if (t -> l == NULL && t -> r == NULL) count ++; else { num(t -> l); num(t -> r); } } } void cengxunum(struct node *t) { int in = 0; int out = 0; struct node *p[1050]; p[in ++] = t; while(in > out) { if (p[out] != NULL) { if (p[out] -> l == NULL &&p[out] -> r == NULL) { printf("%c",p[out] -> data); } else { p[in ++] = p[out] -> l; p[in ++] = p[out] -> r; } } out ++; } } int depth(struct node *t) { int d1, d2; if (t != NULL) { d1 = depth(t -> l); d2 = depth(t -> r); if (d1 > d2) return d1 + 1; else return d2 + 1; } return 0; }
阅读全文
1 0
- 二叉树 STL 图 哈希 详解
- c++之stl 二叉树
- STL源码—二叉树查找树
- 二叉树的遍历(C++、STL)
- AVL平衡二叉树图+代码详解
- 二叉树详解
- 平衡二叉树 详解
- 详解平衡二叉树
- 二叉树详解
- 二叉树实现详解
- 二叉树递归详解
- 二叉查找树详解
- hdu1710 二叉树详解
- 平衡二叉树详解
- 平衡二叉树详解
- 二叉树详解
- 平衡二叉树详解
- 二叉树-详解二叉排序树
- POJ1542 Ultra-QuickSort
- 简单的背包问题
- Maven学习 (一) 搭建Maven环境
- springmvc 的静态资源配置
- 《机器学习技法》学习笔记13——深度学习
- 二叉树 STL 图 哈希 详解
- Editor does not contain a main type
- JS事件处理程序的几种方法
- HDU5512 Pagodas(GCD+水题)
- UVa11582 [快速幂]Colossal Fibonacci Numbers!
- php &
- Shell常见命令实践
- CodeForces
- springBoot如何如何设置fileMazSize()