A tree is a non-linear data structure. It has many concepts, such as root nodes and Subtrees.
Binary Tree: each node can have a maximum of two Subtrees, with left and right Subtrees.
Concept: Tree depth, full Binary Tree, full Binary Tree, and tree node tree
Binary Trees include sequential storage and chain storage. Here we only talk about chain storage. Each node of a binary tree is similar to a double-link table, but the tree structure is more complex than that of a double-Link Table. recursive calling is involved in the tree construction process, the recursion problem is often very complicated. Therefore, the construction of a binary tree is described here.
In international practice, first go to the Code:
1 # include <stdio. h> 2 # include <stdlib. h> 3 # include <malloc. h> 4 5 // define the binary tree 6 typedef struct node {7 int data; // Data Element 8 struct node * left; // point to the left subtree 9 struct node * right; // point to the right subtree 10} btree; 11 12 // construct a binary tree 13 int btreecreate (btree ** TP) 14 {15 // The constructor, or the constructor order: construct 16 int X; 17 scanf ("% d", & X); 18 if (x <0) 19 {20 * TP = NULL; // The pointer is null. A pointer in the tree node is null 21 return 0; 22} 23 * TP = (btree *) malloc (sizeof (btree )); // point the pointer in the tree node to the address space 24 if (TP = NULL) 25 return 0; 26 (* TP)-> DATA = X; 27 btreecreate (& (* TP)-> left); 28 btreecreate (& (* TP)-> right); 29 return 1; 30} 31 32 int main () 33 {34 btree * tree; 35 printf ("create Binary Tree: \ n"); 36 btreecreate (& tree); 37 38 return 0; 39}
The Code seems simple. The core is the btreecreate function, which completes the construction of a binary tree. According to the function definition, a node is created only when a positive integer is input.
The process of building a binary tree is as follows:
Create the left subtree of a subtree. If the left subtree does not exist (when a negative number is entered), create the right subtree. If the left subtree exists, create the left subtree of the subtree, in turn. When the left subtree does not exist, the right subtree is determined. If the right subtree does not exist, the parent node is returned and the right subtree is determined.
Note that once a node is created successfully, both the left and right subtree of the node must be determined, whether they exist or not, because btreecreate calls and enters a positive integer, that is, after a node is created successfully, both left and right are created.
Example: For the above program, set the breakpoint in line 38th and input 1, 2,-1,-1,-2,-3, the storage structure of the tree pointer is as follows (that is, the storage structure of the tree)
Based on the input data, the following tree structure can be drawn:
Comparing the above memory storage, we can see that the blue circle is the real tree node. The tree construction process is as follows:
Enter 1 to create the master node, then create the left subtree of 1, create the function wait of the right subtree, and set the function entry address to AD1
Input 2. The left subtree of master node 1 is created successfully. Next, create the left subtree of master node 2. Create the function wait of the right subtree of master node 2. Set the function entry address to ad2.
If the left subtree of input-1 and node 2 fails to be created, the system returns the function (closest to the current one) that is waiting for execution, that is, ad2. The right subtree of Node 2 is created.
Enter 3. The right subtree of Node 2 is successfully created. Next, create the left subtree of Node 3, and create the function wait of Node 3. The function entry address is ad2.
......
Until the function is called recursively, there is no waiting function.
Create a Complete Binary Tree verification result. The structure of the tree is as follows:
Input: 1, 2, 4,-1,-,-3,-,-5,-6, the Tree Memory is stored as follows: