-
- Introduction to Binary search tree
- Collection operations
- Search searches
- Mininum to find the smallest key node of a subtree
- Maxnum subtree Max key node
- Predecessor to find the largest node smaller than this node
- Succesor Post
- Insert Insertion
- Delete Deletes
- C implementation
Introduction to Binary search tree
Binary search tree, also known as binary tree.
It is a special two-fork tree: For binary trees, suppose X is a node in a two-fork tree, X-nodes contain the keyword key, and the key value of Node x is recorded as Key[x]. If Y is a node in the left subtree of x, then key[y] <= key[x], and if Y is a node of the right subtree of x, then key[y] >= key[x]. So this tree is a binary search tree.
Collection Operation Search
There are iterations and recursive versions.
The idea is to find the appropriate value down, the value will return the node, no suitable value will go down until NULL is returned
/* * (Recursive implementation) find the node in "Binary tree X" with key value */BNode*Search (Bstree x,TypeKey) {if(x==NULL ||X -Key==KeyreturnXif(Key<X -KeyreturnBstree_search (x -Left, key);Else returnBstree_search (x -Right, key);}/* * (non-recursive implementation) find the node in "Binary tree X" with key value */BNode*Search (DataType key) const{BNode*P=Root while(p!=NULL&&P -Key!=Key) {if(Key<P -Key) {P=P -Left }Else{p=P -Right } }if(p!=NULL) printf ("Search%d\n"P -Key);returnP }
Mininum to find the smallest key node of a subtree
The minimum node is the leftmost child node
Maxnum subtree Max key node
The maximum node is the right child node
Predecessor to find the largest node smaller than this node
In both cases,
Normally it is the largest node of the left subtree of the second node, if there is no left subtree, look up root
BNode*Predecessor (BNode*T) {BNode*X=Tif(x -Left!=NULL) {returnMaxmun (x -left); } BNode*Y=X -P while(Y!=NULL&&X==Y -left) {x=Y Y=Y -P }returnY };
Succesor Post
In the same two cases, the minimum value of the right subtree, or the absence of a right sub-tree, the root is found
BNode*Successor (BNode*T) {BNode*X=Tif(x -Right!=NULL){returnMinmun (x -right); } BNode*Y=T -P while(Y!=NULL&&Y -Right==x) {X=Y Y=Y -P }returnY }
Insert Insertion
Insert Downward,
In both cases, there is root and no root. Note the operation on root
Delete Deletes
Delete node X
1. No left subtree, right sub-tree connected.
2. No right sub-tree, Zuozi connected.
3. Around, looking for the right subtree minimum y
3.1 If the minimum value is a direct right subtree, replace it directly with the
3.2 Not directly right subtree, replace y right subtree with y,y and replace X
C + + implementation
#include <iostream>#include <Cstdio>#include <CString>#include <Cstdlib>using namespace Std;typedef int datatype;struct bnode{DataType key; BNode*P*Left*right;}; Class btree{Private: BNode*Root Public: Btree () {root=NULL; }; ~btree () {destorynode (root); Root=NULL; };voidInsert (DataType key) {BNode*Z=NewBNode; BNode*Y=NULL; BNode*X=Root Z -Key=Key Z -P=Z -Right=Z -Left=NULL;//Initialize x, Y and Z while(x!=NULL) {Y=Xif(x -Key>Key) {X=X -Left }Else{x=X -Right } }//x pointing to root, looking downZ -P=Yif(Y==NULL) {root=Z }Else if(Y -Key>Key) {Y -Left=Z }Else{y -Right=Z }//Establish the relationship between Y and Z of the parent node of Zprintf"Node%d"Z -Key);if(Y!=NULL) {printf ("P%d\n"Y -Key); }Else{printf ("\ n"); } }; BNode*Search (DataType key) const{BNode*P=Root while(p!=NULL&&P -Key!=Key) {if(Key<P -Key) {P=P -Left }Else{p=P -Right } }if(p!=NULL) printf ("Search%d\n"P -Key);returnP } BNode*Minmun (BNode*T) {BNode*P=T while(p -Left!=NULL) {P=P -Left } printf ("%d ' s Minmun%d\n"T -Key,p -Key);returnP } BNode*Maxmun (BNode*T) {BNode*P=T while(p -Right!=NULL) {P=P -Right } printf ("%d ' s Maxmun%d\n"T -Key,p -Key);returnP } BNode*Successor (BNode*T) {BNode*X=Tif(x -Right!=NULL){returnMinmun (x -right); } BNode*Y=T -P while(Y!=NULL&&Y -Right==x) {X=Y Y=Y -P }returnY } BNode*Predecessor (BNode*T) {BNode*X=Tif(x -Left!=NULL) {returnMaxmun (x -left); } BNode*Y=X -P while(Y!=NULL&&X==Y -left) {x=Y Y=Y -P }returnY };voidTranslate (BNode*U,bnode*V) {if(U -P==NULL) {root=V }Else if(U -P -Left==u) {u -P -Left=V }Else{u -P -Right=V }if(V!=NULL) {V -P=U -P }} int Treedelete (BNode*Z) {if(Z -Left==NULL) {printf ("Translate z z->right\n"); Translate (z,z -right); Delete Z; }Else if(Z -Right==NULL) {printf ("Translate z z->left\n"); Translate (z,z -left); Delete Z; }Else{BNode*Y=Minmun (Z -right);if(Y -P!=Z) {Translate (y,y -right); Y -Right=Z -Right Y -Right -P=Y printf"Translate y y->right\n"); } Translate (Z,y); Y -Left=Z -Left Y -Left -P=Y printf"Translate z y\n"); } }voidDestorynode (BNode*T) {if(T -Left!=NULL) {Destorynode (T -left); }if(T -Right!=NULL) {Destorynode (T -right); } printf ("Destory%d\n"T -Key); BNode*P=T Delete p; };}; int main (int argc, char const*Argv[]) {Btree *t=new Btree (); T->insert (3); T->insert (7); T->insert (1); T->insert (12); T->insert (8); BNode *t=t->search (12); BNode *t1=t->search (3); T->maxmun (t); T->minmun (t); T->treedelete (t); T->treedelete (t1); Delete T; return 0;}
Introduction to Algorithms notes (5) binary search tree