二叉树简介及C++实现
二叉树是每个结点最多有两个子树的树结构,即结点的度最大为2。通常子树被称作”左子树”和”右子树”。二叉树是一个连通的无环图。
二叉树是递归定义的,其结点有左右子树之分,逻辑上二叉树有五种基本形态:(1)、空二叉树;(2)、只有一个根结点的二叉树;(3)、只有左子树;(4)、只有右子树;(5)、完全二叉树。
二叉树类型:
(1)、满二叉树:深度(层数)为k,且有2^k-1个结点的二叉树。这种树的特点是每一层上的结点数都是最大结点数。即除了叶结点外每一个结点都有左右子树且叶节点都处在最低层。
(2)、完全二叉树:除最后一层外,其余层都是满的,并且最后一层或者是满的,或者是在右边缺少连续若干节点,即叶子结点都是从左到右依次排布。具有n个节点的完全二叉树的深度为floor(log(2n))+1。深度为k的完全二叉树,至少有2^(k-1)个结点,至多有(2^k)-1个结点。
(3)、平衡二叉树:又被称为AVL树,它是一颗二叉排序树,且具有以下性质:它是一颗空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一颗平衡二叉树。
(4)、斜树:所有的结点都只有左子树(左斜树),或者只有右子树(右斜树)。
(5)、二叉搜素树(或二叉排序树):特殊的二叉树,每个结点都不比它左子树的任意元素小,而且不比它的右子树的任意元素大。二叉搜索树的左右子树也都是二叉搜索树。按中序遍历,则会得到按升序排列的有序数据集。
二叉树不是树的一种特殊情形。
遍历二叉树:按一定的规则和顺序走遍二叉树的所有结点,使每一个结点都被访问一次,而且只被访问一次。对一颗二叉树的遍历有四种情况:先序遍历、中序遍历、后序遍历、按层遍历。
(1)、先序遍历:先访问根结点,再先序遍历左子树,最后再先序遍历右子树,即先访问根结点-------左子树------右子树。
(2)、中序遍历:先中序遍历左子树,然后再访问根结点,最后再中序遍历右子树,即先访问左子树------根结点------右子树。
(3)、后序遍历:先后序遍历左子树,然后再后序遍历右子树,最后再访问根结点,即先访问左子树------右子树------根结点。
(4)、按层遍历:从上到下,从左到右依次访问结点。
下面代码是二叉搜索树的实现,主要包括树的创建、插入、删除、查找、遍历、保存、载入。
binary_search_tree.hpp:
#ifndef FBC_CPPBASE_TEST_BINARY_SEARCH_TREE_HPP_
#define FBC_CPPBASE_TEST_BINARY_SEARCH_TREE_HPP_#include <vector>
#include <fstream>
#include <string>namespace binary_search_tree_ {typedef struct info {int id; // suppose id is uniquestd::string name;int age;std::string addr;
} info;typedef struct node {info data;node* left = nullptr;node* right = nullptr;
} node;class BinarySearchTree {
public:BinarySearchTree() = default;~BinarySearchTree() { DeleteTree(tree); }typedef std::tuple<int, int, std::string, int, std::string> row; // flag(-1: no node, 0: have a node), id, name, age, addrint Init(const std::vector<info>& infos); // create binary search treebool Search(int id, info& data) const;int Insert(const info& data);int Delete(int id); // delete a nodeint Traversal(int type) const; // 0: pre-order, 1: in-order, 2: post-order, 3: levelint GetMaxDepth() const; // get tree max depthint GetMinDepth() const; // get tree min depthint GetNodesCount() const; // get tree node countbool IsBinarySearchTree() const; // whether or not is a binary search tree//bool IsBinaryBalanceTree() const; // whether ot not is a binary balance treeint GetMinValue(info& data) const;int GetMaxValue(info& data) const;int SaveTree(const char* name) const; // tree write in txt fileint LoadTree(const char* name);protected:void PreorderTraversal(const node* ptr) const;void InorderTraversal(const node* ptr) const;void PostorderTraversal(const node* ptr) const;void LevelTraversal(const node* ptr) const;void LevelTraversal(const node* ptr, int level) const;void DeleteTree(node* ptr);void Insert(node* ptr, const info& data);const node* Search(const node* ptr, int id) const;void IsBinarySearchTree(const node* ptr, bool is_bst) const;int GetNodesCount(const node* ptr) const;int GetMaxDepth(const node* ptr) const;int GetMinDepth(const node* ptr) const;//bool IsBinaryBalanceTree(const node* ptr) const;node* Delete(node* ptr, int id); // return new rootnode* GetMinValue(node* ptr);void NodeToRow(const node* ptr, std::vector<row>& rows, int pos) const;void RowToNode(node* ptr, const std::vector<row>& rows, int n, int pos);private:node* tree = nullptr;bool flag;
};int test_binary_search_tree();} // namespace binary_search_tree_#endif // FBC_CPPBASE_TEST_BINARY_SEARCH_TREE_HPP_
binary_search_tree.cpp:
#include "binary_search_tree.hpp"
#include <set>
#include <iostream>
#include <limits>
#include <tuple>
#include <string>
#include <sstream>
#include <string.h>
#include <algorithm>namespace binary_search_tree_ {int BinarySearchTree::Init(const std::vector<info>& infos)
{std::vector<int> ids;for (const auto& info : infos) {ids.emplace_back(info.id);}std::set<int> id_set(ids.cbegin(), ids.cend());if (id_set.size() != ids.size()) {fprintf(stderr, "id must be unique\n");return -1;}for (const auto& info : infos) {Insert(info);}return 0;
}bool BinarySearchTree::Search(int id, info& data) const
{const node* root = tree;const node* tmp = Search(root, id);if (tmp) {data = tmp->data;return true;} else {return false;}
}const node* BinarySearchTree::Search(const node* ptr, int id) const
{if (ptr) {if (ptr->data.id == id) {return ptr;} else if (ptr->data.id > id) {return Search(ptr->left, id);} else {return Search(ptr->right, id);}} else {return nullptr;}
}int BinarySearchTree::Insert(const info& data)
{flag = true;if (tree) {Insert(tree, data);} else {tree = new node;tree->data = data;tree->left = nullptr;tree->right = nullptr;}return (int)flag;
}void BinarySearchTree::Insert(node* ptr, const info& data)
{if (ptr->data.id == data.id) {flag = false;return;}if (ptr->data.id < data.id) {if (ptr->right) {Insert(ptr->right, data);} else {ptr->right = new node;ptr->right->data = data;ptr->right->left = nullptr;ptr->right->right = nullptr;}} else {if (ptr->left) {Insert(ptr->left, data);} else {ptr->left = new node;ptr->left->data = data;ptr->left->left = nullptr;ptr->left->right = nullptr;}}
}bool BinarySearchTree::IsBinarySearchTree() const
{bool is_bst = true;const node* root = tree;IsBinarySearchTree(root, is_bst);return is_bst;
}void BinarySearchTree::IsBinarySearchTree(const node* ptr, bool is_bst) const
{static int last_data = std::numeric_limits<int>::min();if (!ptr) return;IsBinarySearchTree(ptr->left, is_bst);if (last_data < ptr->data.id) last_data = ptr->data.id;else {is_bst = false;return;}IsBinarySearchTree(ptr->right, is_bst);
}int BinarySearchTree::Traversal(int type) const
{if (!tree) {fprintf(stderr, "Error: it is an empty tree\n");return -1;}const node* root = tree;if (type == 0)PreorderTraversal(root);else if (type == 1)InorderTraversal(root);else if (type == 2)PostorderTraversal(root);else if (type == 3)LevelTraversal(root);else {fprintf(stderr, "Error: don't suppot traversal type, type only support: 0: pre-order, 1: in-order, 2: post-order\n");return -1;}return 0;
}void BinarySearchTree::PreorderTraversal(const node* ptr) const
{if (ptr) {fprintf(stdout, "Info: id: %d, name: %s, age: %d, addr: %s\n",ptr->data.id, ptr->data.name.c_str(), ptr->data.age, ptr->data.addr.c_str());PreorderTraversal(ptr->left);PreorderTraversal(ptr->right);}
}void BinarySearchTree::InorderTraversal(const node* ptr) const
{if (ptr) {InorderTraversal(ptr->left);fprintf(stdout, "Info: id: %d, name: %s, age: %d, addr: %s\n",ptr->data.id, ptr->data.name.c_str(), ptr->data.age, ptr->data.addr.c_str());InorderTraversal(ptr->right);}
}void BinarySearchTree::PostorderTraversal(const node* ptr) const
{if (ptr) {PostorderTraversal(ptr->left);PostorderTraversal(ptr->right);fprintf(stdout, "Info: id: %d, name: %s, age: %d, addr: %s\n",ptr->data.id, ptr->data.name.c_str(), ptr->data.age, ptr->data.addr.c_str());}
}void BinarySearchTree::LevelTraversal(const node* ptr) const
{int h = GetMaxDepth();for (int i = 1; i <= h; ++i)LevelTraversal(ptr, i);
}void BinarySearchTree::LevelTraversal(const node* ptr, int level) const
{if (!ptr) return;if (level == 1)fprintf(stdout, "Info: id: %d, name: %s, age: %d, addr: %s\n",ptr->data.id, ptr->data.name.c_str(), ptr->data.age, ptr->data.addr.c_str());else if (level > 1) {LevelTraversal(ptr->left, level-1);LevelTraversal(ptr->right, level-1);}
}void BinarySearchTree::DeleteTree(node* ptr)
{if (ptr) { DeleteTree(ptr->left);DeleteTree(ptr->right);delete ptr;}
}int BinarySearchTree::GetNodesCount() const
{ const node* root = tree;return GetNodesCount(root);
}int BinarySearchTree::GetNodesCount(const node* ptr) const
{if (!ptr) return 0;else return GetNodesCount(ptr->left) + 1 + GetNodesCount(ptr->right);
}int BinarySearchTree::GetMaxDepth() const
{const node* root = tree;return GetMaxDepth(root);
}int BinarySearchTree::GetMaxDepth(const node* ptr) const
{if (!ptr) return 0;int left_depth = GetMaxDepth(ptr->left);int right_depth = GetMaxDepth(ptr->right);return std::max(left_depth, right_depth) + 1;
}int BinarySearchTree::GetMinDepth() const
{const node* root = tree;return GetMinDepth(root);
}int BinarySearchTree::GetMinDepth(const node* ptr) const
{if (!ptr) return 0;int left_depth = GetMaxDepth(ptr->left);int right_depth = GetMaxDepth(ptr->right);return std::min(left_depth, right_depth) + 1;
}/*bool BinarySearchTree::IsBinaryBalanceTree() const
{const node* root = tree;return IsBinaryBalanceTree(root);
}bool BinarySearchTree::IsBinaryBalanceTree(const node* ptr) const
{// TODO: code need to modifyif (GetMaxDepth(ptr) - GetMinDepth(ptr) <= 1) return true;else return false;
}*/int BinarySearchTree::GetMinValue(info& data) const
{if (!tree) {fprintf(stderr, "Error: it is a empty tree\n");return -1;}const node* root = tree;while (root->left) root = root->left;data = root->data;return 0;
}int BinarySearchTree::GetMaxValue(info& data) const
{if (!tree) {fprintf(stderr, "Error: it is a empty tree\n");return -1;}const node* root = tree;while (root->right) root = root->right;data = root->data;return 0;
}int BinarySearchTree::Delete(int id)
{if (!tree) {fprintf(stderr, "Error: it is a empty tree\n");return -1;}const node* root = tree;const node* ret = Search(root, id);if (!ret) {fprintf(stdout, "Warning: this id don't exist in the tree: %d", id);return 0;} tree = Delete(tree, id); return 0;
}node* BinarySearchTree::GetMinValue(node* ptr)
{node* tmp = ptr;while (tmp->left) tmp = tmp->left;return tmp;
}node* BinarySearchTree::Delete(node* ptr, int id)
{if (!ptr) return ptr;if (id < ptr->data.id)ptr->left = Delete(ptr->left, id);else if (id > ptr->data.id) ptr->right = Delete(ptr->right, id);else {if (!ptr->left) {node* tmp = ptr->right;delete ptr;return tmp;} else if (!ptr->right) {node* tmp = ptr->left;delete ptr;return tmp;}node* tmp = GetMinValue(ptr->right);ptr->data = tmp->data;ptr->right = Delete(ptr->right, tmp->data.id);}return ptr;
}int BinarySearchTree::SaveTree(const char* name) const
{std::ofstream file(name, std::ios::out);if (!file.is_open()) {fprintf(stderr, "Error: open file fail: %s\n", name);return -1;}const node* root = tree;int max_depth = GetMaxDepth(root);int max_nodes = (1 << max_depth) -1;root = tree;int nodes_count = GetNodesCount(root);//fprintf(stdout, "max_depth: %d, max nodes: %d\n", max_depth, max_nodes);file<<nodes_count<<","<<max_depth<<std::endl;std::vector<row> vec(max_nodes, std::make_tuple(-1, -1, " ", -1, " "));root = tree;NodeToRow(root, vec, 0); for (const auto& v : vec) {file << std::get<0>(v)<<","<<std::get<1>(v)<<","<<std::get<2>(v)<<","<<std::get<3>(v)<<","<<std::get<4>(v)<<std::endl;}file.close();return 0;
}void BinarySearchTree::NodeToRow(const node* ptr, std::vector<row>& rows, int pos) const
{if (!ptr) return;rows[pos] = std::make_tuple(0, ptr->data.id, ptr->data.name, ptr->data.age, ptr->data.addr);if (ptr->left) NodeToRow(ptr->left, rows, 2 * pos + 1);if (ptr->right) NodeToRow(ptr->right, rows, 2 * pos + 2);
}int BinarySearchTree::LoadTree(const char* name)
{std::ifstream file(name, std::ios::in);if (!file.is_open()) {fprintf(stderr, "Error: open file fail: %s\n", name);return -1;}std::string line, cell;std::getline(file, line);std::stringstream line_stream(line);std::vector<int> vec;while (std::getline(line_stream, cell, ',')) {vec.emplace_back(std::stoi(cell));}if (vec.size() != 2) {fprintf(stderr, "Error: parse txt file fail\n");return -1;}fprintf(stdout, "nodes count: %d, max depth: %d\n", vec[0], vec[1]);int max_nodes = (1 << vec[1]) - 1;std::vector<row> rows;while (std::getline(file, line)) {std::stringstream line_stream2(line);std::vector<std::string> strs;while (std::getline(line_stream2, cell, ',')) {strs.emplace_back(cell);}if (strs.size() != 5) {fprintf(stderr, "Error: parse line fail\n");return -1;}row tmp = std::make_tuple(std::stoi(strs[0]), std::stoi(strs[1]), strs[2], std::stoi(strs[3]), strs[4]);rows.emplace_back(tmp);}if (rows.size() != max_nodes || std::get<0>(rows[0]) == -1) {fprintf(stderr, "Error: parse txt file line fail\n");return -1;}node* root = new node;root->data = {std::get<1>(rows[0]), std::get<2>(rows[0]), std::get<3>(rows[0]), std::get<4>(rows[0])};root->left = nullptr;root->right = nullptr;tree = root;RowToNode(root, rows, max_nodes, 0);file.close();return 0;
}void BinarySearchTree::RowToNode(node* ptr, const std::vector<row>& rows, int n, int pos)
{if (!ptr || n == 0) return;int new_pos = 2 * pos + 1;if (new_pos < n && std::get<0>(rows[new_pos]) != -1) {ptr->left = new node;ptr->left->data = {std::get<1>(rows[new_pos]), std::get<2>(rows[new_pos]), std::get<3>(rows[new_pos]), std::get<4>(rows[new_pos])};ptr->left->left = nullptr;ptr->left->right = nullptr;RowToNode(ptr->left, rows, n, new_pos);}new_pos = 2 * pos + 2;if (new_pos < n && std::get<0>(rows[new_pos]) != -1) {ptr->right = new node;ptr->right->data = {std::get<1>(rows[new_pos]), std::get<2>(rows[new_pos]), std::get<3>(rows[new_pos]), std::get<4>(rows[new_pos])};ptr->right->left = nullptr;ptr->right->right = nullptr;RowToNode(ptr->right, rows, n, new_pos);}
}int test_binary_search_tree()
{fprintf(stdout, "create binary search tree:\n");std::vector<info> infos{{1004, "Tom", 8, "Beijing"}, {1005, "Jack", 9, "Tianjin"}, {1003, "Mark", 6, "Hebei"}, {1009, "Lisa", 11, "Beijiing"}, {1007, "Piter", 4, "Hebei"}, {1001, "Viner", 6, "Beijing"}};BinarySearchTree bstree;bstree.Init(infos);fprintf(stdout, "\ninsert operation:\n");std::vector<info> infos2{{1007, "xxx", 11, "yyy"}, {1008, "Lorena", 22, "Hebie"}, {1002, "Eillen", 14, "Shanxi"}};for (const auto& info : infos2) {int flag = bstree.Insert(info);if (flag) fprintf(stdout, "insert success\n");else fprintf(stdout, "Warning: id %d already exists, no need to insert\n", info.id);}fprintf(stdout, "\ntraversal operation:\n");fprintf(stdout, "pre-order traversal:\n");bstree.Traversal(0);fprintf(stdout, "in-order traversal:\n");bstree.Traversal(1);fprintf(stdout, "post-order traversal:\n");bstree.Traversal(2);fprintf(stdout, "level traversal:\n");bstree.Traversal(3);fprintf(stdout, "\nsearch operation:\n");std::vector<int> ids {1009, 2000};for (auto id : ids) {info ret;bool flag = bstree.Search(id, ret);if (flag)fprintf(stdout, "found: info: %d, %s, %d, %s\n", ret.id, ret.name.c_str(), ret.age, ret.addr.c_str());elsefprintf(stdout, "no find: no id info: %d\n", id);}fprintf(stdout, "\nwhether or not is a binary search tree operation:\n");bool flag2 = bstree.IsBinarySearchTree();if (flag2) fprintf(stdout, "it is a binary search tree\n");else fprintf(stdout, "it is not a binary search tree\n");fprintf(stdout, "\ncalculate node count operation:\n");int count = bstree.GetNodesCount();fprintf(stdout, "tree node count: %d\n", count);fprintf(stdout, "\ncalculate tree depth operation:\n");int max_depth = bstree.GetMaxDepth();int min_depth = bstree.GetMinDepth();fprintf(stdout, "tree max depth: %d, min depth: %d\n", max_depth, min_depth);/*fprintf(stdout, "\nwhether or not is a binary balance tree operation:\n");flag2 = bstree.IsBinaryBalanceTree();if (flag2) fprintf(stdout, "it is a binary balance tree\n");else fprintf(stdout, "it is not a binary balance tree\n");*/fprintf(stdout, "\nget min and max value(id):\n");info value;bstree.GetMinValue(value);fprintf(stdout, "tree min value: id: %d\n", value.id);bstree.GetMaxValue(value);fprintf(stdout, "tree max value: id: %d\n", value.id);fprintf(stdout, "\ndelete node operation:\n");bstree.Delete(1005);bstree.Traversal(1);fprintf(stdout, "\nsave tree operation:\n");
#ifdef _MSC_VERchar* name = "E:/GitCode/Messy_Test/testdata/binary_search_tree.model";
#elsechar* name = "testdata/binary_search_tree.model";
#endifbstree.SaveTree(name);fprintf(stdout, "\nload tree operation:\n");BinarySearchTree bstree2;bstree2.LoadTree(name);int count2 = bstree2.GetNodesCount();int max_depth2 = bstree2.GetMaxDepth();int min_depth2 = bstree2.GetMinDepth();fprintf(stdout, "tree node count: %d, tree max depth: %d, min depth: %d\n", count2, max_depth2, min_depth2);bstree2.Traversal(1);return 0;
}} // namespace binary_search_tree_
支持Linux和Windows直接编译,Windows通过VS,linux下执行prj/linux_cmake_CppBaseTest/build.sh脚本。执行结果如下:
保存的binary_search_tree.model结果如下:
7,4
0,1004,Tom,8,Beijing
0,1003,Mark,6,Hebei
0,1009,Lisa,11,Beijiing
0,1001,Viner,6,Beijing
-1,-1, ,-1,
0,1007,Piter,4,Hebei
-1,-1, ,-1,
-1,-1, ,-1,
0,1002,Eillen,14,Shanxi
-1,-1, ,-1,
-1,-1, ,-1,
-1,-1, ,-1,
0,1008,Lorena,22,Hebie
-1,-1, ,-1,
-1,-1, ,-1,
GitHub: https://github.com/fengbingchun/Messy_Test
相关文章:

swift实现ios类似微信输入框跟随键盘弹出的效果
为什么要做这个效果 在聊天app,例如微信中,你会注意到一个效果,就是在你点击输入框时输入框会跟随键盘一起向上弹出,当你点击其他地方时,输入框又会跟随键盘一起向下收回,二者完全无缝连接,那么…

行人被遮挡问题怎么破?百度提出PGFA新方法,发布Occluded-DukeMTMC大型数据集 | ICCV 2019...
作者 | Jiaxu Miao、Yu Wu、Ping Liu、Yuhang Ding、Yi Yang译者 | 刘畅编辑 | Jane出品 | AI科技大本营(ID:rgznai100)【导语】在以人搜人的场景中,行人会经常被各种物体遮挡。之前的行人再识别(re-id)方法…

WinAPI: Arc - 绘制弧线
为什么80%的码农都做不了架构师?>>> //声明: Arc(DC: HDC; {设备环境句柄}X1, Y1, X2, Y2, X3, Y3, X4, Y4: Integer {四个坐标点} ): BOOL;//举例: procedure TForm1.FormPaint(Sender: TObject); constx1 10;y1 10;…

提高C++性能的编程技术笔记:跟踪实例+测试代码
当提高性能时,我们必须记住以下几点: (1). 内存不是无限大的。虚拟内存系统使得内存看起来是无限的,而事实上并非如此。 (2). 内存访问开销不是均衡的。对缓存、主内存和磁盘的访问开销不在同一个数量级之上。 (3). 我们的程序没有专用的CPUÿ…

2019年不可错过的45个AI开源工具,你想要的都在这里
整理 | Jane 出品 | AI科技大本营(ID:rgznai100)一个好工具,能提高开发效率,优化项目研发过程,无论是企业还是开发者个人都在寻求适合自己的开发工具。但是,选择正确的工具并不容易,有时这甚至是…

swift中delegate与block的反向传值
swift.jpg入门级 此处只简单举例并不深究,深究我也深究不来。对于初学者来说delegate或block都不是一下子能理解的,所以我的建议和体会就是,理不理解咱先不说,我先把这个格式记住,对就是格式,delegate或blo…

Direct2D (15) : 剪辑
为什么80%的码农都做不了架构师?>>> 绘制在 RenderTarget.PushAxisAlignedClip() 与 RenderTarget.PopAxisAlignedClip() 之间的内容将被指定的矩形剪辑。 uses Direct2D, D2D1;procedure TForm1.FormPaint(Sender: TObject); varcvs: TDirect2DCanvas;…

女朋友啥时候怒了?Keras识别面部表情挽救你的膝盖
作者 | 叶圣出品 | AI科技大本营(ID:rgznai100)【导读】随着计算机和AI新技术及其涉及自然科学的飞速发展,整个社会上的管理系统高度大大提升,人们对类似人与人之间的交流日渐疲劳而希望有机器的理解。计算机系统和机械人如果需要…

提高C++性能的编程技术笔记:构造函数和析构函数+测试代码
对象的创建和销毁往往会造成性能的损失。在继承层次中,对象的创建将引起其先辈的创建。对象的销毁也是如此。其次,对象相关的开销与对象本身的派生链的长度和复杂性相关。所创建的对象(以及其后销毁的对象)的数量与派生的复杂度成正比。 并不是说继承根…

swim 中一行代码解决收回键盘
//点击空白收回键盘 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { view.endEditing(true) }

WinAPI: SetRect 及初始化矩形的几种办法
为什么80%的码农都做不了架构师?>>> 本例分别用五种办法初始化了同样的一个矩形, 运行效果图: unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls;typeTForm1 class(TForm)Butto…

Windows10上使用VS2017编译OpenCV3.4.2+OpenCV_Contrib3.4.2+Python3.6.2操作步骤
1. 从https://github.com/opencv/opencv/releases 下载opencv-3.4.2.zip并解压缩到D:\soft\OpenCV3.4.2\opencv-3.4.2目录下; 2. 从https://github.com/opencv/opencv_contrib/releases 下载opencv_contrib-3.4.zip并解压缩到D:\soft\OpenCV3.4.2\opencv_contrib-3…

swift 跳转网页写法
var alert : UIAlertView UIAlertView.init(title: "公安出入境网上办事平台", message: "目前您可以使用网页版进行出入境业务预约与查询,是否进入公安出入境办事平台?", delegate: nil, cancelButtonTitle: "取消", o…

智能边缘计算:计算模式的再次轮回
作者 | 刘云新来源 | 微软研究院AI头条(ID:MSRAsia)【导读】人工智能的蓬勃发展离不开云计算所带来的强大算力,然而随着物联网以及硬件的快速发展,边缘计算正受到越来越多的关注。未来,智能边缘计算将与智能云计算互为…

WinAPI: 钩子回调函数之 SysMsgFilterProc
为什么80%的码农都做不了架构师?>>> SysMsgFilterProc(nCode: Integer; {}wParam: WPARAM; {}lParam: LPARAM {} ): LRESULT; {}//待续...转载于:https://my.oschina.net/hermer/blog/319736

提高C++性能的编程技术笔记:虚函数、返回值优化+测试代码
虚函数:在以下几个方面,虚函数可能会造成性能损失:构造函数必须初始化vptr(虚函数表);虚函数是通过指针间接调用的,所以必须先得到指向虚函数表的指针,然后再获得正确的函数偏移量;内联是在编译…

ICCV 2019 | 无需数据集的Student Networks
译者 | 李杰 出品 | AI科技大本营(ID:rgznai100)本文是华为诺亚方舟实验室联合北京大学和悉尼大学在ICCV2019的工作。摘要在计算机视觉任务中,为了将预训练的深度神经网络模型应用到各种移动设备上,学习一个轻便的网络越来越重要。…

oc中特殊字符的判断方法
-(BOOL)isSpacesExists { // NSString *_string [NSString stringWithFormat:"123 456"]; NSRange _range [self rangeOfString:" "]; if (_range.location ! NSNotFound) { //有空格 return YES; }else { //没有空格 return NO; } } -(BOOL)i…

理解 Delphi 的类(十) - 深入方法[23] - 重载
为什么80%的码农都做不了架构师?>>> {下面的函数重名, 但参数不一样, 此类情况必须加 overload 指示字;调用时, 会根据参数的类型和个数来决定调用哪一个;这就是重载. }function MyFun(s: string): string; overload; beginResult : 参数是一个字符串: …

玩转ios友盟远程推送,16年5月图文防坑版
最近有个程序员妹子在做远程推送的时候遇到了困难,求助本帅。尽管本帅也是多彩的绘图工具,从没做过远程推送,但是本着互相帮助,共同进步的原则,本帅还是掩饰了自己的彩笔身份,耗时三天(休息时间…

提高C++性能的编程技术笔记:临时对象+测试代码
类型不匹配:一般情况是指当需要X类型的对象时提供的却是其它类型的对象。编译器需要以某种方式将提供的类型转换成要求的X类型。这一过程可能会产生临时对象。 按值传递:创建和销毁临时对象的代价是比较高的。倘若可以,我们应该按指针或者引…

北美欧洲顶级大咖齐聚,在这里读懂 AIoT 未来!
2019 嵌入式智能国际大会即将来袭!购票官网:https://dwz.cn/z1jHouwE随着海量移动设备的时代到来,以传统数据中心运行的人工智能计算正在受到前所未有的挑战。在这一背景下,聚焦于在远离数据中心的互联网边缘进行人工智能运算的「…

c# 关闭软件 进程 杀死进程
c# 关闭软件 进程 杀死进程 foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("Server")){p.Kill();} 转载于:https://www.cnblogs.com/lxctboy/p/3999053.html

提高C++性能的编程技术笔记:单线程内存池+测试代码
频繁地分配和回收内存会严重地降低程序的性能。性能降低的原因在于默认的内存管理是通用的。应用程序可能会以某种特定的方式使用内存,并且为不需要的功能付出性能上的代价。通过开发专用的内存管理器可以解决这个问题。对专用内存管理器的设计可以从多个角度考虑。…

【Swift】 GETPOST请求 网络缓存的简单处理
GET & POST 的对比 源码: https://github.com/SpongeBob-GitHub/Get-Post.git 1. URL - GET 所有的参数都包含在 URL 中 1. 如果需要添加参数,脚本后面使用 ? 2. 参数格式:值对 参数名值 3. 如果有多个参数,使用 & 连接 …

深度CTR预估模型的演化之路2019最新进展
作者 | 锅逗逗来源 | 深度传送门(ID: deep_deliver)导读:本文主要介绍深度CTR经典预估模型的演化之路以及在2019工业界的最新进展。介绍在计算广告和推荐系统中,点击率(Click Through Rate,以下简称CTR&…
2015大型互联网公司校招都开始了,薪资你准备好了嘛?
2015年的校招早就开始了,你还不知道吧?2015年最难就业季来了,你还没准备好嘛?现在就开始吧,已经很多大型互联网公司祭出毕业生底薪了看谷歌、看百度、看腾讯、看阿里巴巴再看传统软件公司:看微软、看联想、…

提高C++性能的编程技术笔记:多线程内存池+测试代码
为了使多个线程并发地分配和释放内存,必须在分配器方法中添加互斥锁。 全局内存管理器(通过new()和delete()实现)是通用的,因此它的开销也非常大。 因为单线程内存管理器要比多线程内存管理器快的多,所以如果要分配的大多数内存块限于单线程…

iOS中几种定时器
一、NSTimer 1. 创建方法 NSTimer *timer [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:selector(action:) userInfo:nil repeats:NO];TimerInterval : 执行之前等待的时间。比如设置成1.0,就代表1秒后执行方法target : 需要执行方法的对象…

手把手教你使用Flask轻松部署机器学习模型(附代码链接) | CSDN博文精选
作者 | Abhinav Sagar翻译 | 申利彬校对 | 吴金笛来源 | 数据派THU(ID:DatapiTHU)本文旨在让您把训练好的机器学习模型通过Flask API 投入到生产环境 。当数据科学或者机器学习工程师使用Scikit-learn、Tensorflow、Keras 、PyTorch等框架部署…