Tree Definition
Professional definition: there is only one node called the root node, and there are several child trees that do not conflict with each other. These child trees are also a tree.
Common Definition: A tree consists of nodes and edges. Each node has only one parent node but can have multiple subnodes. Except for one node, this node does not have a parent node, this node is called the root node.
Terminology
Parent node child brother depth
Depth: the layers from the root node to the underlying node are called depth. The root node is the first layer.
Leaf nodes: nodes without subnodes.
Non-terminal node: it is actually a non-leaf node.
Degree: the number of subnodes is called degree.
Tree Classification
General tree: the number of subnodes of any node is not limited.
Binary Tree: the maximum number of subnodes of any node is 2, and the position of the subnode cannot be changed.
A binary tree is an ordered tree.
Forest: a set of N trees that do not match each other.
Binary Tree Classification
Common Binary Tree
Full Binary Tree: without increasing the number of layers of trees, a binary tree that cannot be added to another node is a full binary tree.
Full Binary Tree: If you only delete several consecutive nodes at the bottom and rightmost of the full binary tree, the resulting Binary Tree is a full binary tree.
Full binary trees are a special case of full Binary Trees.
Tree Storage
Binary Tree Storage
Continuous storage [full Binary Tree]
Advantage: Find the parent and child nodes of a node
Disadvantage: the memory consumption is too large.
General tree storage
Parental notation
Convenience for finding the parent node ---- subscript, starting from 0
Child notation
Convenient for sub-nodes
Parent-Child notation
It is convenient to find the Parent and Child Nodes.
Binary Tree Representation
Store a common tree into a binary tree.
Specific conversion method:
Try to ensure that the Left pointer field of any node points to its first child,
The right pointer field points to its next brother.
If this condition is met, a common tree can be converted into a binary tree.
If a common tree is converted into a binary tree, there must be no right subtree.
Forest storage:
First, convert the forest into a binary tree, and then store the binary tree.
# Include <stdio. h> # include <malloc. h>/*** construct a binary tree statically * 2014/8/30 */struct btnode * createbtree (void); void pretraversebtree (struct btnode * PT); void intraversebtree (struct btnode * PT ); void posttraversebtree (struct btnode * PT); typedef struct btnode {int data; struct btnode * plchild; // P is the pointer, L is the left, child is the Child struct btnode * prchild;}; int main (void) {struct btnode * PT = createbtree (); // pretraversebtree (PT ); // Intraversebtree (PT); posttraversebtree (PT); Return 0;} // post-order traversal void posttraversebtree (struct btnode * PT) {If (null! = Pt) {If (null! = Pt-> plchild) {pretraversebtree (Pt-> plchild);} If (null! = Pt-> prchild) {pretraversebtree (Pt-> prchild);} printf ("% C \ n", Pt-> data ); // output and element} // traverse void intraversebtree (struct btnode * PT) {If (null! = Pt) {If (null! = Pt-> plchild) {pretraversebtree (Pt-> plchild);} printf ("% C \ n", Pt-> data); // output and element if (null! = Pt-> prchild) {pretraversebtree (Pt-> prchild) ;}}// first traverse void pretraversebtree (struct btnode * PT) {If (PT! = NULL) {printf ("% C \ n", Pt-> data); // output and element if (null! = Pt-> plchild) {pretraversebtree (Pt-> plchild);} If (null! = Pt-> prchild) {pretraversebtree (Pt-> prchild);} // Pt-> plchild can represent the entire left subtree}/** the pseudo algorithm first accesses the root node; then traverse the left subtree in sequence; then traverse the right subtree in sequence. * // create a binary tree struct btnode * createbtree (void) {struct btnode * pA = (struct btnode *) malloc (sizeof (struct btnode )); struct btnode * pb = (struct btnode *) malloc (sizeof (struct btnode); struct btnode * Pc = (struct btnode *) malloc (sizeof (struct btnode )); struct btnode * Pd = (struct btnode *) malloc (sizeof (struct btnode); struct btnode * Pe = (struct btnode *) malloc (sizeof (struct btnode )); pa-> DATA = 'a'; Pb-> DATA = 'B'; PC-> DATA = 'C'; Pd-> DATA = 'D '; pe-> DATA = 'E'; pa-> plchild = Pb; pa-> prchild = pc; Pb-> plchild = Pb-> prchild = NULL; PC-> plchild = Pd; PC-> prchild = NULL; Pd-> plchild = NULL; Pd-> prchild = PE; pe-> plchild = pe-> prchild = NULL; return Pa; // address of the root node}
Application of the tree
Tree is an important form of data organization in databases.
The relationship between the sub-parent process of the operating system is itself a tree.
Class inheritance relationships in object-oriented languages.
Harman tree.
Relationship between sorting and searching
Sorting is the prerequisite for searching.
Sorting is important.
/*** Quick Sort **/# include <stdio. h> int findpos (int * a, int low, int high); void quicksort (int * a, int low, int high); int main (void) {int A [6] = {2, 1, 0, 5, 4, 3}; int I; // The second parameter represents the subscript of the first element // The third parameter represents the subscript quicksort (A, 0, 5) of the last element; for (I = 0; I <6; ++ I) {printf ("% d", a [I]);} printf ("\ n"); Return 0 ;} void quicksort (int * a, int low, int high) {int Pos; If (low
(Haobin Lecture) Data Structure Learning (7) --- tree