(Haobin Lecture) Data Structure Learning (7) --- tree
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
# Include
/*** 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); PostTraverseBTr Ee (pT); return 0;} // traverse 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
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