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

Source: Internet
Author: User

First, prefaceall along, the tree-related things are prohibitive. Before every time said to see, because of inertia, time is so wasted. Make a decision today and decide what to look for in a good data structure. 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 (^ω^) .
definition of a two or two-fork search treeTwo fork 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 (too lazy to draw your own pictures, 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.
Common operations related to three or two-fork search tree1, establish;2, insert a node;3, delete a node;4, find the smallest and largest node;5, find out the node of the former node and the successor node.
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, the node "8" smaller than the nodes have many, but the largest one node is "7", then the node "7" is the node "8" of the forward node; successor: The successor node of a node, which 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
"Maximum & Minimum Value"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."Forward node"to find a node in 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 node"to find the successor node of a node, we are divided into two cases
    • 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.
as shown

Full Code
#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 << "Minimum node is:" <&Lt Findminnode (pbsttree)->key << endl;cout << "Max node:" << Findmaxnode (pbsttree)->key << endl;/* looking for a node's precursor 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 &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 subsequent nodes */pnode findsuccessor (Pnode &psearchnode) {/* If the right subtree exists, it returns the smallest node in the right subtree, which is the smallest node that is 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, consider using the node's predecessoror 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

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.