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;
So the function that added the data also needs to be modified?
STATUS _insert_node_into_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;}elsepTreeNode = 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;}elsepTreeNode = pTreeNode->right_child;}}return TRUE;}STATUS insert_node_into_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_into_tree(*ppTreeNode, data);}
So what data should be stored in the hard disk at this time? 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;
Do I have to enable the stored data again? How to load it? It is very simple. 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 them to tree_node, for the moment, the left_side and right_side pointers are null. 3) for each node N, find its parent node n> 1, fill in the left_side or right_side, and according to (n % 2) check whether the value is 1 to determine whether the current node is a left node or a right node. 4) Obtain a node with n = 1. Then, this node is the root node we need to find. Data is loaded now.