Data structure reading notes (vi)--tree definition, abstract data type, storage structure

Source: Internet
Author: User
Tags abstract
Tree:
A tree is a finite set of n (n>=0) nodes. N=0 is called an empty tree, in any non-empty tree: (1) There is only one specific node called root (Root), (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 called the root of the subtree (subtree).
The definition of a tree also emphasizes two points:
1.n>0 The root node is unique, there can be no more than one root node, should be separated from the real tree
2.m>0, there is no limit to the number of subtrees, but they must be disjoint.

Node Classification:
A node-owned subtree is called the degree of Node (degree). A node with a degree of 0 is called a leaf node (leaf) or terminal node, and a node with a degree of 0 is called a non-terminal node or branch node. Root nodes, the branch node is also called an internal node. The degree of the tree is the maximum of the degree of each node within the tree.


Relationship between nodes:
The root of the node's subtree is called the child of the child, which, accordingly, is referred to as the parent (parent).
A sibling (Sibling) between a child of the same parent.
The ancestor of a node is all nodes from the root to the branch on which the node is branched.
Any node in a subtree that is rooted in a node is called a descendant of that node.

Other related concepts of the tree:
The level of the node is defined from the root, and the root is the first layer, and the child of the root is the second layer.
Parents on the same level of the knot are cousins.
The maximum level of nodes in a tree is called the depth (Depth) or height of the tree.

If the sub-trees of the nodes in the tree are considered to be sequential from left to right and cannot be interchanged, the number is called an ordered tree, otherwise it is called a unordered tree.
The forest (Forest) is a collection of M (m>=0) disjoint trees.

Compare the structure of a linear table with a tree:
Linear structure:
First Data element: no precursor
Last data element: no successor
Intermediate element: a precursor a successor
Tree structure:
Root node: No parents, only
Leaf node: No child, can be multiple
Middle node: One parent and one child (or one child)

Abstract data types for trees:
ADT Trees (tree)
Data
A tree is composed of a root node and several subtrees trees. Nodes in the tree have the same data type and hierarchical relationships.
Operation
Inittree (*t): Constructs an empty tree T.
Destroytree (*t): Destroys Tree T.
Createtree (*t,definition): Constructs the tree by the definition of the tree given in difinition.
Cleartree (*t): If the tree t exists, the tree T is cleared to an empty tree.
Treeempty (t): If T is an empty tree, returns True, otherwise false.
Treedepth (t): Returns the depth of T.
Root (t): Returns the root node of T.
Value (t,cur_e): Cur_e is a node in the tree T that returns the value of this node.
Assign (T,cur_e,value): Assigns the node cur_e of the tree T to value.
Parent (T,cur_e): If Cur_e is a non-root node of the tree, it returns its parents, otherwise it returns NULL.
Leftchild (t,cur_e): If Cur_e is a non-leaf node of the tree, it returns the leftmost child, otherwise it returns NULL.
Rightsibling (t,cur_e): If Cur_e has a right sibling, return to its right brother, otherwise return empty.
Insertchild (*t,*p,i,c): where P points to a node of the tree, I for the point of the node P is the degree plus 1, non-empty tree C and T do not intersect, the operation result is inserted C is the P-point in the tree T-node i subtrees tree.
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 subtrees tree of the node in P referred to in T.
Endadt

The storage structure of the tree:
Parental representations: We assume that the nodes of a tree are stored in a contiguous set of spaces, and that an indicator is attached to each node to indicate the position of its parent node in the array. ---that every node knows who he is, and where its parents are, in addition to knowing who he is.
Its node structure is as follows:
|----------|-----------|
|data |parent |
|----------|-----------|

Where data is a field, the data information of the node is stored, and the parent is the pointer field that stores the subscript of the parent of the node in the array.

The following is the node structure definition code for the parent representation:
#define MAX_TREE_SIZE 100
typedef int TELEMTYPE;
typedef struct PTNODE
{
Telemtype data;
int parent;
}ptnode;
typedef struct
{
Ptnode Nodes[max_tree_size];
The position and node number of the int r,n;//root
}ptree;

The following is a representation of a tree:
|-----------|-----------|-----------|
| Subscript | Data | Parent |
|-----------|-----------|-----------|
| 0 | A | -1 |
|-----------|-----------|-----------|
| 1 | B | 0 |
|-----------|-----------|-----------|
| 2 | C | 0 |
|-----------|-----------|-----------|
| 3 | D | 1 |
|-----------|-----------|-----------|
| 4 | E | 2 |
|-----------|-----------|-----------|
| 5 | F | 2 |
|-----------|-----------|-----------|
| 6 | G | 3 |
|-----------|-----------|-----------|
| 7 | H | 3 |
|-----------|-----------|-----------|
| 8 | I | 3 |
|-----------|-----------|-----------|
| 9 | J | 4 |
|-----------|-----------|-----------|

In addition, you can have the following types of representations (just a subset):
|-----------|-----------|-----------|-----------|
| Subscript | Data | Parent | firstchild|
|-----------|-----------|-----------|-----------|

|-----------|-----------|-----------|-----------|
| Subscript | Data | Parent | Rightsib |
|-----------|-----------|-----------|-----------|

Storage structure of the designer a very flexible process, the design of a storage structure is reasonable, depending on the storage structure based on whether the operation is appropriate, convenient, time complexity is not good.

Child notation:
Multilink notation: Each node is represented by multiple pointer fields, where each pointer points to the root node of a subtrees tree, and we call this method a multi-link table notation.
There are two ways to solve a problem with a different number of children.
Programme one:
|----------|-----------|-----------|-----------|-----------|-----------|
|data | Child1 | Child2 | child3 | ....... | CHILDN |
|----------|-----------|-----------|-----------|-----------|-----------|

Where data is the field, Child1 to Childn is the pointer field, which points to the child node of the node.
Advantages and disadvantages of the analysis: This method for each node in the tree, the degree of difference is obviously a waste of space, because there are a lot of nodes, its pointer field is empty, but if the degree of different nodes is not large, it means that the space is fully utilized, then the shortcomings of the storage structure has become the advantage.

Given the potential for space waste in the first scenario mentioned above, we have proposed programme II:
Scenario Two:
In the second scenario, the number of pointer fields per node equals the degree of the node, and we specifically take a location to store the number of node pointer fields, which are structured as follows
|----------|-----------|-----------|-----------|-----------|-----------|-----------|
|data | degree | Child1 | Child2 | child3 | ....... | CHILDN |
|----------|-----------|-----------|-----------|-----------|-----------|-----------|
Where data is the domain, degree is the degree field, that is, the number of child nodes that store the node, child1 to CHILDN as the pointer field, and points to the nodes of each child.
Advantages and disadvantages of the analysis: this solution to the shortcomings of waste space, the utilization of space is very high, but due to the various nodes linked list is not the same structure, plus to maintain the degree of the value of the node, in the calculation will lead to the loss of time.

In view of the fact that the above two scenarios of the multi-link table notation are not suitable, we introduce the concept of child notation:
The concrete way is, each node of the child node to arrange, to a single-linked table as the storage structure, n nodes have n children linked list, if the leaf node is the single linked list is empty. The N-head pointer then forms a linear table, which is stored in a one-dimensional array using sequential storage structures, as shown below

|------|------|-----------| |-------|------|
| Subscript | Data |     firstchild| |child |next |
|--------|--------|-------------|    |-------|------| |----|----|
| 0 |           A | |      ==> | |==> | ^  |
|--------|--------|-------------|    |-------|------| |----|----|
| 1 |           B | | ==> |3 |^ |
|--------|--------|-------------|    |-------|------| |----|----|
| 2 |           C | |      ==> |4 |  |==> |5 | ^ |
|------|------|-----------|    |-------|------|    |----|----| |----|----|
| 3 |           D | |      ==> |6 |    |==> |7 | |==> |8 |^ |
|------|------|-----------|    |-------|------|    |----|----| |----|----|
| 4 |           E | | ==> |^ |
|------|------|-----------| |-------|------|
| 5 | F | ^         |
|------|------|-----------|
| 6 | G | ^         |
|------|------|-----------|
| 7 | H | ^         |
|------|------|-----------|
| 8 | I | ^         |
|------|------|-----------|
| 9 | J | ^         |
|------|------|-----------|

To do this, we need to design two kinds of node structure, one is children's linked list of children node
|-------|------|
|child |next |
|-------|------|
Where child is the data field that stores the subscript of a node in an array of table headers, next is a pointer field that stores a pointer to the next kid node of a node.
The other is the header node of the table header array
|------|-----------|
| Data | firstchild|
|------|-----------|
Where data is a data field that stores the information of a node, FirstChild is the head pointer field that stores the node's child linked list's head pointer.

The following is the structure definition code for child notation:
#define MAX_TREE_SIZE 100
typedef struct CTNODE
{
int child;
struct Ctnode *next;
}*childptr;
typedef struct
{
Telemtype data;
Childptr FirstChild;
}ctbox;
typedef struct
{
Ctbox Nodes[max_tree_size];
The position and node number of the int r,n;//root
}ctree;

The above structure for some of the children we want to find a node, or to find a node of the brothers, only need to find the children of this node single-linked list, for traversing the whole tree is also very convenient, the head node array loop.

If you need to find out who the parent node of a node is, consider this structure
|------|------|------|-----------| |-------|------|
| Subscript | Data |parent|     firstchild| |child |next |
|------|------|------|-----------|    |-------|------| |----|----|
| 0 |           A |-1 | |      ==> | |==> | ^  |
|------|------|------|-----------|    |-------|------| |----|----|
| 1 |           B | | | ==> |3 |^ |
|------|------|------|-----------|    |-------|------| |----|----|
| 2 |           C | | |      ==> |4 |  |==> |5 | ^ |
|------|------|------|-----------|    |-------|------|    |----|----| |----|----|
| 3 |           D | | |      ==> |6 |    |==> |7 | |==> |8 |^ |
|------|------|------|-----------|    |-------|------|    |----|----| |----|----|
| 4 |           E | | ==> |^ |
|------|------|------|-----------| |-------|------|
| 5 | F | ^         |
|------|------|------|-----------|
| 6 | G |3 | ^         |
|------|------|------|-----------|
| 7 | H |3 | ^         |
|------|------|------|-----------|
| 8 | I |3 | ^         |
|------|------|------|-----------|
| 9 | J |4 | ^         |
|------|------|------|-----------|

These representations are called parental child representations


Child Brother representation:
Any tree, the first child of its node 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.
|-----------|-----------|-----------|
| Data | firstchild| Rightsib |
|-----------|-----------|-----------|
Where data is the domain, FirstChild is the pointer field, storing the node's first child node storage address, Rightsib is the pointer field, storing the node's right sibling node storage address.
The structure definition code is as follows.
typedef struct CSNODE
{
Telemtype data;
struct Csnode *firstchild,*rightsib;
}csnode,*cstree;

Analysis: This notation, to find a certain node of a child to bring convenience, only need to find this node by firstchild the eldest son, and then through the eldest son node Rightsib find its second brother, and then go down, know to find the specific child. Of course, if you want to find the parents of a certain node, this notation is flawed. Of course, you can also add a parent pointer field to solve the problem of quickly finding parents, so that the diagram is actually a binary tree. The contents of the binary tree, put to the next review section.
A tree is a finite set of n (n>=0) nodes. N=0 is called an empty tree, in any non-empty tree: (1) There is only one specific node called root (Root), (2) when n>1, the remaining nodes can be divided into m (m)

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.