Basic data structure notes (i) "Min"

Source: Internet
Author: User

Generalized tables

Generalized table-related concepts:
A1 (the first element in the table) is called a table header ;
A sub-table consisting of the remaining elements is called a footer ; (A2,a3,...,an)
The number of elements (including atoms and sub-tables) contained in a generalized table is called the length of the table.
The maximum number of brackets in a generalized table is called the table depth (degrees).

According to the definition of the table header and footer, any non-empty generalized table header can be either an atom or a child table, and the footer must be a generalized table.
As long as the generalized table is non-empty, it consists of a table header and a footer, that is, a definite table header and a footer are the only ones that determine a generalized table.

Generalized table Data node definitions:

typedefstruct GLNode{      int   tag ;     /*  标志域,为1:表结点;为0 :原子结点  */    union    {  elemtype value;     /* 原子结点的值域  */    struct        {  struct GLNode  *hp , *tp ;         }ptr ;   /*  ptr和atom两成员共用  */    }Gdata ; } GLNode ;      /* 广义表结点类型  */
Two-fork Tree

full binary tree with two forks
A two-fork tree with a depth of K and a 2^k-1 node is called a full two-tree.
Full binary tree (complete binary trees)
If the depth is K, the two-Tree of n nodes, when and only if each of its nodes corresponds to a node one by one in a full two-tree with a depth of K, the two-fork tree is called a complete binary tree.

A complete binary tree is part of a two-fork tree, and a full two-tree is a special case of a complete binary tree.

# # #遍历

    • Pre-order non-recursive traversal
      Ideas:
      1). Access the current node, using the stack to temporarily hold the right node of the current node "if not empty";
      2). If the left node of the current node is not empty, then go to continue access, otherwise access from the stack node, that is, return 1 to continue, if the stack is empty to end the traversal;
     vector<int>Preordertraversal (treenode* root) { vector<int>Ansif(!root)returnAns Stack<TreeNode*>Stk_tree; while(1) {Ans.push_back (root->val);if(root->right) Stk_tree.push (root->right);if(root->left) root = root->left;Else if(Stk_tree.empty ()) Break;Else{root = Stk_tree.top (); Stk_tree.pop (); }        }returnAns }
    • In-sequence non-recursive traversal
      Ideas:
      1). If p is not empty, the P-in-stack,p=p->lchild;
      2). Otherwise (that is, p is empty), go back to p, access the node to which P points; "Stack is empty then end"
      3). If the right node of P is not empty, p=p->rchild, turn 1;
     vector<int>Inordertraversal (treenode* root) { vector<int>Ansif(!root)returnAns Stack<TreeNode*>S while(Root)            {S.push (root);        root = root->left; } while(!s.empty ())            {TreeNode * t = s.top ();            S.pop (); Ans.push_back (T->val);if(T->right) {T = t->right; while(t)                    {S.push (t);                t = t->left; }            }        }returnAns }
    • Post-sequential non-recursive traversal
      Ideas:
      Here the flag stack corresponds to the node attribute, if the flag=1 indicates that the node is the parent node, it can be accessed directly after the stack, and if flag=0 indicates that the node is the right child node, the child node of the nodes needs to be put into the stack, and its own flag becomes 1.
      1). If p is not empty, p enters the stack "flag=1", and if P's right node is not empty, the right node goes into the stack "flag=0" and enters p=p->left;.
      2). Take a value from the stack, if flag=1, indicates that the node's child node has reached the end, the node can be accessed; otherwise, enter 1 to continue. "Stack is empty End"
     vector<int>Postordertraversal (treenode* root) { vector<int>Ansif(!root)returnAns Stack<TreeNode*>Stk_tree; Stack<int>Stk_flag; while(Root)            {Stk_tree.push (root); Stk_flag.push (1);if(Root->right) {Stk_tree.push (root->right); Stk_flag.push (0);        } root = root->left; } while(!stk_tree.empty ())            {root = Stk_tree.top (); Stk_tree.pop (); Flag = Stk_flag.top (); Stk_flag.pop ();if(!flag) { while(Root)                    {Stk_tree.push (root); Stk_flag.push (1);if(Root->right) {Stk_tree.push (root->right); Stk_flag.push (0);                } root = root->left; }            }Else{Ans.push_back (root->val); }        }returnAns }
Figure

In one path, if the same vertex is not repeated, the path is called a simple path ;
The same path as the first vertex and the last vertex is called the Loop (loop);
For undirected graph g= (v,e), if any vi,vj∈v,vi and VJ are connected, then the graph G is connected graph , otherwise called non-connected graph . If G is a non-connected graph, the maximal connected sub-graph is called the connected component of G;
To the direction of the graph g= (v,e), if any vi,vj∈v, have to VI as the starting point, VJ as the end and the VJ as the starting point, VI for the end of the direction of the path, said the figure G is a strong connected graph , otherwise known as non-strong connected graph . If G is a non-strong connected graph, the strongly connected sub-graph is called the strong connected component of G.

Minimum spanning tree
    • Primm (Prim) algorithm
      Algorithmic Thinking (Algorithmic complexity O (n^2)):
      1). If starting from the vertex v0 construction, u={v0},te={};
      2). First find the least-weighted edge (u,v), where U∈u and V∈v-u, and the sub-graph does not constitute a ring, then u= u∪{v},te=te∪{(U,V)};
      3). Repeat the ⑵ until u=v. There must be n-1 edges in TE, and t= (U,te) is the smallest spanning tree.

Topic Links: #1097: minimum spanning tree one · Prim algorithm
Defines a one-dimensional array that holds vertices in the v-u with the least weighted edge "lowcost": If Lowcost is 0, the lowcost of each vertex is updated in v-u if it is added to U.

struct {    int  adjvex;     /*   边所依附于U中的顶点   */    int  lowcost;    /*   该边的权值   */}closedge[N];
#include <stdio.h>#include <string.h>#define N 1002#define INFINITY 10005;struct{intAdjvex;/ * Edge is attached to the vertex in U * /    intLowcost;/ * Weight of the edge * /}closedge[n];int ArrayN [N];intPrimintN) {intI, J, Min, V, ans =0, IDX;//Initialize array "default starting from V0"     for(i =0; I < n; i++) {Closedge[i].adjvex =0; Closedge[i].lowcost =ArrayI [0]; }//n-1 Trip     for(j =0; J < N-1; J + +) {min = INFINITY;//Find the vertex of the minimum weight to u         for(v =0; v < n; v++) {if(Closedge[v].lowcost! =0&& closedge[v].lowcost < min) {min = Closedge[v].lowcost;            idx = v;        }} ans + = min; Closedge[idx].lowcost =0;//Add the vertex to u         for(v =0; v<n; v++)if(Array[V] [idx]<closedge[v].lowcost) {closedge[v].lowcost =Array[V]                [IDX];            Closedge[v].adjvex = idx; }/ * Modify the value of each element of the array closedge[n] * /}returnAns;}intMain () {intN, I, J;scanf("%d", &n); for(i =0; I < n; i++) for(j =0; J < N; J + +)scanf("%d", &ArrayI [j]);printf("%d\n", Prim (n));return 0;}

Note: The prim algorithm can be changed into a Dijkstra algorithm with slight modification. "Lowcost represents the shortest distance to each vertex to the order point. 】

    • Kruskal (Kruskal) algorithm
      Thought:
      G= (V, E) is a connected network with n vertices, and t= (U, TE) is its smallest spanning tree. Initial value: u=v,te={}. C
      The weights in g are selected from small to large.
      1). Select the edge with the least weight (VI,VJ), discard the Edge (VI,VJ) if the Edge (VI,VJ) is joined to the Te and form the loop, otherwise, merge the edge into Te, which is te=te∪{(VI,VJ)}.
      2). Repeat 1) until the TE contains a n-1 edge.

Topic Links: #1098: Minimum spanning tree two · Kruscal algorithm
Sort all the edges first, and then traverse to determine if the two vertices of the edge are in a set, and if the loop is formed, discard it, or change the edge to join TE. "Determine if two points are used in a set and check the set algorithm"

#include <iostream>#include <algorithm>using namespace STD;structedge{intUintVintLen;};intcmpConstEdge A,ConstEdge b) {returnA.len < B.len;}#define M 1000005#define N 100005Edge E[m];intPos[n];//Record the individual nodes in which collection. voidInitintN) { for(inti =1; I <= N; i++) Pos[i] = i;}intFindintx) {if(x = = Pos[x])returnXElse{Pos[x] = find (pos[x]);returnPOS[X]; }}intKruskal (intNintm) {intsum =0, Count =1; for(inti =0; I < m; i++) {intFX = Find (E[I].U);intFY = find (E[I].V);if(FX! = FY)            {sum + = E[i].len; count++;if(count = = N) Break;        POS[FX] = FY; }    }returnsum;}intMain () {intN, M; while(Cin>> n >> m) {init (n);//Initialize and check set         for(inti =0; I < m; i++)Cin>> e[i].u >> e[i].v >> E[i].len; Sort (E, E + M, CMP);cout<< Kruskal (n, m) << Endl; }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Basic data structure notes (i) "Min"

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.