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

Source: Internet
Author: User
Tags prev

Original: Step-by-step writing algorithm (sort binary tree cues)

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


Before we talked about the sort of binary tree, not yet familiar with the classmate can look at this, binary tree basic operation, binary tree insert, binary tree Delete 1, delete 2, delete 3. But sorting a binary tree is not without its drawbacks, for example, what if we want to delete a piece of data in the sort binary tree? According to the present structure, we can only one data lookup verification, first of all to see in the binary tree is not sorted, if the deletion, if there is no data, then continue to find. So is there a way to save the next node of the current node? This eliminates the need for unnecessary lookups. In fact, this method exists, that is, in the sort binary tree to add forward and backward bidirectional nodes.
The data structure is now defined as follows:

typedef struct _TREE_NODE{INT data;struct _tree_node* prev;struct _tree_node* next;struct _tree_node* left;struct _TREE_ node* right;} Tree_node;
With the addition of nodes, we may need to add prev, next processing steps.

void Set_link_for_insert (tree_node* pparent, tree_node* pnode) {if (NULL = = Pparent | | NULL = = Pnode) return;if (Pnode = pparent->left) {Pnode->prev = Pparent->prev;if (Pparent->prev) pParent-> Prev->next = Pnode;pnode->next = Pparent;pparent->prev = Pnode;} Else{pnode->next = Pparent->next;if (pparent->next) Pparent->next->prev = PNode;pNode->prev = Pparent;pparent->next = Pnode;} return;} STATUS Add_node_into_tree (tree_node** pptreenode, int data) {tree_node* phead; tree_node* pnode;if (Null = = Pptreenode) return false;if (null = = *pptreenode) {*pptreenode = Create_new_node (data); return TRUE;} if (NULL! = Find_data_in_tree (*pptreenode, data)) return False;phead = *pptreenode;while (1) {if (Data < Phead->data) {if (phead->left) {phead = Phead->left;} Else{pnode = Create_new_node (data);p head->left = Pnode;break;}} Else{if (phead->right) {phead = Phead->right;} Else{pnode = Create_new_node (data);p head->right = Pnode;break;}}} Set_link_for_insert (Phead, Pnode); return TRUE;} 
Adding nodes this way, deleting a node does not work carelessly.

void Set_link_for_delete (tree_node* pnode) {if (Pnode->prev) {if (pnode->next) {pnode->prev->next = pNode- >next;pnode->next->prev = Pnode->prev;} Elsepnode->prev->next = NULL;} Else{if (pnode->next) Pnode->next->prev = NULL;}} Tree_node* _delete_node_from_tree (tree_node* root, tree_node* pnode) {tree_node* Pleftmax; tree_node* pleftmaxparent; tree_node* pparent = Get_parent_of_one (root, Pnode), if (null = = Pnode->left && NULL = = pnode->right) {if ( Pnode = = pparent->left) Pparent->left = Null;elsepparent->right = NULL;} else if (NULL! = Pnode->left && NULL = = pnode->right) {if (Pnode = = pparent->left) Pparent->left = pnode- >left;elsepparent->right = Pnode->left;} else if (NULL = = Pnode->left && null! = Pnode->right) {if (Pnode = = pparent->left) Pparent->left = pnode- >right;elsepparent->right = Pnode->right;} Else{pleftmax = Get_max_node_of_one (Pnode->left); if (Pleftmax = = pnode->left) {pnode->left->right = pnode->right;if (Pnode = = pparent->left) Pparent->left = pnode->left;elsepparent-> right = Pnode->left;} Else{pleftmaxparent = Get_parent_of_one (root, Pleftmax);p node->data = Pleftmax->data;pleftmaxparent->right = Null;pnode = Pleftmax;}} return pnode;} STATUS Delete_node_from_tree (tree_node** pptreenode, int data) {tree_node* pnode; tree_node* Pleftmax; tree_node* pleftmaxparent;if (NULL = = Pptreenode | | NULL = = *pptreenode) return false;if (null = = (Pnode = Find_data_in_tree (*pptreenode, data)) return false;if (Pnode = = *pptr Eenode) {if (null = = Pnode->left && NULL = = pnode->right) *pptreenode = Null;else if (null! = Pnode->left &am p;& NULL = = pnode->right) *pptreenode = Pnode->left;else if (null = = Pnode->left && null! = Pnode->r ight) *pptreenode = pnode->right;else {Pleftmax = Get_max_node_of_one (Pnode->left); if (PNode->left = = PLeftMax ) {pnode->left->right = Pnode->right;*pptreenode = Pnode->lEFT;} Else{pleftmaxparent = Get_parent_of_one (*pptreenode, Pleftmax);p node->data = pleftmax->data;pleftmaxparent- >right = Null;pnode = Pleftmax;}} Goto Final;} Pnode = _delete_node_from_tree (*pptreenode, Pnode); Final:set_link_for_delete (Pnode); free (pnode); return TRUE;}

Where the code for finding the maximum node and looking for the parent node is as follows:

tree_node* Get_max_node_of_one (tree_node* pnode) {if (NULL = = Pnode) return Null;while (pnode->right) Pnode = pNode- >right;return Pnode;} Tree_node* Get_parent_of_one (tree_node* root, tree_node* pnode) {if (NULL = = Root | | NULL = = Pnode) return Null;while (root) {if (Pnode = = Root->left | | pnode = = root->right) return Root;else if (pnode-> Data < Root->data) root = Root->left;elseroot = Root->right;} return NULL;}

Summarize:
(1) The key to sequencing binary tree is to add a forward pointer and a successor pointer to a binary tree node
(2) Sorting binary tree is a typical case of space exchanging time
(3) Sort the binary tree is the basis of many structures, write how many times are not many, have the chance that friends should practice more
(4) test case writing is the key to code writing, the purpose of the program is to eliminate bugs, especially low-level bugs


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

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.