Step-by-step write algorithm (sort binary tree insert)

Source: Internet
Author: User

Text: Step by step write algorithm (sort binary tree insert)

"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "


The node insertion of a binary tree is relatively simple. In general, the insertion of a binary tree is divided into the following two steps:

1) to judge the current parameters, because the head node needs to be considered, so we use pointer pointers as input parameters of the function

2) Sub-situation discussion:

If the original two-fork tree has no root node, then the newly inserted data is the root node;

If the original two fork tree has a root node, then we determine if the data exists, if it exists, then returns, and if it does not, continue inserting the data.

So how do you keep the data inserted? There are also three different situations:

1) If the inserted data is less than the current node's data, continue looking for the insertion position toward the left subtree of the current node

2) If the inserted data is larger than the currently inserted position, continue looking for the insertion position toward the right subtree of the current node

3) If the current node in the direction is empty, then the location of the insertion is found and the data can be inserted

The algorithm says so much, and here we begin to practice our code:

A) judging the legitimacy of the input data

STATUS Insert_node_into_tree (tree_node** pptreenode, int data) {if (NULL = = Pptreenode) return False;return TRUE;}
At this point, you can verify with a test case

static void Test1 () {assert (FALSE = = Insert_node_into_tree (NULL, 10));}

b) Determine if the current root node exists, modify the code

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); return TRUE;} return TRUE;}
The code is modified, and the test case is added.

static void Test2 () {tree_node* Ptreenode = null;assert (TRUE = = Insert_node_into_tree (&ptreenode, ten)); assert (10 = = Ptreenode->data); free (ptreenode);}

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

STATUS _insert_node_into_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); Elsereturn _insert_node_into_tree (& (*pptreenode)->right_child, data, *pptreenode);} 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); return TRUE;} Return _insert_node_into_tree (Pptreenode, data, NULL);}
The above code has taken into account the situation that is not the root node. We can add a test case accordingly.

static void Test3 () {tree_node* Ptreenode = Null;assert (true = = Insert_node_into_tree (&ptreenode, 9)); assert (true = = Insert_node_into_tree (&ptreenode, 8)); assert (TRUE = = Insert_node_into_tree (&ptreenode, ten)); assert (9 = = Ptreenode->data); assert (8 = = Ptreenode->left_child->data); assert (= = Ptreenode->right_child->data Free (ptreenode->left_child), free (ptreenode->right_child), free (ptreenode);}
Since the code above is recursive code, in order to achieve the robustness and completion of the code, in fact, we design test cases should include at least 9 test cases:

(1) Illegal parameters

(2) root node does not exist

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

(4) root node exists, insert data is 9, 8

(5) Root node exists, insert data is 9, 10

(6) Root node exists, insert data is 9, 8, 7

(7) root node exists, insert data is 9,7,8

(8) root node exists, insert data is 7, 8, 9

(9) Root node exists, insert data is 7,9,8


"Trailer: The following blog mainly introduces the node deletion of binary tree"


Step-by-step write algorithm (sort binary tree insert)

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.