Note-taking data structure-Nonlinear Structure

Source: Internet
Author: User

The non-linear structure in the Data Organization refersTreeAndFigure.

Tree structure is very useful. A data element in this structure can have two or more direct successor elements, which can be used to describe the widespread hierarchical relationships in the objective world.

A tree is composed of nodes. The root of the Child tree of a node is the child of the node. The node is called the parent of its child node, and the nodes with the same parent and parent are brothers to each other. For example, E and F are B's children, B is E and F's parents, and E and F are mutual brothers. The number of Subtrees of a node is recorded as the degree of the node. A node with zero degree is also called a terminal node, such as E, F, C, and G; nodes whose degree is not zero are called branch nodes or non-terminal nodes. The root is the first layer, the root is the second layer, and so on. The maximum number of layers of a tree is the height of the tree.

The tree with the maximum node size of 2 is a binary tree, which differentiates the left and right subtree in the binary tree-even if the node has only one subtree.

The binary tree storage structure is also divided into sequential storage and chain storage.

Binary Tree for sequential storage:

In the worst case, a binary tree (single-tree) with a depth of K and only K nodes requires 2 ^ k-1 storage unit.

Binary Tree for chain storage:

The Node Type of the binary linked list can be defined:

 

typedef struct BiTnode{    int data;    struct BiTnode *lchild, *rchild;}BiTnode, *BiTree;

Binary tree traversal:

void InOrder(BiTree root){    if(root==NULL)        return;    else    {        InOrder(root->lchild);        printf("%d\n", root->data);        InOrder(root->rchild);    }}

Central traversal: the access root node is placed between the left subtree traversal and the right subtree traversal; Forward traversal: the access root node is placed before the left subtree traversal; subsequent operations: the access root node is placed after the right subtree traversal.

Regardless of the traversal, for a Binary Tree Containing N nodes, the time complexity of the traversal algorithm is O (n ).

 

Several special Binary Trees:

1. The optimal binary tree, also known as the Harman tree, has the shortest length of the weighted path. The length of the tree's weighted path is the sum of the length of the weighted path of all leaf nodes in the tree, the length of a node's weighted path is the product of the node's right and the path length between the node and the root.

To construct an optimal binary tree:

1. Based on the given n weights {W1, W2, W3 ,..., wn} is a set of N Binary Trees F = {T1, T2, T3 ,..., tn}, where each tree TI has only one root node with the WI permission, and its left and right subtree is empty.

2. Select the tree with the minimum weight of the two root nodes as the left and right Subtrees in F to construct a new binary tree, the root node weights of the newly constructed binary tree are the sum of the weights of left and right child root nodes.

3. Delete the two trees in F and add the new binary tree to F.

Repeat steps 2 and 3 until F contains only one tree, which is the optimal binary tree.

This method is used for coding and decoding.

According to this method, for example, a character set {a, B, c, d, e} and the corresponding weight set {0.25, 2.30, 0.12, 0.25 }, A user-defined tree can be constructed as follows:

Encoding result: a 00, B 01, C 100, d 11, E 101.

2. Binary Search Tree

Property: if its left subtree is not empty, the values of all nodes on the left subtree are smaller than those of the root node;

If its right subtree is not empty, the value of all nodes on the right subtree is greater than the value of the root node;

The left and right subtree are two binary search trees.

Traverse the binary search tree in the middle order to obtain an incremental and ordered node sequence.

 

To study the figure.

Graph G is a binary group composed of two sets V and E. It is recorded as G = (V, E), and V is a non-empty finite set of vertices in the graph, E is a finite set of edges in the graph. The data elements in the figure are represented by vertices, and the relationship between data elements is represented by edges.

Directed Graph: each edge in the graph has a direction.

Undirected graph: each side in the graph has no direction.

Full graph: an undirected graph of N vertices with edges between each vertex and other n-1 vertices. It is called a undirected full graph (n vertices, n (n-1)/2 edges ); A directed graph with n vertices and an edge between each vertex and other n-1 vertices is called a directed complete graph (n vertices, n (n-1) edges ).

Degree: the degree of a vertex refers to the number of edges associated with the vertex. If it is a directed graph, the degree is the sum of the inbound and outbound degrees, the indegree of a vertex is the number of directed edges that end with the vertex. The outdegree of a vertex is the number of directed edges starting with the vertex.

Path :... If it is a directed graph, its path also has a direction. The path length is the number of top paths or arcs. The path of the first vertex and the last vertex is called a loop or loop. If a path has the same starting point and ending point, the other vertices are not the same. It is called a simple path.

Subgraph:

Connected Graph: In an undirected graph, if two vertices have paths, the two vertices are connected. If any two vertices in an undirected graph are connected, they are called connected graphs.

Strongly Connected Graph: In a directed graph, A and B have paths from vertex A to vertex B and from vertex B to vertex A, which is called a strongly connected graph.

Network: the graph of the edge (or arc) weight value is called a network.

 

Figure storage structure:

1. Adjacent matrix representation

For a graph with n vertices, Its Adjacent matrix is an n-order matrix that satisfies

The adjacent matrix of an undirected graph is symmetric.

Correspondingly, the data organization of the graph represented by the adjacent matrix can be defined:

#define MaxN 30typedef int AdjMatrix[MaxN][MaxN];

2. Adjacent linked list Representation

A single-chain table is created for each vertex of the graph. the nodes in the I-th single-chain table are the edges attached to the vertex VI (for Directed Graphs, the arc ending with VI)

The corresponding data types can be defined as follows:

# Define maxn 30 typedef struct arcnode/* Table node */{int adjvex;/* vertex number */double weight;/* Edge Weight */struct arcnode * nextarc; /* Next vertex */} edgenode; typedef struct vnode/* Header node */{char data;/* data indicated by vertex */struct arcnode * firstarc; /* pointer to the first arc attached to the vertex */} adjlist [maxn]; typedef struct {int vnum; adjlist vertices;} graph;

For an undirected graph with n vertices and e edges, the adjacent linked list requires n header nodes and 2e table nodes. For example:


As you can see, for an undirected list, the degree of vertex VI is exactly the number of table nodes in the I-th list.

For Directed Graphs, you must create a list of inverse adjacent links for the graph at the same time,

 

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.