Binary Tree Interface Definition
Binary Tree Interface Definition
This set of interfaces provides basic operations on Binary Trees and some simple attributes, such as binary tree initialization, destruction, and insertion, deletion, and merging of leaf nodes (note that they are leaf nodes, attributes include the number of nodes in the tree, the root node of the tree, the branch end ID of the tree, the leaf node ID, the data in the node, the left child node of the node, and the right child node.
Bitree_init
Void bitree_init (BiTree * tree, void (* destroy) (void * data ));
Return Value: None
Description: initializes the binary tree specified by the parameter tree. This function must be called before other operations are performed.
When bitree_destroy is called, the function specified by the destroy parameter provides a way to release dynamic data space allocation. For example, if a fruit tree contains data dynamically allocated using malloc, destroy should be set to free when the binary tree is destroyed. For structured data that contains multiple dynamically allocated members, destroy should be set as a user-defined destructor to recycle resources for each dynamically allocated member and struct. If the binary tree contains data that does not need to be released, set destroy to NULL.
Complexity: O (1)
Bitree_destroy
Void bitree_destroy (BiTree * tree );
Return Value: None
Description: destroys the binary tree specified by the parameter tree. After this function is called, no other operations can be executed unless you call bitree_init again.
The bitree_destroy operation removes all nodes in the binary tree. If the destroy parameter is not NULL, call the function specified by destroy to recycle resources for each removed node.
Complexity: O (n ). N indicates the number of nodes in the binary tree.
Bitree_ins_left
Int bitree_ins_left (BiTree * tree, BiTreeNode * node, const void * data );
Return Value: If the insert operation is successful, 0 is returned; otherwise,-1 is returned.
Description: inserts a node into the binary tree specified by the tree to make it the left subnode of the node.
If the node already has a left subnode, bitree_ins_left returns-1. If node is NULL, the new node is inserted as the root node. When inserting the root node, the tree must be left empty. Otherwise, bitree_ins_left returns-1. When the insertion is successful, the new node contains a pointer to data, so as long as the node is still in the binary tree, the memory referenced by data must be valid. The user is responsible for managing the memory space referenced by data.
Complexity: O (1)
Bitree_ins_right
Int bitree_ins_right (BiTree * tree, BiTreeNode * node, const void * data );
Return Value: If the insert operation is successful, 0 is returned; otherwise,-1 is returned.
Description: This operation is similar to bitree_ins_left. Except that the node to be inserted is the right subnode of the node specified in the binary tree specified by the tree.
Complexity: O (1)
Bitree_rem_left
Void bitree_rem_left (BiTree * tree, BiTreeNode * node );
Return Value: none.
Description: removes the child tree whose left node is the root node in the binary tree specified by the tree.
If node is NULL, all nodes in the tree are removed. If the destroy parameter passed to bitree_init is not NULL, the function specified by destroy is called when the node is removed.
Complexity: O (n). Here n represents the number of nodes in the subtree.
Bitree_rem_right
Void bitree_rem_left (BiTree * tree, BiTreeNode * node );
Return Value: none.
Description: removes the subtree whose right node is the root of the Binary tree specified by the tree.
If node is NULL, all nodes in the tree are removed. If the destroy parameter passed to bitree_init is not NULL, the function specified by destroy is called when the node is removed.
Complexity: O (n). Here n represents the number of nodes in the subtree.
Bitree_merge
Int bitree_merge (BiTree * merge, BiTree * left, BiTree * right, const void * data );
Return Value: If the merge operation is successful, 0 is returned; otherwise,-1 is returned.
Description: combines the two binary trees specified by left and right into a single binary tree.
After merging, the data represented by the parameter data is stored in the root node of merge, while left and right represent the left and right subtree of the root node. Once the merge is complete, left and right are like bitree_destroy operations on them.
Complexity: O (1 ).
Bitree_size
Int bitree_size (const BiTree * tree );
Return Value: number of nodes in the returned tree.
Description: This is a macro used to calculate the number of nodes in the binary tree specified by the parameter tree.
Complexity: O (1 ).
Bitree_root
BiTreeNode * bitree_root (const BiTree * tree );
Return Value: return the root node of the Binary tree specified by the parameter tree.
Description: This is a macro used to return the root node in the binary tree specified by the parameter tree.
Complexity: O (1 ).
Bitree_is_eob
Int bitree_is_eob (const BiTreeNode * node );
Return Value: If the node identifies the end of the tree branch, 1 is returned; otherwise, 0 is returned.
Description: This is a macro used to determine whether the node identified by the parameter node is the end of a branch in a binary tree.
Complexity: O (1 ).
Bitree_is_leaf
Int bitree_is_leaf (const BiTreeNode * node );
Return Value: If the node is a leaf node, 1 is returned; otherwise, 0 is returned.
Description: This is a macro used to determine whether the node identified by the parameter node is a leaf node in a binary tree.
Complexity: O (1 ).
Bitree_data
Void * bitree_data (const BiTreeNode * node );
Return Value: return the data stored in the node.
Description: This is a macro used to determine the data stored in the node identified by the parameter node.
Complexity: O (1 ).
Bitree_left
BiTreeNode * bitree_left (const BiTreeNode * node );
Return Value: returns the left subnode of the specified node.
Description: This is a macro used to return the left subnode of the node identified by the parameter node.
Complexity: O (1 ).
Bitree_right
BiTreeNode * bitree_right (const BiTreeNode * node );
Return Value: return the right child node of the specified node.
Description: This is a macro used to return the right child node of the node identified by the parameter node.
Complexity: O (1 ).
The implementation and analysis of these interfaces will be elaborated in the next article.