Implementation of C + + for binary search tree establishment, insertion, deletion, forward node, successor node

Source: Internet
Author: User

First, the preface has always been to the tree-related things to be deterred. Before every time said to see, because of inertia, time is so wasted. Make a decision today, decide to be gooddata structure in the things to look at. Do not know to see this article you, is not and I have the same feeling, empty a hard heart, but slow to pay action. If so, if also want to take good knowledge of the tree to consolidate, let us together chew knowledge point over it. This article strives to let every student who has no foundation after reading, can have some gain. Before the text begins, add an oil to yourself. Come on (^ω^) .Two or two fork search tree definition Binary search tree means that for a node, its left node is less than or equal to it. and the node on its right is greater than or equal to it. Borrow a picture of the Internet (lazy yourselfdrawing, please forgive me). As shown, is a binary search tree, you can find a few nodes on their own to try to verify this definition. Three or two fork search tree for common operations 1, build, 2, insert a node, 3, delete a node, 4, find the smallest and largest node, 5, identify the node's predecessor and successor nodes. The work done in the previous 4 is a good understanding, but to understand the work done in the first section, you need to understand what the previous and subsequent nodes are. Let's review what is the forward node and what is the successor node. \ (^o^)/~, come on! Forward node: A node's forward node, which is the largest node in all nodes that are smaller than that node. As in, there are many nodes smaller than node "8", but the largest node is "7", then node "7" is the forward node of node "8"; successor: the successor node of a node is the smallest node in all nodes that are larger than that node. As in the above node "1" larger than the node has a lot of songs, but the smallest node is "3", then the node "3" is the node "1" of the successor node. Under INSERT, let's take a look at the specific steps that each operation implements. Creating a binary search tree is actually inserting n nodes. Instead of inserting a node, we do this:
    1. If a node is small, and the left child of the root node is empty, then the node is directly the left child of the root node;
    2. If a node is small, but the left child of the root node is not, then we take the left child of the root node as the new root node, recursively 1, 22 steps.
    3. Similarly, we can know how to handle a situation where a node is larger than the root node.
    4. Here is a special case to explain, that is, if you encounter duplicate nodes, this article adopts soft-insertion and soft-deletion method. That is, we set a variable to indicate the number of occurrences of the node, and if the node to be inserted already exists in the binary search tree, then we only need to add 1 of the number of occurrences of this node variable.
As shown in "Maximum & minimum" It is simpler to find the maximum and minimum values for a binary tree, because the minimum value of the binary tree is definitely on the leftmost side of the binary tree, and the maximum value is definitely at the far right of the binary tree. We only need to use a loop that has been looping to the left or right node of the node separately, then we can get the maximum and minimum value of the two fork tree. For example, the minimum node is 2 and the maximum node is 18. "Forward node" to find a node of the forward node, we are divided into two cases
    • If the node has left child, then the anterior node is the maximum value to be found by the left child of the node as the root node;
    • If the node does not have a left child, then the anterior node needs to look up the node for the right child.
As in, 12 of the previous node is 10. "Successor" find the successor node of a node, we are divided into two kinds of situations
    • If the node has a right child, then the anterior node is the minimum value to be found by the left child of the node as the root node;
    • If the node does not have a right child, then the anterior node needs to look up the node for the left child.
For example, 3 of the successor node is 4. "Delete" Delete a node, we are also divided into three cases
    1. If the node to be deleted has no left child and no right child, delete the node directly, but note that the corresponding parent node needs to be modified. For example, if the node to be deleted is the left child of the parent node, then the left child node of the parent node should be empty;
    2. If the node you want to delete has only one child, use that child instead of the node directly. Here, you also need to be aware of modifying the parent node. That is, if the left child of the parent node is to be deleted, and it has only one right child, then deleting the node needs to point the left node of the parent node to the right child of the node to be deleted;
    3. If the node to be deleted has two children, we can choose to use the node's forward node or the successor node instead of the node. You can try in the pen, you will find two alternative ways, can ensure that the updated two-fork tree still satisfies the definition of binary search tree.
Complete code as shown
#include "StdAfx.h "#include <string> #include <stdlib.h> #include <iostream>using namespace std;typedef struct                Node{int key;     Key value struct node *left;//left node struct node *right;              Right node struct node *father;//parent node int times; Node occurrences} node, *pnode;void creatbinarysearchtree (pnode &pbsttree, int *ptr, int len), void Insertnode (Pnode & Pbsttree, int value), void Mallocinitnode (Pnode &pinsertnode, int value);p node Findminnode (pnode &ptree);p node Findmaxnode (pnode &ptree);p node findpredecessor (pnode &psearchnode);p node Findsuccessor (PNode & Psearchnode); void Deletenode (pnode& pdeletenode); void Changefatherchildnode (pnode& pdeletenode, pNode& PNEWCHILDNODE); int main () {int a[] = {15,15, 6, 3, 7, N, 2, 4, PBST, 9};int len = sizeof (a)/sizeof (int);p node Tree = Null;pnode Pprenode = Null;pnode Psuccessor = null;/* Create two fork find Tree */creatbinarysearchtree (Pbsttree, A, Len);/* Find binary search tree The maximum value of */cout << "Min node is:" << FINDMINnode (pbsttree)->key << endl;cout << "Max node:" << Findmaxnode (pbsttree)->key << endl;/* Find the precursor node of a node */pprenode = Findpredecessor (pbsttree->left->right); if (NULL! = Pprenode) {cout << "the node's precursor node is:" << pprenode->key << Endl;} Else{cout << "No precursor node for this node" << Endl;} /* Search for the successor node of a node */psuccessor = Findsuccessor (Pbsttree->left->left->left); if (NULL! = Pprenode) {cout << The successor node of the node is: "<< psuccessor->key << Endl;} Else{cout << "Node no successor" << Endl;} /* Delete a node */deletenode (pbsttree->right->right);d Eletenode (pbsttree->left->left); cout << "Minimum node is : "<< findminnode (pbsttree)->key << endl;psuccessor = Findsuccessor (Pbsttree->left->left); if ( NULL! = Pprenode) {cout << "subsequent nodes of this node are:" << psuccessor->key << Endl;} Else{cout << "Node no successor" << Endl;} Free (pbsttree);p bsttree = Null;return 0;} /* Create a binary find tree */void creatbinarysearchtree (pnode &AMP;PBSttree, int *ptr, int len) {for (int i = 0; i < len; i++) {Insertnode (pbsttree, * (ptr + i));}} /* Insert a node with a complexity of O (NLOGN) */void Insertnode (pnode &pbsttree, int value) {Pnode pinsertnode;/* first node, insert */if directly (NULL = = PBS Ttree) {Mallocinitnode (Pinsertnode, value);p bsttree = Pinsertnode;return;} /* If the key value already exists, only need times++ */if (value = = Pbsttree->key) {Pbsttree->times++;return;} /* If it is less than the value of this node, and the node has no left child */if ((NULL = = Pbsttree->left) && (Value < Pbsttree->key)) {Mallocinitnode ( Pinsertnode, value);p insertnode->father = Pbsttree;pbsttree->left = Pinsertnode;return;} /* If it is greater than the value of this node and the node has no right child */if ((NULL = = pbsttree->right) && (Value > Pbsttree->key)) {Mallocinitnode ( Pinsertnode, value);p insertnode->father = Pbsttree;pbsttree->right = Pinsertnode;return;} /* If it is less than the value of this node, but the node already has a left child, then continue to recursive */if ((NULL! = pbsttree->left) && (Value < Pbsttree->key)) {Insertnode (pbsttree->left, value);} /* If it is greater than the value of this node, but the node already has a right child, then continue to recursive */if ((NULL! = Pbsttree-> right) && (Value > Pbsttree->key)) {Insertnode (pbsttree->right, value);}} /* Create a new node and initialize */void Mallocinitnode (pnode &pinsertnode, int value) {Pinsertnode = (pnode) malloc (sizeof (Node)); Pinsertnode->key = Value;pinsertnode->father = Null;pinsertnode->left = Null;pinsertnode->right = NULL; Pinsertnode->times = 1;} /* Find the smallest node in the binary tree and the largest node */pnode findminnode (Pnode &ptree) {Pnode ptemp = ptree;while (NULL! = ptemp->left) {ptemp = Ptem P->left;} return ptemp;} Pnode Findmaxnode (Pnode &ptree) {Pnode ptemp = ptree;while (NULL! = ptemp->right) {ptemp = Ptemp->right;} return ptemp;} /* Locate the precursor node */pnode findpredecessor (Pnode &psearchnode) {/* If the left dial hand tree exists, it returns the largest node in the left subtree, which is the largest node in the smaller one than it */if (NULL! = Psearchnode->left) {return findmaxnode (psearchnode->left);} /* If the left dial hand tree does not exist, it needs to be looked up until the target node is found to be the right child of the target node's father node */pnode ptemp = Psearchnode;while (ptemp! = ptemp->father->right) { Ptemp = Ptemp->father;} return ptemp->father;} /* Find the successor */pnode Findsuccessor (Pnode &Amp;psearchnode) {/* If the right subtree exists, it returns the smallest node in the right subtree, which is the smallest node larger than it */if (NULL! = psearchnode->right) {return Findminnode ( Psearchnode->right);} /* If the left dial hand tree does not exist, it needs to be looked up until the target node is found to be the right child of the target node's father node */pnode ptemp = Psearchnode;while (ptemp! = ptemp->father->left) { Ptemp = Ptemp->father;} return ptemp->father;} void Deletenode (pnode& pdeletenode) {/* 1. Determine the number of nodes, if the number of nodes is greater than or equal to 1, then the number of nodes directly-1 */if (1 < pdeletenode->times) { Pdeletenode->times--;return;}  /* 2. If the node has only one, then consider deleting *//* 2.1 if the node does not have children, then delete */pnode ptemp = Null;if ((null = = Pdeletenode->left) && (null = = Pdeletenode->right)) {Changefatherchildnode (Pdeletenode, ptemp);} /* 2.2 If the node has only one child, then use the child directly instead of the node */else if ((null = = Pdeletenode->left) && (null! = pdeletenode->right)) {CH Angefatherchildnode (Pdeletenode, pdeletenode->right);} else if ((null = = Pdeletenode->right) && (null! = Pdeletenode->left)) {Changefatherchildnode (Pdeletenode, Pdeletenode->left);} /* 2.3 If the node has two children, then consider using the node's predecessor or successor to replace the node。 Here we choose to replace the node with a precursor */else{ptemp = Findpredecessor (pdeletenode);p node Prightchild = pdeletenode->right; Changefatherchildnode (Pdeletenode, ptemp);p temp->right = Prightchild;}} void Changefatherchildnode (pnode& pdeletenode, pnode& pnewchildnode) {if (Pdeletenode = = pdeleteNode-> Father->right) {pdeletenode->father->right = Pnewchildnode;} Else{pdeletenode->father->left = Pnewchildnode;}}
Code Run Result:

Implementation of C + + for binary search tree establishment, insertion, deletion, forward node, successor node

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.