10. Tree and tree storage structure

Source: Internet
Author: User

first, the tree 1. Tree definition : The tree is a finite set of n (n>=0) nodes , where n=0 is called an empty tree. In any non-empty tree: (1) There is only one specific node called the root, and (2) when n>1, the remaining nodes can be divided into m (m>0) disjoint finite set T1, T2 、...、 Tm, each of which is itself a tree, and is called the subtree of the root (subtree )。 Note: When m>0, there is no limit to the number of subtrees, but they must be disjoint. 2. The degree of the node and the degree of the treethe nodes of a tree contain a data element and several branches that point to its subtree. (1) node degree : The node has a sub-tree called the Node degree (degree), where the degree of 0 of the node is called the leaf node or terminal node; (2) The degree of the tree: the degree of the tree is the maximum number of nodes in the tree;
3. Depth of treethe level of the node, that is, from the beginning of the definition, the root is the first layer, the root of the child is the second layer, etc... The maximum level of nodes in a tree is called the depth (Depth) or height of the tree.
If the subtree of the nodes in the tree is viewed from left to right and is not interchangeable, then the tree is called an ordered tree, otherwise it is called an unordered tree. II. abstract data types for treesADT Trees (tree)DataA tree is composed of a root node and several subtrees. Nodes in the tree have the same data type and hierarchical relationships. OperationInittree (*t): Constructing an empty tree TDestroytree (*t): Destroy Tree TCreatetree (*t,definition): constructs the tree according to the definition of the tree given in the definition;Cleartree (*t): If the tree t exists, the tree T is cleared to an empty treeTreeempty (t): If T is an empty tree, returns true, otherwise false;treedepth (t): Returns the depth of T    ......Deletechild (*t,*p,i): where P points to a node of the tree T, I is the degree of P of the node being referred to, and the result of the operation is to delete the point of P in Tthe sub-tree of the first point. Endadt three, the storage structure of the treebecause a child of a node in the tree can have more than one, regardless of the order (sequential storage or chained storage) to store all nodes in the tree into an array, the node's storage location cannot directly reflect the tree's logical relationship . Therefore, we can not simply use sequential storage or chain storage to store the tree, we could take full advantage of the two storage structure features, to achieve the tree storage structure of the representation, from the node: parents, children, brothers to start. 1. Parental representation (1) Core ideaswe assume that a set of contiguous spaces stores the nodes of the tree , and that each node is accompanied by an indicator indicating its parent node to the linked list , that is, each node, in addition to who it is, knows where its parents are, and that is the parent representation of the tree. (2) node structure
The Data tree node is used to store the data information of the node, and the parent is the pointer field that stores the subscript of the parent of the node in the array. The definition code for the node structure with the parent notation is as follows:
<span style= "FONT-SIZE:18PX;" Parent representation of >/* tree node structure definition */#define MAX_TREE_SIZE        //array storage space size is 100typedef int telemtype;                      The data type of the tree node is currently tentative for integer/* node structure */typedef struct Ptnode                      {    telemtype.    Node data (data field)    int parent;                Parent position (pointer field)}ptnode;/* tree structure */typedef struct{    ptnode nodes[max_tree_size];    Node Array (node number of the tree)    int r,n;        Root position and node count}</span>
Note: Since there are no parents with the node, we agree that the Location field (pointer field) of the root node is set to-1. (3) Example of parent-node notation
from the above example we can see that it is easy to find its parent node according to the parent pointer of the node, so the time complexity is O (1), and until the parent is-1, the root of the tree node is found . In addition, we can add a node to the left child's pointer field or right sibling pointer field for the node to solve the problem of finding a child or sibling. 2. Child notation      in each node in the tree, the general situation would be to consider multi-link table notation : Each node has multiple pointer fields, where Each pointer points to the root node of a subtree of that node . But The degree of each node (number of children) is different , set Two kinds of solution: One is the degree to which the number of pointer fields in the design node equals the tree; (1) Core ideasto traverse the entire tree that is stored in a sequential storage structure array, we create a single linked list for each node's child to reflect the relationship between the children of the node. The expression is: the Child node of each node is arranged in order to single- linked table as a storage structure, n nodes have n children linked list, if it is a leaf node, this single linked list is empty. Then -head pointer then forms a linear table , storing the result sequentially, storing a one-dimensional array. (2) node structure >>>> children 's linked list of children's knot points: , child is the data field that stores the subscript of a node in the table header array, and next is a pointer field that stores a pointer to the next child node of a node.          >>>> header nodes for header arrays: , where data is the data field that stores the information for a node, and the parent is the parent of the pointer field pointing to a node of the table header (without increasing the parent pointer field); FirstChild is the head pointer field that stores the child's list of children in the structure. define the code with the structure of the child notation (figure I):
/* Child notation definition for tree */#define MAX_TREE_SIZE    //Storage space *//child node */typedef struct ctnode{    int.                        Data field: An array of nodes in the table header array subscript    struct Ctnode *next;   Pointer field: Pointer to the next child node of the node} *childptr;/* table Header node */typedef struct{    telemtype data;    Data field    childptr firstchild;} ctbox;/* tree structure */typedef struct{    ctbox nodes[max_tree_size];    Node array    int r,n;    Root position and node count} ctree;    
(3) Examples of child knot representationssuch a structure for us to find a child, or find a node of the brothers, only need to find the node of the child single-linked list. It is also convenient to traverse the entire tree, that is, the array loop of the head node (figure I). Of course, in addition to finding a child and brother of a node, we can also implement finding the parent of that node , as shown in two. 3. Child Brother representation (1) Core ideasany tree, the first child of any of its nodes if existence is the only one, its right brother If existence is also unique. Therefore, we set two pointers to the first child of the node and the right sibling of the node, respectively. (2) node structure    where data is the field, FirstChild is the pointer field, storing the address of the first child node of the node, and the Rightsib pointer field, which stores the address of the right sibling node of the node. Node structure definition code:
<span style= "FONT-SIZE:18PX;" >/* Tree child Brother representation structure definition */typedef struct csnode{    telemtype data;                            Data domain    struct csnode *firstchild,*rightsib;//pointer field}csnode,*cstree;</span>
(3) Examples of child brother representations
It can be concluded that when we look for a child of a node , we simply find the eldest son of the node through Fitstchild, and then find its second brother through the rightsib of the eldest son node, and then go on until the child is found. In addition, we can add a parent pointer field to the node to implement a quick lookup for the parent of that node. Summary: Child brother notation The biggest thing about it is that it turns a complex tree into a two-fork tree , and then we can take advantage of binary tree properties and algorithms to handle the tree.

10. Tree and tree storage structure

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.