Redis Research-3.3 data structure tree and find, sort, etc.

Source: Internet
Author: User

1. Tree-related content 1.1 tree is a finite set of n (n>=0) nodes. When we n=0, we called it an empty tree. Meet two features in any non-empty tree: (1) and there's only one node called the root.。 (2) When n>1, the remaining nodes can be divided into m (m>0) Non-intersectingThe finite set T1,t2,...      Each of these combinations itself is also a tree. The concept above uses a recursive definition.
Related concepts of the tree: the degree of the node: Refers to the number of sub-trees of this node. the degree of the tree: Refers to the degree of the node in the tree that has the most amount of nodes. Node's relationship: The root of a node's subtree is called the node's ChildrenThat, accordingly, the node became the child's Parents(parents in the same body). Between the children of the same parents is called Brother, the ancestor of the node is all nodes on the branch from the root node to the node. Any node in a subtree that is rooted in a node is the node's DescendantsHierarchy of Nodes: The hierarchy of nodes is defined from the root, the root is the first layer, the child of the root is the second layer, and so on. The parents are cousins on the same level as each other. the depth of the tree: The maximum level of a node in a tree is called the depth or height of the tree.   We use a tree to understand the concept above. The degree of node D is 3 because he has 3 subtrees and the whole tree is 4 because the maximum of all nodes in the whole tree is G, and he has 4 subtrees. Nodes D, e are called brothers. D, E, F are cousins, and the depth of the tree is 4. 1.2 Tree structure and storage through the above description, we know that a tree is composed of nodes, so the tree node is the most basic constituent units, then, a tree node how to say or how to constitute it? We know that the node of a tree can not be jumped out of the stone (except with the elements), so he must exist both parents, at the same time, he may exist in the subtree (this refers to the child node). So, we can get the basic node structure as follows:
typedef struct treenode{    struct TreeNode *parent;//parent    struct TreeNode **childs;//child array    int childcount;// Number of children    void *data;//node data}treenode;



However, we simply from the node itself, the above representation method, there is a lot of redundant information, so, we from the most common node information, that is, for the node, we only consider his parents and their own data, therefore, can be expressed as the following structure:
typedef struct snode{    struct SNode *parent;//parent    void *data;//node data}snode;



The parents of the above code, we are represented by pointers, in order to simplify our formulation, we can replace the above-mentioned two-parent pointers to an integer type of positional data to represent (the root node location data is-1).
typedef struct nnode{   int parentposition;//parent    void *data;//node data    int32_t curposition;//current node coordinates}nnode;
So, for the tree we mentioned at the beginning of this chapter, we can represent the following table:
Curpositon data parent
0 A /td> -1
1 B 0
2 C 0
3 D 1
4 E 1
5 F 2
6 G 2
7 H 3
8 I 3
9 3
K 6
one L 6
M 6
all N 6
This method of expression is very convenient for us to find the parents of a certain node, but you can think about it, it is not very laborious to find the child of a certain node, so we need to do some improvement, that is to say, we should consider the problem of the degree of the node, and after the improvement, we get the following node structure definition:
typedef struct cpnode{    int32_t parentposition;//parent    void *data;//node data    int32_t curposition;//current node coordinates    int32_t *childs;//node's child position coordinate array    int32_t degree;//node's degree}cpnode;



With this improvement, we can quickly find both parents and subtrees on a node. To do this, we can get the structure definition of the final tree:
typedef struct tree{  int32_t tail;//Tree The number of nodes of the root node  int32_t nodecount;//The node count of the tree  struct cpnode *nodes;//node array}tree ;



Well, let's extend this to the general situation, and get a regular representation of tree nodes and the way trees are represented:



typedef struct GTREENODE{VOID *data;//node data int32_t degree;//node degree struct Gtreenode *parent;//parent, forward pointer struct Gtreenode * NEXT;//uses linked lists to store sibling nodes, and backward pointers to}gtreenode;



Tree structure definition typedef struct G{STRUCT gtreenode *tail;int32_t nodes;//tree node number}gtreenode;



OK, the structure of the tree has been basically completed, the following is the definition of some conventional tree (no matter how special he is, you can use the relevant representation of the above tree) 2. Two fork Tree Two fork tree is a restriction on the definition of a general tree, the restriction is that the degree of a node cannot exceed 2. This is the binary tree.     Within the binary tree there are also concentrated special two-fork trees: 2.1 Oblique tree: Refers to the node of the two-fork tree or only the left dial hand tree, or only the case of the right subtree, is called the oblique tree; 2.2 Full two fork tree: refers to the binary tree, if all the branch nodes exist Saozi right subtree, and all the leaf nodes are on the same layer. 2.3 Complete Binary tree: This special column is defined in relation to a two-fork tree, meaning that, for a two-fork tree with n nodes, if the node numbered I is exactly the same as the position in the two-tree of the node numbered I in the same depth of the two-fork tree, then this is the binary tree, in other words, A complete binary tree is a part of a two-fork tree, and all the leaf nodes are at the bottom of the two layers, and the lowest leaf node, must be from the leftmost continuous start, if the penultimate layer has leaf nodes, then these nodes should be in the right side of the continuous position, if the degree of the node is 1, Then this node can only have one left child. So, what is the nature of the binary tree? Property 1: There are at most one node on layer I of the two fork tree. Property 2: Two fork tree with a depth of K, up to 1 nodes. Layer: 2^0=1=2^0=2^1-1 Two floor: 1+2^ (2-1) =1+2^1=2^2-1 three: 2^2-1+2^ (3-1) =2^3-1 Property 3: If the end node of a binary tree is m, the number of nodes with a degree of 2 is N, then, m=n+1;How is this property obtained?   We know that for a binary tree, the tree node is divided into three cases: the degree of 1 node n0, leaf node m, the degree of 2 node n, then the number of nodes t=n0+m+n tree. At the same time, we know that the number of connections between all two-fork tree nodes equals the total number of nodes minus 1. At the same time, we also know that for a degree of 2 of the node, his line should be 2, so the degree of 2 of the node corresponding to the connection should be =2n. And for the end node is no outgoing, for the degree of 1 of the node, his outgoing only 1, therefore, from the point of view of the number of nodes t=2n+n0+1, so, Can get n0+m+n=2n+n0+1, thus get m=n+1;     Property 4: The depth of a fully binary tree with n nodes is +1.How did you figure that out?       We know that the number of nodes with a two-fork tree is n=-1, where k is the degree of this binary tree, so here's the k=.     According to the definition of a complete binary tree, his number of nodes can be equal to the same depth of two tree node number 1, but must be greater than. That is, because n must be an integer, you can take the logarithm to get the above conclusion. 2.4 Two fork tree structure and storage through the above learning, we know that the nodes in the binary tree should have four properties: Data field, left child, right child, parent, so for a binary tree, we can redefine the definition of the node structure of the tree mentioned above:
typedef struct bitnode{    void *data;//data domain    struct bitnode *left;//left child    struct Bitnode *right;//right child    struct Bitnode *parent;//parents}bitnode;



2.5 Traversing the binary tree learning here, the tree and the two-tree related concepts and definitions have been taken care of, so, for a tree, we have to traverse the nodes, how to do?  I remember when I was in the undergraduate course, there is a test is the examination of this topic, NND, was not very clear, the results are conceivable. When traversing a binary tree, the method we use depends on the direction we choose. Let's look at a binary tree, and then a way to see how it's going to come true.
Binary tree, node definition typedef struct bitnode{   void *data;//data domain    struct bitnode *left;//left child    struct Bitnode *right;//right child    struct Bitnode *parent;//Parent} Bitnode,*bitree;
The rule of 2.5.1 pre-sequence traversal is to start from the root node and if the binary tree is empty, return directly, otherwise access the root node first, then the pre-order traverses the left subtree, and then the pre-sequence traverses the right subtree. The result is: abdhiejcfg
Pre-order traversal binary tree void Preordertraverse (Bitree t) {    if (null==t| | Null==t->data) return;    operate each node, such as print node    printf ("%s", t->data);    Preordertraverse (t->left);    Preordertraverse (t->right);}


2.5.2 Middle Sequence traversal

The rule of the middle order traversal is to start from the root node, if the binary tree is empty, then return directly, otherwise from root node Start (not first access root node), the middle sequence traverses the left subtree of the root node, then accesses the root node and finally accesses the right subtree of the root node. The end result is HDIBJEAFCG.
Middle order traversal binary tree void Inordertraverse (Bitree t) {    if (null==t| | Null==t->data) return;    Inordertraverse (t->left);    operate each node, such as print node    printf ("%s", t->data);    Inordertraverse (t->right);}



2.5.3 post-order traversal of the rule is that if the tree is empty, then directly return, otherwise left to right before the leaves of the node to access the tree, the last access to the root node. The final access result is: HIDJEBFGCA
Post-traversal binary tree void Inordertraverse (Bitree t) {    if (null==t| | Null==t->data) return;    Inordertraverse (t->left);    Inordertraverse (t->right);        Manipulate each node, such as print node    printf ("%s", T->data);}



2.5.4 sequence Traversal sequence traversal rule is that if the tree is empty, then return directly, or first from the root of the first layer, when accessing the same layer, first left and right.  We go back to the previously defined two-fork tree node and you will find that there is a very serious problem, what is the problem? The nodes we define have three fields, one is the left child pointer field, the other is the right child pointer field, and the other is the data field.   However, we look at a binary tree example, we can clearly find this approach, there is a great disadvantage. (Note: I was ignoring the situation of the parent-pointer domain of each node) and we see that the pointer field for many of the nodes above is not fully utilized.   We know that for a two-fork tree with n nodes, there are a total of 2n pointer fields, and n-1 lines, that is, there is 2n-(n-1) =n+1 a null pointer field, the tree node above the null pointer field has 11 null pointer field. We use the sequence throughout the history of the two-fork tree, the result is HDIBJEAFCG, the problem comes, you can easily know any node of the precursor and successor? If you want to know, we have to go through the first to know, so we can imagine, when the node is established, is it possible to explicitly label it? We call this precursor and successor a tree of clues two. After the improvement, the structure definition of the node we get becomes the following definition:
/*** link=0 indicates that the left and right child pointer is marked *thread=1 indicates a precursor or successor pointer */typedef enum {link,thread} tag;typedef struct tagbitreenode{    struct Tagbitreenode *left;    struct Tagbitreenode *right;    void *data;    Tag Ltag;    Tag Rtag;} Tagbitreenode,*tagbitree;



OK, the content of the binary tree is basically settled, but you find that the implementation of our target is based on binary tree, but how much of our life is to meet the binary tree structure?   Can we turn conventional trees into two-fork trees? 3. Tree, forest, binary tree conversion I put the following content in the following chapters of this chapter, it seems that the content of this chapter is too many things may not be clear, welcome to comment, at the same time can add qq:359311095 discussion

Redis Research-3.3 data structure tree and find, sort, etc.

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.