记录
######################################### # File name :compile.sh # Author :kangkangliang # File desc : # Mail :liangkangkang@paag.com # Create time :2015-11-06 ######################################### #!/usr/bin/bash currentPath=$(pwd | sed 's$\/$\\\/$g') echo $currentPath find . name "*.h"o name "*.c"o name "*.cc"o name "*.cpp"| sed "s/^\./$currentPath/"> cscope.files cscope -bkq i cscope.files ctags -R
CentOS 7的yum源中貌似没有正常安装mysql时的mysql-sever文件,需要去官网上下载 # wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm # rpm -ivh mysql-community-release-el7-5.noarch.rpm # yum install mysql-community-server 成功安装之后重启mysql服务 # service mysqld restart 初次安装mysql是root账户是没有密码的 设置密码的方法 # mysql -uroot mysql> set password for ‘root’@‘localhost’ = password('mypasswd'); mysql> exit
#include <stdio.h> #include <stdlib.h> typedef struct BiTree{ int val; struct BiTree *lchild,*rchild; }BiTree; void Create_Sort_Tree(BiTree **t, int value) { if(*t == NULL){ *t = (BiTree *)malloc(sizeof(BiTree)); (*t)->val = value; (*t)->lchild = NULL; (*t)->rchild = NULL; } else{ if((*t)->val > value) Create_Sort_Tree(&(*t)->lchild, value); else Create_Sort_Tree(&(*t)->rchild, value); } } void lar(BiTree *t) { if(t == NULL) return; else{ lar(t->lchild); printf("%d\t",t->val); lar(t->rchild); } } void Preorder(BiTree *t) { if(t) { printf("%d\t ", t->val); Preorder(t->lchild); Preorder(t->rchild); } } void Inorder(BiTree *t) { if(t) { Inorder(t->lchild); printf("%d\t ", t->val); Inorder(t->rchild); } } void Postorder(BiTree *t) { if(t) { Postorder(t->lchild); Postorder(t->rchild); printf("%d\t ", t->val); } } /**根据前序周游和中序周游,重建一颗二叉树。 * @param pre 前序周游的结果,其中每一个字符表示一个节点的值 * @param mid 中序周游的结果,其中每一个字符表示一个节点的值 * @param n 该树节点总数。 * @return 生成的树的根节点。 */ BiTree* buildTree(char *pre, char *mid, int n) { if (n==0) return NULL; char c = pre[0]; // BiTree*node = new TreeNode(c); //This is the root node of this tree/sub tree. BiTree *node = (BiTree*)malloc(sizeof(BiTree)); int i; for(i=0; i<n && mid[i]!=c; i++); int lenL = i; // the node number of the left child tree. int lenR = n - i -1; // the node number of the rigth child tree. //build the left child tree. The first order for thr left child tree is from // starts from pre[1], since the first element in pre order sequence is the root // node. The length of left tree is lenL. if(lenL > 0) node->lchild = buildTree(&pre[1], &mid[0], lenL); //build the right child tree. The first order stree of right child is from //lenL + 1(where 1 stands for the root node, and lenL is the length of the // left child tree.) if(lenR > 0) node->rchild = buildTree(&pre[lenL+1], &mid[lenL+1], lenR); return node; } BiTree* ConstructCore(int *start_pre,int *end_pre,int *start_in,int* end_in) { // 先序列的第一个节点为根节点 int key = start_pre[0]; BiTree *root = (BiTree*)malloc(sizeof(BiTree)); root->val = key; root->rchild = root->lchild = NULL; ///quit condition if(start_pre == end_pre){ // if(start_in == end_in && *start_pre == *start_in) if(start_in == end_in) return root; else printf("invalid input\n"); } // 在中序序列中找到根节点 int *root_in = start_in; while(root_in <= end_in && *root_in != key) ++root_in; if(root_in == end_in && *root_in != key) printf("invalid input\n"); int left_len = root_in - start_in; int *left_pre_end = start_pre + left_len; if(left_len > 0){ // create left subtree // root->lchild = ConstructCore(start_in+1,left_pre_end,start_in,root_in-1); root->lchild = ConstructCore(start_pre+1,left_pre_end,start_in,root_in-1); } if(left_len < end_pre - start_pre){ // create right subtree root->rchild = ConstructCore(left_pre_end+1,end_pre,root_in+1,end_in); } return root; } // 根据先序和中序来构建二叉树 BiTree * Construct(int *pre,int *in,int len) { if(pre == NULL || in == NULL || len <= 0) return NULL; return ConstructCore(pre,pre+len-1,in,in+len-1); } int SumNode(BiTree *root) { int count = 0; if(root == NULL) return 0; else if(root->rchild == NULL && root->lchild == NULL) return 1; else count = SumNode(root->lchild)+SumNode(root->rchild); return count; } unsigned int FullNodeSum(BiTree *root) { unsigned int count = 0; if(root == NULL) return 0; else if(root->lchild == NULL && root->rchild == NULL) return 0; else if(root->lchild != NULL && root->rchild == NULL) count = FullNodeSum(root->lchild); else if(root->left == NULL && root->right != NULL) count = FullNodeSum(root->right); else // 1 key code 防止只有一个结点 count = 1 + FullNodeSum(roott->left) + FullNodeSum(root->right); return count; } int main(void) { int i; BiTree *t = NULL; int value[] = {5,8,14,36,21,1,3}; for(i = 0;i < 7;i++) Create_Sort_Tree(&t,value[i]); // lar(t); printf("\n"); Preorder(t); printf("\n"); // two printf("one\n"); // int pre[8] = {1,2,4,7,3,5,6,8}; // int in[8] = {4,7,2,1,5,3,8,6}; // // char pre[] = "12473568"; // // char in[] = "47215386"; // BiTree *t2 = Construct(pre,in,8); int preorder[] = {4,2,1,3,7,6,5,8}; int inorder[] = {1,2,3,4,5,6,7,8}; BiTree* t2 = Construct(preorder,inorder,8); Preorder(t2); printf("\n"); Inorder(t2); printf("\n"); Postorder(t2); printf("\n"); printf("leaf :\n"); int leaf = SumNode(t2); printf("%d\n", leaf); }
tree
/* delete dup node -------- if(pHead==NULL || pHead->next==NULL){ return pHead; } if(pHead->val==pHead->next->val){ ListNode* cur_node = pHead->next->next; int val = pHead->val; delete pHead; delete pHead->next; while(cur_node && cur_node->val==val){ ListNode* be_deleted = cur_node; cur_node = cur_node->next; delete be_deleted; } return deleteDuplication(cur_node); } else{ pHead->next = deleteDuplication(pHead->next); return pHead; } ---------- struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } }; class Solution { public: ListNode* deleteDuplication(ListNode* pHead) { if(pHead==0) return 0; map<int,int> m; ListNode* p=pHead; while(p!=0) { m[p->val]++; p=p->next; } //判断head while(m[pHead->val]>1) { pHead=pHead->next; if(pHead==0) return 0; } ListNode* p1=pHead; ListNode* p2=pHead->next; while(p2!=0) { if(m[p2->val]>1) { p2=p2->next; p1->next=p2; } else { p2=p2->next; p1=p1->next; } } return pHead; */ #include <stdio.h> #include <iostream> #include <fstream> #include <vector> #include <string> #include <queue> #include <iterator> #include <stack> #include <malloc.h> using namespace std; #define N 10 typedef struct node { int data; struct node * lchild; struct node * rchild; }node; int count = 0; #define MIN(a,b) ((a) >= (b) ? (b) : (a)) #define MAX(a,b) ((a) >= (b) ? (a) : (b)) void Init(node *t) { t = NULL; } node * Insert(node *t , int key) { if(t == NULL) { node * p; p = (node *)malloc(sizeof(node)); p->data = key; p->lchild = NULL; p->rchild = NULL; t = p; } else { if(key < t->data) t->lchild = Insert(t->lchild, key); else t->rchild = Insert(t->rchild, key); } return t; //important! } node * creat(node *t) { int array[N] = {5,7,1,9,4,8,3,6,11,12}; int i; for (i = 0; i < N; ++i) { t = Insert(t, array[i]); } return t; } // / 前序遍历递归 void PreOrder(node * t) { if(t != NULL) { printf("%d ", t->data); PreOrder(t->lchild); PreOrder(t->rchild); } } /* * 对于任意节点, * 1.访问节点p,并将节点p入栈 * 2.判断该节点p的左孩子是否为空,若为空,则取栈顶节点并进行出栈操作 * 并将该栈顶节点的右孩子置为当前的p,循环1 若不为空,则将p的左孩子置为当前的节点p */ /// 前序遍历非递归 void RePreOrder(node *t) { /// / use stack stack<node *> s; node *p = t; while(p != NULL || !s.empty()) { while( p != NULL ) { cout << p->data << " "; s.push(p); p=p->lchild; } if(!s.empty()) { p = s.top(); s.pop(); p=p->rchild; } } } void InOrder(node * t) //中序遍历输出 { if(t != NULL) { InOrder(t->lchild); // printf("%d ", t->data); printf("%d ", t->data); InOrder(t->rchild); } } /* *对于任意节点p *1.若其左孩子不为空,则将p入栈,并将p的左孩子置为当前的p, *然后对其节点p再做相同的处理 *2.若左孩子为空,则取栈顶元素进行出栈,访问该节点,然后将当前的p置为该 *栈顶节点的右孩子 *3.直到p为NULL,并且栈为空,则遍历结束 */ /// 中序遍历非递归 void ReInOrder(node *t) { stack<node *> s; node *p = t; while(p!= NULL || !s.empty()) { while(p!=NULL) { s.push(p); p=p->lchild; } if(!s.empty()) { p = s.top(); cout << p->data << " "; s.pop(); p=p->rchild; } } } /// 后序遍历 void PostOrder(node * t) { if(t != NULL) { PostOrder(t->lchild); PostOrder(t->rchild); printf("%d ", t->data); // cout << t->data << " "; } } /* * 要保证根结点在左孩子和右孩子访问之后才能访问,因此对于任一结点P,先将其入栈。如果P不存在左孩子和右孩子,则可以直接访问它;或者P存在左孩子或者右孩子,但是其左孩子和右孩子都已被访问过了,则同样可以直接访问该结点。若非上述两种情况,则将P的右孩子和左孩子依次入栈,这样就保证了每次取栈顶元素的时候,左孩子在右孩子前面被访问,左孩子和右孩子都在根结点前面被访问。 */ void RePostOrder(node *t) { stack<node *> s; node *cur; node *pre = NULL; s.push(t); while(!s.empty()) { cur = s.top(); if((cur->lchild == NULL && cur->rchild==NULL)|| (pre != NULL &&(pre == cur->lchild || pre == cur->rchild)) ) { cout << cur->data << " "; s.pop(); pre = cur; } else { if(cur->rchild != NULL) s.push(cur->rchild); if(cur->lchild != NULL) s.push(cur->lchild); } } } /// 层次遍历 vector<vector<int> > LevelOrder(node *root) { vector<vector<int> > res; vector<int> v; if(root == NULL) return res; int before = 0; int curr = 0; queue<node *> q; q.push(root); curr++; while(!q.empty()) { before = curr; curr = 0; while(before--) { node * t = q.front(); if(t->lchild != NULL) { q.push(t->lchild); curr++; } if(t->rchild != NULL) { q.push(t->rchild); curr++; } v.push_back(t->data); q.pop(); } res.push_back(v); v.clear(); } return res; } node * KthNode(node * root,unsigned int k) { if(root) { node * ret = KthNode(root->lchild,k); if(ret) return ret; if(++count == k) return root; ret = KthNode(root->rchild,k); if(ret) return ret; } return NULL; } void InOrdeTwo(node * t,vector<node *> &v) //中序遍历输出 { if(t != NULL) { InOrdeTwo(t->lchild,v); v.push_back(t); InOrdeTwo(t->rchild,v); } } node * KthNodeTwo(node * root,unsigned int k) { /// 利用中序遍历,和vector,直接读取倒数第k个就可以了 /// 因为返回来的是节点的指针 if(root == NULL || k <= 0) return NULL; vector<node*> vec; /// 存入节点 InOrdeTwo(root,vec); if(k > vec.size()) return NULL; return vec[k-1]; } int Depth(node *root) { return root?(MAX(Depth(root->lchild),Depth(root->rchild)) + 1):0; } void FreeNode(node *root) { if(root) { FreeNode(root->lchild); FreeNode(root->rchild); /// 当左右节点都不为空,释放空间 free(root); } } /* * pre 前序遍历的数组,in后遍历 的数组,len 树的长度 递归思想,递归的终止条件是树的长度len == 0 在中序遍历的数组中找到前序数组的第一个字符,记录在中序数组中的位置index.如果找不到,说明前序遍历数组和中序遍历数组有问题 ,提示错误信息,退出程序即可;找到index后,新建一个二叉树节点t,t->item= *pre, 然后递归的求t的左孩子和有孩子 递归的左孩子: void rebuildTree(pre + 1, in, index) 递归的右孩子: void rebuildTree(pre + (index + 1), in + (index +1), len - (index + 1)) * * * * * */ node *rebuildTree(char *pre,char *in,int len) { node *t; if(len <= 0) t = NULL; else { int index = 0; while(index < len && *(pre) != *(in + index)) { index++; } if(index >= len) { cout << "error " << endl; return NULL; } t = (node *)malloc(sizeof(node)); t->data = *pre; t->lchild = rebuildTree(pre+1,in,index); t->rchild = rebuildTree(pre+(index+1),in +(index+1),len-(index+1)); } return t; } /* * 根据中序遍历和后序遍历,得到前序遍历 * 根据后序遍历的特点,知道后序遍历最后一个节点为根节点,即为A 观察中序遍历,A左侧CBEDF为A左子树节点,A后侧HGJI为A右子树节点 然后递归的构建A的左子树和后子树 * * * */ node *rebuildTreeTwo(char *in,char *post,int len) { node *t; if(len <= 0) t = NULL; else { int index = 0; while(index < len && *(post + len -1) != *(in + index)) { index++; } // if(index >= len) // { // cout << "error " << endl; // return NULL; // } t = (node *)malloc(sizeof(node)); t->data = *(in +index); t->lchild = rebuildTreeTwo(in,post,index); t->rchild = rebuildTreeTwo(in+(index+1),post +index,len-(index+1)); } return t; } /** * @param * @param 之字形打印二叉树 * @param * @param */ vector<vector<int> > ZhiPrint(node * t) { #if 0 vector<vector<int> > result; stack<node *> stack1,stack2; if(t!=NULL) stack1.push(t); node *p; while(!stack1.empty() || !stack2.empty()){ vector<int> data; if(!stack1.empty()){ while(!stack1.empty()){ p = stack1.top(); stack1.pop(); data.push_back(p->data); if(p->lchild!=NULL) stack2.push(p->lchild); if(p->rchild!=NULL) stack2.push(p->rchild); } result.push_back(data); } else if(!stack2.empty()){ while(!stack2.empty()){ p = stack2.top(); stack2.pop(); data.push_back(p->data); if(p->rchild!=NULL) stack1.push(p->rchild); if(p->lchild!=NULL) stack1.push(p->lchild); } result.push_back(data); } } return result; #endif vector<vector<int> > res; stack<node *> stack1,stack2; if(t) stack1.push(t); else return res; node *p; /// 一个小小的错误判断是否为空,对于stack2导致自己调试了好久,一定要小心,对于别人的东西,先要理解,然后在跟踪代码 while(!stack1.empty() || !stack2.empty()){ ///对于临时的数据data,每次的使用都会清空,所以我们在两个栈交换的同时,转到while在重新定义变量 vector<int> data; if(!stack1.empty()){ while(!stack1.empty()){ p = stack1.top(); stack1.pop(); data.push_back(p->data); if(p->lchild!=NULL) stack2.push(p->lchild); if(p->rchild!=NULL) stack2.push(p->rchild); } res.push_back(data); } else if(!stack2.empty()){ while(!stack2.empty()){ p = stack2.top(); stack2.pop(); data.push_back(p->data); if(p->rchild!=NULL) stack1.push(p->rchild); if(p->lchild!=NULL) stack1.push(p->lchild); } res.push_back(data); } } return res; } /* * 代码的规范 * while(exper){ * ......; } */ int SumNode(node *root) { int count = 0; if(root == NULL) return 0; else if(root->rchild == NULL && root->lchild == NULL) return 1; else count = SumNode(root->lchild)+SumNode(root->rchild); return count; } int main() { node * t = NULL; t = creat(t); cout << "preorder " << endl; /// PreOrder cout << "--------------" << endl; PreOrder(t); cout << endl; RePreOrder(t); cout << endl; cout << "--------------" << endl; cout << endl; cout << "inorder" << endl; cout << "--------------" << endl; InOrder(t); cout << endl; ReInOrder(t); cout << endl; cout << "--------------" << endl; cout << "---post-----------" << endl; PostOrder(t); cout << endl; cout << "---repost-----------" << endl; RePostOrder(t); cout << endl; #if 0 cout << endl; cout << endl; cout << "postorder" << endl; cout << "--------------" << endl; PostOrder(t); cout << endl; cout << endl; cout << "--------------" << endl; #endif /// 之子打印二叉树 vector<vector<int> > zhi; zhi = ZhiPrint(t); cout << "liang***" << endl; for (unsigned int i = 0; i < zhi.size(); ++i) { for (unsigned int j = 0; j < zhi[i].size(); ++j) { cout << zhi[i][j] << " "; } cout << endl; } cout << "liang***" << endl; cout << "sum node:" << endl; int sum = SumNode(t); cout << sum << endl; // SumNode(t,sum); #if 0 vector<vector<int> > layer = LevelOrder(t); for (int i = 0; i < layer.size(); ++i) { for (int j = 0; j < layer[i].size(); ++j){ cout << layer[i][j] << " "; } cout << endl; } for (int i = 0; i < layer.size(); ++i) { if(i % 2 == 0) { for (int j = 0; j < layer[i].size(); ++j) cout << layer[i][j] << " "; } else { for (int k = layer[i].size()-1; k >=0 ; k--) cout << layer[i][k] << " "; } cout << endl; } cout << "2 node :"; node * k = KthNode(t,2); cout << k->data << endl; cout << "3 node :"; node * k1 = KthNodeTwo(t,3); cout << k1->data << endl; cout << "the tree depth : " << Depth(t) << endl; /// 根据前序和中序遍历得到二叉树 char pre[] = "abcdefgh"; char in[] = "cbedfagh"; node *bt = NULL; bt = rebuildTree(pre,in,8); InOrder(bt); /// 根据后序和中序遍历得到二叉树 char str_inder[] = "cbedfahgji"; char str_post[] = "cefdbhjiga"; node *bt1 = NULL; bt1 = rebuildTreeTwo(str_inder,str_post,10); cout << endl; InOrder(bt1); FreeNode(bt); bt=NULL; FreeNode(bt1); bt1=NULL; #endif FreeNode(t); t = NULL; return 0; }
ipyt to pdf
######################################### # File name :npynb2pdf.sh # Author :kangkangliang # File desc : # Mail :liangkangkang@paag.com # Create time :2016-03-08 ######################################### #!/usr/bin/bash ipython nbconvert --to latex --post PDF 00-classification.ipynb ipython nbconver --to PDF 00-classification.ipynb ipython nbconvert --to latex Example.ipynb ipython nbconvert --to latex 00-classification.ipynb xelatex 00-classification.tex