Using MySQL data and data structures and two-fork tree algorithms in python environments

Source: Internet
Author: User
Tags mysql in

Using MySQL data and data structures and two-tree algorithms in Python environments (figure):
1 using MySQL in the Python environment
2 is using the Pymysql library
3 Start-to-create connection--> get cursor--> action-close cursor-> close connection-> end
4
5 Code Framework
6 Import Pymysql.cursors
7 # # #连接数据库
8 connection = Pymysql.connect (host= ' 127.0.0.1 ', port=3306,user= ' root ',
9 password= ' ... ', db=database_name,charset= ' uft8mb4 ', cursorclass=pymysql.cursors.dictcursor)
# #创建游标
cursor = Connection.cursor ()
12
# #执行操作
Sql= ' operation statement, MySQL syntax '
15 Create, delete, query, add, modify, etc...
# #执行
Cursor.execute (SQL)
18
# #提交到数据库
Connection.commit ()
21st
# #关闭连接
Cursor.close ()
Connection.close ()
####### #操作语句
26 inserting data the same as MySQL
27 Querying data
Sql= ' select ' id ', ' password ' from ' Users (table name) ' Where ... '
Cursor.execute (SQL)
result = Cursor.fetchone () #单条数据查询
Cursor.fetchone () #多条数据查询
Print (result)/for data in Result:print (data) #显示在python输出结果里

Python data structure and two-fork tree algorithm:
A tree is an abstract data type (ADT) or a data structure of an abstract data type that simulates a collection of data of a tree-like nature. It is a set of hierarchical relationships consisting of n (n>=1) finite nodes. It's called a "tree" because it looks like an upside down tree, which means it's rooted up and leaves facing down.
Terminology of the tree
Node Degree: The number of sub-trees that a node contains is called the degree of the node;
Degree of the tree: the degree of the largest node in a tree is called the degree of the tree;
leaf node or end node: a zero-degree node;
Father node or parent node: If a node contains child nodes, this node is called the parent node of its child nodes;
child node or child node: the root node of a subtree containing a node is called a child node of that node;
Sibling nodes: nodes with the same parent node are called sibling nodes;
The hierarchy of nodes: From the beginning of the root definition, the root is the 1th layer, the root of the child node is the 2nd layer, and so on;
The height or depth of a tree: the maximum level of nodes in a tree;
Cousin node: The parent node in the same layer of the nodes are each other cousins;
Ancestor of a node: all nodes from the root to the branch of the node;
Descendants: Any node in a subtree that is rooted in a node is known as the descendant of that node.
Forest: The collection of trees that are disjoint by M (m>=0) is called a forest;
Types of Trees
Unordered tree: There is no sequential relationship between the child nodes of any node in the tree, which is called the unordered tree, also known as the free tree;
Ordered tree: There is a sequential relationship between the child nodes of any node in the tree, which is called ordered tree;
Binary tree: A tree with a maximum of two subtrees per node is called a binary tree;
Complete binary tree: For a binary tree, suppose its depth is D (d>1). In addition to layer D, the number of nodes in each layer has reached the maximum, and all nodes of layer d are continuously arranged from left to right, so that the two-fork tree is called a complete binary tree, where the definition of full two-tree is the complete binary tree with all the leaf nodes at the bottom;
Balanced binary tree (AVL tree): When and only if the height difference of the two subtrees trees of any node is not greater than 1 of the two-fork tree;
Sort binary tree (binary lookup tree (English: Binary search tree), also known as binary searching trees, ordered binary tree);
Hoffman (For information coding): The shortest two-tree with the right path is called Huffman tree or optimal binary tree;
B-Tree: A self-balancing two-fork lookup tree optimized for read and write operations that keeps data in order and has two additional subtrees
Storage and representation of trees
Binary tree usually chain-store
Common Application Scenarios
1.xml,html and so on, then the parser that writes these things, inevitably uses the tree
2. The routing protocol is the algorithm that uses the tree
3.mysql Database Index
4. Directory structure of the file system
5. So many of the classic AI algorithms are actually tree searches, and the decision tree in machine learning is also the structure of trees
Two-fork Tree
A binary tree is a tree structure with a maximum of two subtrees per node. The subtree is often referred to as the left subtree and the right sub-tree (subtree
Nature (properties)
Property 1: There are at most 2^ (i-1) nodes on the first layer of the binary tree (i>0).
Property 2: Two fork tree with a depth of k at most 2^k-1 nodes (k>0)
Property 3: For any binary tree, if its leaf node is N0, and the number of nodes with a degree of 2 is N2, then n0=n2+1;
Property 4: The depth of a complete binary tree with n nodes must be log2 (n+1)
Property 5: For a complete binary tree, if numbered from top to bottom, from left to right, the node numbered I, the left child number must be 2i, the right child number must be 2i+1, the parents of the number must be I/2 (I=1 when the root, except)
Breadth-First traversal
Queue queues are generally used
Depth-First traversal
Depth-First search is a branch of the tree that traverses the tree's depth as deep as possible, Depth the tree.Guo Fu on reading notesAnd insights, three methods of first order traversal (preorder), Middle sequence Traversal (inorder) and post-order traversal (Postorder)
First order traversal root-left-right
In the sequence traversal, we first access the root node and then recursively use the first order traversal to access the left subtree, then recursively use the first order traversal to access the right subtree, the right subtree, left dial hand tree
Middle sequence traversal left-root-right
In the middle sequence traversal, we recursively use the middle order traversal to access the left subtree, then access the root node, and then recursively use the middle order traversal to access the right subtree.
Post-sequential traversal left-right-root
After the sequential traversal in the post-operation traversal, we first recursively use the post-traversal to access the Saozi right subtree, and finally access the root node.

Using MySQL data and data structures and two-tree algorithms in Python environments (figure)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.