One-step write algorithm (sort Binary Tree insertion)

Source: Internet
Author: User

 

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

 

 

 

 

Binary Tree node insertion is relatively simple. In general, Binary Tree insertion is divided into the following two steps:

 

1) judge the current parameter. Because we need to consider the header node, we use the pointer as the input parameter of the function.

 

2) situation discussion:

 

If the original binary tree does not even have a root node, the newly inserted data is the root node;

 

If the original binary tree has a root node, we can determine whether the data exists. If the data exists, it is returned. If the data does not exist, we can continue to insert data.

 

How can we save the inserted data? There are three situations:

 

1) if the inserted data is smaller than the data of the current node, continue searching for the insert location in the left subtree of the current node.

 

2) If the inserted data is greater than the current Insertion Location, continue searching for the insertion location in the right subtree direction of the current node.

 

3) if the current node is empty, it indicates that the inserted position is found and data can be inserted.

 

After the algorithm is said so much, we will start to practice our code below:

 

A) determine the validity of input data

 

 

STATUS insert_node_pai_tree (TREE_NODE ** ppTreeNode, int data)

{

If (NULL = ppTreeNode)

Return FALSE;

 

Return TRUE;

}

STATUS insert_node_pai_tree (TREE_NODE ** ppTreeNode, int data)

{

If (NULL = ppTreeNode)

Return FALSE;

 

Return TRUE;

} At this time, you can use a test case to verify

 

 

Static void test1 ()

{

Assert (FALSE = insert_node_pai_tree (NULL, 10 ));

}

Static void test1 ()

{

Assert (FALSE = insert_node_pai_tree (NULL, 10 ));

}

 

B) determine whether the current root node exists and modify the code

 

 

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 );

Return TRUE;

}

 

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 );

Return TRUE;

}

 

Return TRUE;

} Modified the code and added test cases.

 

 

Static void test2 ()

{

TREE_NODE * pTreeNode = NULL;

Assert (TRUE = insert_node_pai_tree (& pTreeNode, 10 ));

Assert (10 = pTreeNode-> data );

Free (pTreeNode );

}

Static void test2 ()

{

TREE_NODE * pTreeNode = NULL;

Assert (TRUE = insert_node_pai_tree (& pTreeNode, 10 ));

Assert (10 = pTreeNode-> data );

Free (pTreeNode );

}

 

C) if there is no root node, what if the root node exists?

 

 

STATUS _ insert_node_pai_tree (TREE_NODE ** ppTreeNode, int data, TREE_NODE * pParent)

{

If (NULL = * ppTreeNode ){

* PpTreeNode = create_tree_node (data );

Assert (NULL! = * PpTreeNode );

(* PpTreeNode)-> parent = pParent;

Return TRUE;

}

 

If (data <(* ppTreeNode)-> data)

Return _ insert_node_into_tree (& (* ppTreeNode)-> left_child, data, * ppTreeNode );

Else

Return _ insert_node_pai_tree (& (* ppTreeNode)-> right_child, data, * ppTreeNode );

}

 

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 );

Return TRUE;

}

 

Return _ insert_node_pai_tree (ppTreeNode, data, NULL );

}

STATUS _ insert_node_pai_tree (TREE_NODE ** ppTreeNode, int data, TREE_NODE * pParent)

{

If (NULL = * ppTreeNode ){

* PpTreeNode = create_tree_node (data );

Assert (NULL! = * PpTreeNode );

(* PpTreeNode)-> parent = pParent;

Return TRUE;

}

 

If (data <(* ppTreeNode)-> data)

Return _ insert_node_into_tree (& (* ppTreeNode)-> left_child, data, * ppTreeNode );

Else

Return _ insert_node_pai_tree (& (* ppTreeNode)-> right_child, data, * ppTreeNode );

}

 

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 );

Return TRUE;

}

 

Return _ insert_node_pai_tree (ppTreeNode, data, NULL );

}

The above code has considered not the root node. We can add a test case accordingly.

 

 

Static void test3 ()

{

TREE_NODE * pTreeNode = NULL;

Assert (TRUE = insert_node_pai_tree (& pTreeNode, 9 ));

Assert (TRUE = insert_node_pai_tree (& pTreeNode, 8 ));

Assert (TRUE = insert_node_pai_tree (& pTreeNode, 10 ));

Assert (9 = pTreeNode-> data );

Assert (8 = pTreeNode-> left_child-> data );

Assert (10 = pTreeNode-> right_child-> data );

Free (pTreeNode-> left_child );

Free (pTreeNode-> right_child );

Free (pTreeNode );

}

Static void test3 ()

{

TREE_NODE * pTreeNode = NULL;

Assert (TRUE = insert_node_pai_tree (& pTreeNode, 9 ));

Assert (TRUE = insert_node_pai_tree (& pTreeNode, 8 ));

Assert (TRUE = insert_node_pai_tree (& pTreeNode, 10 ));

Assert (9 = pTreeNode-> data );

Assert (8 = pTreeNode-> left_child-> data );

Assert (10 = pTreeNode-> right_child-> data );

Free (pTreeNode-> left_child );

Free (pTreeNode-> right_child );

Free (pTreeNode );

} Because the above Code is recursive Code, in order to achieve code robustness and completion, we should include at least nine test cases when designing test cases:

 

(1) The parameter is invalid.

 

(2) The root node does not exist.

 

(3) The root node exists, but the inserted data already exists.

 

(4) The root node exists and the inserted data is 9, 8

 

(5) The root node exists and the inserted data is 9, 10

 

(6) The root node exists and the inserted data is 9, 8, and 7.

 

(7) The root node exists and the inserted data is 9, 7, 8

 

 

(8) The root node exists and the inserted data is 7, 8, 9.

 

(9) The root node exists and the inserted data is 7, 9, 8.

 

 

 

 

[Notice: The following blog mainly introduces the deletion of Binary Tree nodes]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.