Binary sort Tree Definition:
1, with a binary tree to organize.
2. The include attribute points to the left child, right child, and parents.
3, to any node x, the left subtree of the keyword maximum not more than X->value, the right subtree of the minimum number of keywords not less than x->value.
The implementation code is as follows:
#include <stdio.h> #include <stdlib.h>struct treenode{int value;struct TreeNode *left;struct TreeNode * Right;struct TreeNode *p;}; typedef struct TREENODE node;//Insert node *insertnode (node *t,node *insert) {node *y = NULL; Node *x= T;while (x!=null) {y=x;if (insert->value>x->value) X=x->right;else X=x->left;} Insert->p=y;if (y==null) t=insert;else{if (insert->value<y->value) Y->left=insert;else y->right= Insert;} Insert->right=null;insert->left=null;return T;} Middle order traversal void inorder (Node *t) {if (t==null) {return;} Inorder (T->left);p rintf ("%d\t", T->value); Inorder (t->right);} Replace a subtree with a V root with a subtree that is rooted in U node *transplant (node *t,node *u,node *v) {if (u->p==null) {t=v;} else if (u==u->p->left) {u->p->left=v;} Else{u->p->right=v;} if (v!=null) {v->p=u->p;} return T;} Find the smallest element in the tree T node *treemininum (node *t) {while (t->left!=null) T=t->left;return T;}//delete a node in the tree node *deletenode ( Node *t,node *z) {if (t->left==null) {t=transplant (T,z,z->right);} else if (t->right==null) {t=transplant (t,z,z->left);} Else{node *y=treemininum (z->right); if (y->p!=z) {t=transplant (t,y,y->right); y->right=z->right;y- >right->p=y;} T=transplant (t,z,y); y->left=z->left;y->left->p=y;} return T;} int main (int argc, char *argv[]) {Node *t = Null,*insert=null;srand ((unsigned) time (NULL)); int n;scanf ("%d", &n); int * num= (int *) malloc (sizeof (int) *n), int i=0;for (; i<n;i++) {Num[i]=rand ()%100;insert= (node *) malloc (sizeof (node)); insert->value=num[i];insert->p=null; T = Insertnode (T,insert);} printf ("\ n------------randomly generated number------------\ n"), int j=0;for (; j<n;j++) {printf ("%d\t", Num[j]);} printf ("\ n------------the middle sequence traversal of the binary sort tree------------\ n"), Inorder (T);p rintf ("\ n------------Delete an element of the binary sort tree after the middle order traversal------------ \ n "); t = Deletenode (t,t), inorder (t);p rintf ("\ n"); return 0;}
Next we analyze the complexity of the execution time of each module.
For a middle order traversal of void Inorder (node *t), if there are N nodes, then the call time is O (n).
For the smallest node in the Find tree T node *treemininum (node *t), if there are N nodes, then the call time is O (LGN).
For the Insertion node node *insertnode (node *t,node *insert), if there are N nodes, then the call time is O (LGN).
For deleting a node node *deletenode (node *t,node *z), if there are N nodes, then the call time is O (LGN).
There are three cases of deletion:
1. Delete node without child node, delete directly, and modify the parent node of Z, replace Z with NULL.
2, delete the node has a child, then the child node raised to the position of z in the tree, and modify the parent node of z, with the child Z replacement Z.
3, delete the node has two children, first find the successor of Z y, and let y occupy the position of z in the tree. There are two different situations here:
3.1, Y is the right child of Z, then the parent node of the direct y substitution z,z becomes the parent node of Y, and the left child of Z becomes the left child of Y.
3.2, Y is not the right child of Z, then use Y's right child to replace Y, and then replace Z with y, and modify the children of Y and so on information.
The time complexity is O (1) for replacing a subtree that is rooted in V with a subtree of node *transplant (node *t,node *u,node *v) with a root of U.
Resources:
Introduction to Algorithms
Note:
Reprint Please specify source: http://blog.csdn.net/wsyw126/article/details/51365964
WSYW126
Binary sort tree with introduction to the algorithm