[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]
A binary sorting tree is a data structure that we often use during development. It has good insertion, deletion, and search features. However, due to the large number of pointers to Binary Trees, binary trees are more troublesome than other data structures. However, there is no way to do this. The following describes some of my common methods.
We know that if a binary tree is full of trees, the nodes of the binary tree are arranged in sequence according to 1, 2, 3, and 4. However, the reality is that due to the characteristics of the binary sorting tree, a branch node may have branches on the left half side, but no branches on the right half side, or a branch on the right half side without branches on the left half side. The node sequence in the data may be inconsistent.
However, for a node, there is still some connection between its left branch node, right branch node, and parent node. For example, if the order of the parent node is n, its left node can only be n * 2, and the right node can only be 2 * n + 1. So, can we use the relationship between the parent node and the child node to store data? Of course, the answer is yes.
First, we need to redefine the data structure, where number records the serial number:
Typedef struct _ TREE_NODE
{
Int data;
Int number;
Struct _ TREE_NODE * left_child;
Struct _ TREE_NODE * right_child;
} TREE_NODE;
Typedef struct _ TREE_NODE
{
Int data;
Int number;
Struct _ TREE_NODE * left_child;
Struct _ TREE_NODE * right_child;
} TREE_NODE; will the function that added the data be modified?
STATUS _ insert_node_pai_tree (TREE_NODE * pTreeNode, int data)
{
TREE_NODE * pNode;
While (1 ){
If (data <pTreeNode-> data ){
If (NULL = pTreeNode-> left_child ){
PNode = create_tree_node (data );
Assert (NULL! = PNode );
PNode-> number = pTreeNode-> number <1;
PTreeNode-> left_child = pNode;
Break;
} Else
PTreeNode = pTreeNode-> left_child;
} Else {
If (NULL = pTreeNode-> right_child ){
PNode = create_tree_node (data );
Assert (NULL! = PNode );
PNode-> number = pTreeNode-> number <1 + 1;
PTreeNode-> right_child = pNode;
Break;
} Else
PTreeNode = pTreeNode-> right_child;
}
}
Return TRUE;
}
STATUS insert_node_pai_tree (TREE_NODE ** ppTreeNode, int data)
{
If (NULL = ppTreeNode)
Return FALSE;
If (NULL = * ppTreeNode ){
* PpTreeNode = (TREE_NODE *) create_tree_node (data );
Assert (NULL! = * PpTreeNode );
(* PpTreeNode)-> number = 1;
Return TRUE;
}
Return _ insert_node_pai_tree (* ppTreeNode, data );
}
STATUS _ insert_node_pai_tree (TREE_NODE * pTreeNode, int data)
{
TREE_NODE * pNode;
While (1 ){
If (data <pTreeNode-> data ){
If (NULL = pTreeNode-> left_child ){
PNode = create_tree_node (data );
Assert (NULL! = PNode );
PNode-> number = pTreeNode-> number <1;
PTreeNode-> left_child = pNode;
Break;
} Else
PTreeNode = pTreeNode-> left_child;
} Else {
If (NULL = pTreeNode-> right_child ){
PNode = create_tree_node (data );
Assert (NULL! = PNode );
PNode-> number = pTreeNode-> number <1 + 1;
PTreeNode-> right_child = pNode;
Break;
} Else
PTreeNode = pTreeNode-> right_child;
}
}
Return TRUE;
}
STATUS insert_node_pai_tree (TREE_NODE ** ppTreeNode, int data)
{
If (NULL = ppTreeNode)
Return FALSE;
If (NULL = * ppTreeNode ){
* PpTreeNode = (TREE_NODE *) create_tree_node (data );
Assert (NULL! = * PpTreeNode );
(* PpTreeNode)-> number = 1;
Return TRUE;
}
Return _ insert_node_pai_tree (* ppTreeNode, data );
} So what data should be stored in the hard disk? When traversing each node, we only need to put the corresponding data and serial number on the hard disk in sequence.
Typedef struct _ DATA
{
Int data;
Int number;
} DATA;
Typedef struct _ DATA
{
Int data;
Int number;
} DATA;
Do I have to enable the stored data again? How to load it? It is very simple. There are four steps:
1) Allocate n * sizeof (TREE_NODE) space based on the total number of recorded nodes;
2) extract DATA from the hard disk in sequence and copy the DATA to the TREE_NODE. For now, the left_side and right_side pointers are empty;
3) For each node n, find its parent node n> 1, fill in left_side or right_side, and according to (n % 2) 1 indicates whether the current node is a left node or a right node;
4) obtain the node with n = 1, and the node is the root node we need to find. The data is loaded now.