Binary sort tree Establishment, find, delete node operation.
#include <stdio.h> #include <iostream> #include <algorithm> #include <string.h>using namespace std;struct node{int key;///node value node *left; Node *right; Node *parent;///pointer to Father node};///recursive middle order traversal sort binary tree void Show (node *p) {if (p==null) return; if (p->left! = NULL) show (P->left); printf ("%d", p->key); if (p->right! = NULL) show (p->right);} Insert node void Inseart (node * * Root,int key) {node *p=new node; Node1 p= (node1) malloc (sizeof (node)); p->key=key; p->left=p->right=p->parent=null; Empty tree as root node if ((*root) ==null) {*root=p; Return }////To insert the left child into *root//printf ("%d\n", Root->key); if ((*root)->left==null&& (*root)->key>key) {p->parent= (*root); (*root)->left=p; Return }////Insert to *root right child if ((*root)->right==null&& (*root)->key<key) {p->parent= (*root); (*root)->right=p; Return } if ((*root)->key>key) Inseart (& (*root)->left,key); else if ((*root)->key<key) Inseart (& (*root)->right,key); else return;} Create two fork sort tree void Create (node **root,int *a,int length) {for (int i=0; i<length; i++) Inseart (Root,a[i]);} Find binary sort tree node* search (node *root,int key) {if (root==null) return NULL; if (Key>root->key) return search (Root->right,key); else if (Key<root->key) return search (Root->left,key); else return root;} Find the minimum keyword, when the empty tree returns nullnode* searchmin (node *root) {if (root==null) return NULL; if (root->left==null) return root; else return searchmin (root->left);} Find the maximum keyword when empty tree returns nullnode* Searchmax (node *root) {if (root=null) return NULL; if (root->right==null) return root; else return Searchmax (root->right);} Find the precursor of a node (the first node in a sorted binary tree that is smaller than its value) node* searchpredecessor (node *p) {///empty tree if (p==null) return p; There is left dial hand tree, the largest of the left subtree if (p->left) return Searchmax (P->left); No left subtree, finds the right subtree of a node, traverses the else {if (p->parent==null) reutrn NULL; Look up the precursor while (p) {if (p->parent->right==p) break; p=p->parent; } return p->parent; }}///finds the successor of each node (the first node in a sorted binary tree that is larger than its value) node* searchsuccessor (node* p) {///empty tree if (p==null) return p; There is a right subtree, and the smallest of the right subtree is the IF (p->right) return searchmin (p->right); No right subtree, finding the left subtree of a node has traversed the else {if (p->parent==null) return NULL; while (p) {if (p->parent->left==p) is break; p=p->parent; } return p->parent; }}///Delete node, node does not exist return 0int delete_node (node * root,int key) {node* q; Node *p=search (*root,key); int temp; if (!p) return 0; 1. The deleted node is the leaf node, directly delete if (P->left==null& &p->right==null) {if (p->parent==null) {Delete (P); (*root) =null; } else {if (p->parent->left==p) p->parent->left=null; else p->parent->right=null; Delete (p); }}///2 the deleted node is only left dial hand tree else if (p->left&&! ( P->right)) {p->left->parent=p->right; If the parent node is deleted, change the parent node pointer if (p->parent==null) *root=p->left; Delete the Father node of the left child else if (p->parent->left==p) p->parent->left=p->left; Delete the Father node of the right child else p->parent->right=p->left; Delete (p); ///3 the deleted node is only right child else if (p->right&&! ( P->left)) {p->right->parent=p->parent; If the parent node is deleted, change the parent node pointer if (p->parent==null) *root=p->right; Delete the Father node of the left child else if (p->parent->left==P) p->parent->left=p->right; Delete the Father node of the right child else p->parent->right=p->right; Delete (p); ///4 the deleted node has both the left child and the right child else {q=searchsuccessor (P); temp=q->key; Delete_node (Root,q->key); p->key=temp; } return 1;} int main () {int a[11]= {15,6,18,3,7,17,20,2,4,10,9}; Node *root = NULL; Create (&root,a,11); Traverse Show (Root); Find printf ("%d\n", Search (root,17)->key); Delete for (int i=0; i<11; i++) {printf ("%d\n", A[i]); Delete_node (&root,a[i]); Show (root); printf ("\ n"); } return 0;}
Binary search tree (binary sort tree)