Tree (General tree)
definitions and basic operations
terminology of the tree
structure characteristics of the tree
basic operation of the tree
the storage structure of the tree
the traversal of a tree
definitions and basic operations
A tree is a finite set of n (n>=0) nodes. When N=0, the collection is empty, called an empty tree. In any non-empty tree, there is only one specific node called the root. When n>1, the remaining nodes outside the root node can be divided into m (m>=0) disjoint finite node set T1, T2....tm. Each of these sets is itself a tree, called the root subtree.
which
(1) There is only one node called the root: it has no anterior nodes, there are 0 or more successive nodes.
(2) There are several nodes called leaves: they have only one first few nodes, and no subsequent nodes.
(3) The rest of the nodes are called nodes: they have only one anterior node and at least one successor.
In fact, a tree represents a data structure of a set of nodes that differs from the predecessor and successor of a linear table. In general, there is only one anterior continuation of any one node (except the root node), which can have multiple successors (except the leaf node). terminology of the tree
(1) The degree of the node: the number of a node having a subtree (or subsequent nodes) is called degrees. degrees are representations of node branch trees.
(2) The degree of the tree: the maximum value of the degree of all nodes in the tree is called the degree of the tree.
(3) Sub-nodes: The root node (or direct successor) of a subtree of a node is called a sub-node of the node.
(4) Parent node: The forward node of a subtree node is called the parent node. Any node other than the root node has and has only one parent node. The parent node is also called the parental knot.
(5) Sibling knot: A number of sub-nodes belonging to the same parent node are called sibling nodes.
(6) The layer of a node: any node of the tree is in a certain layer. Therefore, the nodes in the tree form a hierarchical structure.
(7) Depth of tree: the maximum level of a tree is called the depth of the tree, also known as the height of the tree.
(8) Ordered tree and unordered number: If the node nodes are considered to be ordered from left to right, it is called ordered tree; otherwise, it is called an unordered tree. The fraternal nodes of the ordered tree cannot be exchanged.
(9) Forest: A collection of trees is called a forest. There is a close relationship between trees and forests. Deletes the root node of a tree, and all its original subtrees are trees, constituting the forest. The root node of all the trees connected to the forest with one node is the tree. This node is called the new root node. All trees in the forest are subtrees of that node. structure characteristics of the tree
(1) Hierarchy: Each node of the tree is at a certain level. There are several nodes at each level beyond the root node. Therefore, the tree structure is also called the hierarchy. Hierarchies are often used to denote the affiliation of things. such as the organization of the Department of Human Relations, Chen Pin parts Assembly relationship, The data schema relation of structure type definition in programming language, the model design of hierarchical model database and the structure of file directory in operating system are the application of tree structure.
(2) Branching: down from the root node, each node has several branches (the number of branches of the leaf node is treated as 0). Along these branches you can go from the root node to any other node in the tree, and the nodes that pass through will form a branching path. Because each node of the tree has and only one path, So branching features provide a way to uniquely locate nodes in a tree. the basic operation of the tree create a tree to find the tree of Trees , to seek the tree nodes, to find the parent node, to find the node of the node. The storage structure of the parent linked table representation
Parent Linked list notation (also known as parental notation) is the storage of all nodes of a tree with a contiguous set of memory spaces, and the relationship with the parent node. Each node space stores the node data itself and the parent node number. The parent node number is the node number used to represent the parent node of the node in the table, and it is also a pointer (but not a pointer type). The location number of the parent node is represented by a special value of "1", because the root node of the tree is the only node with no parent nodes.
A parent linked list is essentially a sequential structure and is also a static structure, so it can be stored in a one-dimensional array. The array contains two fields, a node information field, and a pointer field. The pointer field holds an integer. The array element number is the node position number.
Structure data type TREEFATHERCS.C
#include <stdio.h>
//Parent list notation
#define M 100//assume array contains 100 and element
typedef struct {//define array element structure
char data ;//Assuming that the node information of the tree is the character
int father;//pointer type integer, the parent node pointer
}flnode;
typedef struct {//define parent list
Flnode element[m];//a M (=100) element array
int n;//The number of actual elements
};
The parent list representation is very simple for the "root" and "parent node" operations. finding the root node.
Because the value of the pointer field to the node is "-1" and only one, so long as the node that finds the parent node position number equals "1" is the root node.
TreeFatherControl.h statement
Create a tree using the parent list notation
fttree createfathertree ();
Print tree
void Printfathertree (Fttree tree);
The root node of the tree is
void getfathertreeroot (Fttree tree);
Treefathercontrol. Implementing
Create a tree using the parent list notation Fttree createfathertree () {Fttree tree;
tree.n=10;
Flnode node;
A node.data= ' a ';
Node.father=-1;
Tree.element[0]=node;
b node.data= ' B ';
node.father=0;
Tree.element[1]=node;
C node.data= ' C ';
node.father=0;
Tree.element[2]=node;
D node.data= ' d ';
node.father=0;
Tree.element[3]=node;
E node.data= ' e ';
Node.father=1;
Tree.element[4]=node;
F node.data= ' F ';
Node.father=1;
Tree.element[5]=node;
H node.data= ' h ';
node.father=3;
Tree.element[6]=node;
I node.data= ' i ';
node.father=3;
Tree.element[7]=node;
G Node.data= ' G ';
node.father=6;
Tree.element[8]=node;
K node.data= ' K ';
node.father=7;
Tree.element[9]=node;
return tree;
}//print tree void Printfathertree (Fttree tree) {printf ("\ n");
printf ("tree={");
printf ("{");
int flag=0; for (int I=0;I<TREE.N && i<m;i++{if (flag==0) {printf ("%c", tree.element[i].data);
flag=1;
}else{printf (",%c", tree.element[i].data);
}} printf ("}");
printf ("%d", TREE.N);
printf ("}\n");
printf ("\ n");
} void Printflnode (int position,flnode node) {printf ("(Position number:%d, data field:%c, parent node Number:%d)", Position,node.data,node.father);}
Find the root node of the tree void Getfathertreeroot (Fttree tree) {printf ("\ n");
1. Iterate through the array and find the node where the pointer field is-1, which is the root node for (int i=0;i<tree.n;i++) {Flnode node=tree.element[i];
if (node.father==-1) {printf ("root node of tree--->");
Printflnode (I,node);
Break
}} printf ("\ n");
}
Called in the Main.c method
#include "TreeFatherControl.h"
int main (int argc, const char * argv[]) {
fttree tree=createfathertree ();
Printfathertree (tree);
Finding the root node of the tree
Getfathertreeroot (trees);
return 0;
}
Printing results:
TREE={{A,B,C,D,E,F,H,I,G,K}10}
Tree root node---> (location No.: 0, data field: A, parent node Number:-1)
find the parent node.
The pointer field is the position number of the parent node as long as it finds the array element that contains the known node.
TreeFatherControl.h
The parent node of the specified location number node node,
void Getfathertreefathernode (fttree tree,int position);
Treefathercontrol.c
The parent node of the specified location number node node,
void Getfathertreefathernode (fttree tree,int position) {
printf ("\ n");
1. Determine if this position number is in the array
if (POSITION>=TREE.N) {
printf ("There is no node with position number%d in the tree", position);
} else{
//2. Locate the array element that specifies the position number
Flnode node= tree.element[position];
3. Gets the position number of its parent node (the pointer field of this array element is its parent node position number)
Flnode Father=tree.element[node.father];
Printflnode (Position,node);
printf ("Parent node----");
Printflnode (Node.father,father);
}
printf ("\ n");
}
Called in the Main.c method
#include "TreeFatherControl.h"
int main (int argc, const char * argv[]) {
//Request parent Node
Getfathertreefathernode (tree, 2);
return 0;
}
Printing results:
(Location number: 2, data field: C, parent node Number: 0) parent Node--(location number: 0, data field: A, parent node Number:-1)
To find a child node
To find a child node then it's more troublesome, you must traverse the entire array table and find its pointer field is the position number of its specified node in order to locate its child nodes. Because the node has a sub-node of more than or equal 0, it is variable, so you must traverse the entire array to find all the child nodes.
TreeFatherControl.h
Specifies the sub-node of the location number node
void Getfathertreenodechilds (fttree tree,int position);
Treefathercontrol.c
Specifies the sub-node of the location number node
void Getfathertreenodechilds (fttree tree,int position) {
printf ("\ n");
1. Determine if an element of this position exists in the array or is not empty
if (tree.n==0 | | POSITION>=TREE.N) {
printf ("There is no node with location number%d in the tree", position);
} else{
//2. Specify the location of the node--in order to print elements of this position
Flnode node= tree.element[position];
Printflnode (Position,node);
printf ("Sub-node is: \ n");
3. Iterate through the array and find the node with the pointer field position, because the sub-nodes of a node are greater than or equal to 0, so you must traverse the entire array for
(int i=0;i<tree.n;i++) {
Flnode Child=tree.element[i];
The pointer field is the position number of the parent node
if (child.father==position) {
Printflnode (I, child);
printf ("\ n")
;
}}} printf ("\ n");
}
Called in the Main.c method
#include "TreeFatherControl.h"
int main (int argc, const char * argv[]) {
//Sub-node
getfathertreenodechilds (tree, 3);
return 0;
}
Printing results:
(Location number: 3, data field: D, parent node Number: 0) The Sub-node is:
(position number: 6, data field: H, parent node Number: 3)
(location number: 7, data field: I, parent node number: 3)
Sub-linked list notation
Sub-list notation (also called child notation), the number of sub-nodes of a node is indeterminate.
If you use the storage structure of the sequential table, then you need to use the degree of the tree (the maximum number of child nodes) for each node set equal number of sub-node Number field, which will make a large amount of storage space idle and resulting in space efficiency is not high. With n nodes, the degree of the tree is M, The number of branches that point to the sub-nodes is n-1. That is, only the N-1 sub-node field stores the location number of the child nodes, and the remaining n (m-1) + 1 sub-nodes are idle and not exploited.
Using chained storage fabric storage can increase the utilization of space. Create a single linked list of nodes for each node of the tree. The head node consists of the node data field and the chain Head pointer field. The chain-head pointer holds a pointer to the first child node of the node. The link node is composed of the node data field and the chain Head pointer field. The Sub-node Number field holds the sub-node number , the Chain pointer field holds the link node pointer of the next child node. The Chain pointer field of the last link node is empty so that it is stored as a node chain of nodes.
The sub-linked list is actually a combination of sequential storage structures and chained storage structures.
Data structure Type:
Treechildcs.c
#define M
typedef struct CNODE{//defines the chain node
int CNO;//Sub-node
struct Cnode *next;//next node pointer
}clink;
typedef struct {//Define head node
Char data;//tree node information field
Clink *heaer;//chain head pointer
}ctnode;
typedef struct {//defines the number of
nodes of the node[m];//head node Group
int n,root;//tree, the root node location number}cltree, and the Ctnode list
;
List representation of children
The Children's list representation (also called child Brother notation) overcomes the disadvantage that the parent linked list stores only the relationship to the parent node and the sub-linked table value stores the relationship to the child nodes, At the same time, it solves the problem of the number of sub-nodes in the sub-linked list. The basic idea is that the parent-child relationship of a node is represented in a structure together with a sibling. Make the storage nodes the same form of construction, making it easy to use sequential storage structures.
The children of the list each storage node consists of 3 fields: Node data field, child node Number field, sibling node Number field.
Sub-node field: The position number of the first (or leftmost) child node that holds this node. A leaf node has no sub-nodes to store a special value, such as "1".
Brother node field: The position number of the next sibling node that holds the node (from left to right). If there is no next sibling node, then "1" is stored.
With this kind of storage structure, it is easy to realize the operation of sub-nodes. The method is to select the 1th node of the node based on the known node, and then go down its sibling node to find all its sub-nodes. In fact, the Brother node is a chain of all the sub-nodes that belong to the same node.
In TREEBROTHERCS.C
#define M
typedef struct {//define array element structure
Char data;//tree node is assumed to be a char type
int child,nextsib;//pointer is an integer type
} Csnode;
typedef struct {//definition sibling list
csnode element[m];
int n;//The actual number of elements
};
This structure is not easy to find the parent node operation, but it is simple to add a parent node pointer field.
the traversal of a tree First Root traversal
First access the root node, the order of traversal is: first access to the tree's heel node, and then from left to right to traverse the root node of all the subtree. Because the subtree is also a tree, follow the above steps when traversing each subtrees tree.
Algorithm Description:
If the tree is not empty
(1) Access to root node
(2) The root of each subtree is traversed from left to right in the first sequence. post-root traversal
Last access to the root node. The traversal order is the subtree that traverses the root from left to right. Then access the root node of the tree. Follow the steps above for each subtrees tree.
Algorithm Description:
If the non-empty tree
(1) The root traverses each subtree from left to right.
(2) Access to the root node. Hierarchical Traversal
Access nodes from top to bottom, starting from the root node. The traversal order is: if the tree is not empty, then the root node (the 1th layer, only one root node) is accessed, and the nodes of the 2nd, 3rd, and so on are traversed. The nodes are accessed from the left-to-right order when traversing a layer.
Algorithm description
If the tree is not empty, then:
(1) Access the root node first.
(2) If the first layer has been visited, then the i+1 layer has not visited the node, then from left to right to access the I+1 layer on the node.
This is the basic operation of the general tree, please point out, there are good suggestions please pointed out that there are good books, but also recommended to everyone!
Tree general tree Definition and basic operation Tree term tree structure feature tree basic operation tree storage structure Parent list representation method finding the root node finding the parent node finding the child node list representation The traverse of the Children's list representation tree traversal first root traversal after the root traverse hierarchy traversal