Binary search tree has the following properties:
Insert (recursive)
After insertion of the data to meet the nature of binary 1 and 2, you must first find the insertion position, and the position must be inserted under the leaf node
So inserted in two steps:
1, determines that the insertion position is below a leaf node
2, a new node, so that the leaf node point to the new node
node* insert(node* root,int x) { // insert position must be in a leaf node, here is the first recursive find the leaf node, IF (root == NULL) { // then create a new node, so that the leaf node points to the node the Node * the TEMP = ( struct the Node *) malloc ( sizeof ( struct the Node)); temp->data=x; temp->left=NULL; temp->right=NULL; return temp; } if(x<root->data) { root->left=insert(root->left,x); } the else the root -> right = INSERT (directory root-> right, X); return the root; // when inserted is recursive, it will eventually return to the root }
Find (recursive)
From the root constantly looking down on the line
bool search(node * root,int x) { // recursively to child nodes did not find any course, that this node does not exist IF (the root == NULL) return to false ; the else IF (X <directory root-> Data) return Search (directory root-> left, X); the else IF ( X> directory root-> Data) return Search (directory root-> right, X); the else return to true ; }
Delete (recursive)
Deleted when the more complicated, because the deletion of nodes is not necessarily in the leaf node, it is possible in other positions, after deleting a binary tree node but also to maintain the integrity of nature and trees
How to maintain the integrity and nature of a binary search tree it?
1, start with how to maintain the integrity of the tree:
After finding the location of the nodes are removed, we do not delete this node directly, but to find a node is deleted from sub-tree successor node , to replace the removed node with the value of the successor node,
Then delete the successor node (there is a recursive, recursively to child nodes, and eventually delete the leaf node, deleting the leaf node does not affect the integrity of the tree)
2, how to maintain a binary search tree property, that is, how to find a successor nodes (this is nature: the left is smaller than the root node, right node larger than the root)
1, if both nodes are removed in this sub-tree, but also the right subtree
Right subtree to find a minimum, the minimum is the successor node
For example to explain:
If I want to delete a node value of 3, find the right child tree node 3 is a minimum value of 4,4 larger than all right sub-tree, and all values are smaller than the left subtree, so you can let 4 to replace 3 to become root
Code
// Find the successor node (minimum right sub-tree) in the right subtree the Node * FindMin (the Node * root) { if(root==NULL) return NULL; else if(root->left==NULL) return root; else return FindMin(root->left); }
2, if the node is removed only both left subtree
The left node to find a successor node becomes the maximum value, the principle above
Code
// Find the successor node (maximum left sub-tree) in the left subtree the Node * FindMax (the Node * root) { if(root==NULL) return NULL; else if(root->right==NULL) return root; else return FindMax(root->right); }
3, if both deleted this node is only right subtree
Right subtree to find a successor node becomes minimum
Delete code
// delete the node is x the Node * DELET (the Node * root, int x) { // recursive exports is a leaf node IF (root == NULL) { return root; } else { if(x<root->data) root->left=delet(root->left,x); else if(x>root->data) root->right=delet(root->right,x); else { // left and right node exists IF (root-> left && root-> right) { Node * TEMP = FindMin (directory root-> right); // the value of the current node is modified to the value of the successor node directory root-> Data = temp-> Data; // find the position of the successor node in the right subtree, and deleting directory root-> right = DELET (directory root-> right, directory root-> Data); } else { // there is only one child node IF (directory root-> left == NULL) root=root->right; if(root->right==NULL) root=root->left; } } } return the root; // return the root }
Traversal (recursive)
Tree traversal in three ways: the preamble, after the sequence, sequence
void inOrder (the root Node *) // to the output order sequence { IF (directory root-> left = NULL!) // output the left node inOrder (directory root-> left); COUT << directory root-> Data << " " ; // Output root IF (directory root-> right =! NULL) inOrder (the root -> right); // output right node } void preOrder (the root Node *) // to first order output sequence { cout << root->data << " ";//根 if(root->left!=NULL) preOrder(root->left);//左 if(root->right!=NULL) preOrder (the root -> right); // Right } void houOrder (the root Node *) // subsequent output sequence { IF (directory root-> left = NULL!) // left houOrder (directory root-> left); IF (the root -> right = NULL)! // Right houOrder (root-> right); cout << root->data << " ";//根 }
The complete code
#include<iostream> #include<stdlib.h> #include<string.h> using namespace std; struct node { int data; struct node* left; struct node* right; }; // Find the value of x node whether the binary tree BOOL Search (the Node * root, int x) { // recursively to child nodes did not find any course, that this node does not exist IF (the root == NULL) return to false ; the else IF (X <directory root-> Data) return Search (directory root-> left, X); the else IF ( X> directory root-> Data) return Search (directory root-> right, X); the else return to true ; } // insert a node to the tree Node INSERT * (* the root Node, int X) { // insert position must be in a leaf node, here is the first recursive find the leaf node, IF (root == NULL) { // then create a new node, so that the leaf node points to the node the Node * the TEMP = ( struct the Node *) malloc ( sizeof ( struct the Node)); temp->data=x; temp->left=NULL; temp->right=NULL; return temp; } if(x<root->data) { root->left=insert(root->left,x); } the else the root -> right = INSERT (directory root-> right, X); return the root; // when inserted is recursive, it will eventually return to the root node } // find the successor nodes (left subtree in the left subtree maximum) Node * FindMax (Node * the root) { if(root==NULL) return NULL; else if(root->right==NULL) return root; else return FindMax(root->right); } // Find the successor node (minimum right sub-tree) in the right subtree the Node * FindMin (the Node * root) { if(root==NULL) return NULL; else if(root->left==NULL) return root; else return FindMin(root->left); } // delete the node is x the Node * DELET (the Node * root, int x) { // recursive exports is a leaf node IF (root == NULL) { return root; } else { if(x<root->data) root->left=delet(root->left,x); else if(x>root->data) root->right=delet(root->right,x); else { // left and right node exists IF (root-> left && root-> right) { Node * TEMP = FindMin (directory root-> right); // the value of the current node is modified to the value of the successor node directory root-> Data = temp-> Data; // find the position of the successor node in the right subtree, and deleting directory root-> right = DELET (directory root-> right, directory root-> Data); } else { // there is only one child node IF (directory root-> left == NULL) root=root->right; if(root->right==NULL) root=root->left; } } } return the root; // return the root } void PrintBSTree (the root Node *) // preorder traversal output { IF (the root == NULL) return ; the else { cout<<root->data<<' '; PrintBSTree(root->left); PrintBSTree(root->right); } } void inOrder (the root Node *) // to the output order sequence { IF (directory root-> left = NULL!) // output the left node inOrder (directory root-> left); COUT << directory root-> Data << " " ; // Output root IF (directory root-> right =! NULL) inOrder (the root -> right); // output right node } void preOrder (the root Node *) // to first order output sequence { cout << root->data << " ";//根 if(root->left!=NULL) preOrder(root->left);//左 if(root->right!=NULL) preOrder (the root -> right); // Right } void houOrder (the root Node *) // subsequent output sequence { IF (directory root-> left = NULL!) // left houOrder (directory root-> left); IF (the root -> right = NULL)! // Right houOrder (root-> right); cout << root->data << " ";//根 } int main () { int x,n; node* root=new node; root=NULL; cin>>n; for(int i=0;i<n;i++) { cin>>x; root=insert(root,x); } preOrder(root); cout<<endl; delet(root,4); preOrder(root); cout<<endl; return 0; }