I. Overview
The search process of binary sort tree is similar to that of sub-optimal two fork tree, which usually takes binary list as the storage structure of binary sort tree. In order to get the ordered sequence of a key word, an unordered sequence can be constructed by constructing a binary sort tree, and the process of constructing the tree is the process of ordering the unordered sequence. Each new node that is inserted is a new leaf node on the two-fork sort tree, and the insertion operation does not have to move other nodes, just change the pointer of a node, and the null becomes non-empty. The complexity of searching, inserting, and deleting is equal to the height of the tree, O (log (n)).
Structure of the two or two-fork search tree
Binary search tree, for any node x, the key of Zuozi is not more than the key of X, the key sub-tree of right subtree is no less than the keyword of x, such as. Visually, it is a recursive structure of the small left and right. Recursive structures do not conform to our normal thinking and need to be carefully understood.
The C + + structure definition of the binary search book:
struct treenode{treenode* left;treenode*right;treenode*p;int value; Void*pdata;}; struct Tree{treenode *root;//int nsize;};
Third, the algorithm
Based on the definition of binary search tree structure, the following algorithms are mainly used.
(1) Insert
is to be able to insert the node in the correct position of the binary tree, first need to locate the location to be inserted, and then insert it.
void Treeinsert (Tree &tree, treenode*p) {treenode*ptemp=tree.root;//used to traverse TreeNode *y=null;//to save the correct result for traversal// Perform a binary traversal while (ptemp!=null) {y=ptemp; Save the result if (Ptemp->value<p->value) ptemp=ptemp->right;elseptemp=ptemp->left;} p->p=y; Set the parent node of the insertion node if (y==null)//If the result of the traversal is empty, it indicates that the tree is an empty tree, Tree.root=p;else if (y->value>p->value)//Insert the correct node Y->left =p;elsey->right=p;}
(2) Delete
Delete a binary search tree node Z, slightly more complex. In the subtree of the root of Z, find the successor of Z and form a subtree of the correct two-fork search, which is added to the parent node before Z.
It is divided into the following four types of cases:
A, the left sub-tree of Node Z is empty
B, the right sub-tree of Node Z is empty
C, the right node of Node Z is the successor of Z
D, the right node of node Z is not the successor of Z
void transplant (tree &tree,treenode*u,treenode*v)//will replace the subtree with the root of V with a subtree {if (u->p==null) tree.root=v;else if (u== U->p->left) u->p->left=v;elseu->p->right=v;if (V!=null) v->p=u->p;} void Treedelete (Tree &tree,treenode*z) {if (z->left==null)//Condition 1transPlant (tree,z,z->right); else if (z-> Right==null)//Condition 2transPlant (tree,z,z->left); Else{treenode*p=null;treenode*ptemp=z->right;while (pTemp!= NULL) {p=ptemp;ptemp=ptemp->left;} if (p==z->right)//Situation 3{transplant (tree,z,z->right);p->left=z->left;p->left->p=p;} else//situation 4{transplant (tree,p,p->right);p->right=z->right;p->right->p=p;transplant (tree,z,p);p- >left=z->left;p->left->p=p;}}
(3) Traverse
Traversal is the structure of the binary search tree.
void Treewalk (TreeNode *p) {if (p!=null) {treewalk (p->left); Std::cout<<p->value;treewalk (p->right);}}
(4) Enquiry
On the basis of binary search tree structure, a node of binary tree is queried.
TreeNode * Treesearch (treenode*p,int k) {if (P==null | | p->value==k) return p;if (K<p->value) return Treesearch ( P->LEFT,K); Elsereturn Treesearch (p->right,k);}
(5) Create a node
TreeNode *createnode (int k) {TreeNode *p=new treeNode ();p->value=k;p->p=null;p->left=null;p->right=null ;p->pdata=null;return p;}
Iv. Test Examples
int _tmain (int argc, _tchar* argv[]) {tree Tree;tree.root=null;treeinsert (tree,createnode); Treeinsert (Tree, CreateNode (one)); Treeinsert (Tree,createnode); Treeinsert (Tree,createnode (8)); Treeinsert (Tree,createnode (22)) ; Treeinsert (Tree,createnode (3)); Treedelete (Tree,treesearch (tree.root,8)); Treedelete (Tree,treesearch (Tree.root), 3)); Treewalk (tree.root); int J;std::cin>>j;return 0;}
Binary search Tree