1. What is a two-fork sort tree?
Binary sort tree is a special two-fork tree, which can be an empty tree or a two-tree with the following properties:
1. If the left subtree is not empty, then the value of all nodes of the left subtree is less than the value of its root node.
2. If the right subtree is not empty, then the value of all nodes in the right subtree is greater than the value of its root node.
3. Its left and right sub-tree is also a two-fork sorting tree.
Binary sort tree is also called binary search tree, is a dynamic lookup table, the so-called dynamic lookup table refers to the addition of query retrieval operations, but also can be inserted, delete the structure of operations, and the corresponding is static lookup table, only query retrieval operations.
2. Operation of the two-fork sorting tree
For binary sorting tree implementation is to find, insert, delete operation implementation, because to insert delete operation, so to use the chain list structure to implement, operation is described as follows:
1. Find key: Starting from the root node, if the key is greater than the root node to the right of the root node to go, and the root node of the right node comparison, if the key is small root node to the left side of the root node, and the root node to do a node comparison, the root node of the tree is two forks, so repeat the above steps, Until the key is found, or to null (the child node of the leaf node is empty).
2. Insert key: The same as the Find key step, return when key is found, do not insert, not found, in the last look at the leaf node inserted key value, if the key is greater than the leaf node value is inserted into the right side of the leaf node, as the leaf node of the right subtree, otherwise as the left subtree.
3. Delete key: The same as the Find key step, if the lookup is finally null (not found), then return, if the node P found key, then make a judgment:
A. If p is a leaf node, first find the parent node of P Q, if p is the left subtree of Q, then the left subtree pointer of q to NULL, if p is the right subtree of Q, then the right subtree pointer of q to NULL, delete p
B. If the left subtree of P is not empty and the right subtree is empty, then the parent node of P is found, q, if P is the left subtree of Q, then the left subtree of Q points to the left subtree of P, if p is the right subtree of Q, the right subtree of Q points to the left subtree of P, deleting p
C. If the right subtree of p is not empty and the left subtree is empty, then the parent node of P is found, q, if P is the left subtree of Q, then the left subtree pointer of Q points to the right subtree of P, if p is the right subtree of Q, the right subtree of Q points to the right subtree of p, deleting p
D. If the left and right subtrees of p are not empty, find the parent node Q of P, the left subtree of P is the child node of the leftmost node of the P right subtree, and if p is the right subtree of Q or the left subtree of Q, if P is the left subtree of Q, then the left subtree of Q points to the right subtree of P, if p is the right subtree of Q, The right subtree pointer of Q points to the right subtree of p, deleting p
3. Two fork Sorting Tree code implementation:
C code
Note: The following lookup operation is implemented recursively, since recursion has a recursive depth problem, too deep to cause a stack overflow, on my computer can be up to 10,357 recursion, available loops instead of recursion.
#include <stdio.h>typedef struct tnode{int nkey;struct tnode* lchild;struct tnode* rchild;struct TNode* parent;} tnode;//++++++++++++++++++++++++++ Find retrieval Operations ++++++++++++++++++++++++++/bool Searchbst (tnode* BST,int key,TNode* & psearch,tnode* &pparent) {if (! BST) {psearch = Pparent;return false;} if (Bst->nkey = = key) {Psearch = Bst;pparent = Psearch->parent;return true;} else if (bst->nkey>key) {pparent = Bst;return searchbst (bst->lchild,key,psearch,pparent);} Else{pparent = Bst;return Searchbst (bst->rchild,key,psearch,pparent);} return false;} ++++++++++++++++++++++++++ node insertion operation ++++++++++++++++++++++++++/bool Insertnode (tnode* &bst,int key,TNode* & Psearch) {tnode* pparent = null;if (Searchbst (bst,key,psearch,pparent)) return false; tnode* Pnode = new Tnode;pnode->nkey = Key;pnode->parent = Pparent;pnode->lchild = NULL;pNode->rchild = NULL;i F (!pparent) BST = Pnode;else if (key>pparent->nkey) Pparent->rchild = Pnode;else pparent->Lchild = Pnode;psearch = Pnode;return true;} ++++++++++++++++++++++++++ Delete node operation ++++++++++++++++++++++++++/bool deletenode (tnode* bst,int key) {TNode* PSearch = NULL; tnode* pparent = null;if (! Searchbst (bst,key,psearch,pparent)) return false;if (Null!=psearch->lchild && null!=psearch->rchild) { Tnode *ptmp = Psearch->rchild;while (ptmp->lchild) ptmp = Ptmp->lchild;ptmp->lchild = pSearch->lchild; Psearch->lchild->parent = ptmp->lchild;if (Pparent->lchild = = psearch) Pparent->lchild = pSearch-> Rchild;elsepparent->rchild = psearch->rchild;psearch->rchild->parent=pparent;} else if (null!=psearch->lchild) {if (Pparent->lchild = = psearch) Pparent->lchild = psearch->lchild; Elsepparent->rchild = Psearch->lchild;} else if (null!=psearch->rchild) {if (Pparent->lchild = = psearch) Pparent->lchild = Psearch->rchild;elseppar Ent->rchild = Psearch->rchild;} Else{if (Pparent->lchild = = Psearch) pparent->lcHild = Null;elsepparent->rchild = NULL;} Delete Psearch;psearch = Null;return true;} ++++++++++++++++++++++++++ free memory Operation ++++++++++++++++++++++++++/void Deletebst (tnode* BST) {if (! BST) return;if (! Bst->lchild &&! Bst->rchild) {if (! Bst->parent && bst->parent->lchild = BST) Bst->parent->lchild = Null;else if (! Bst->parent && Bst->parent->rchild = = BST) Bst->parent->rchild = null;delete BST; BST = Null;return;} Deletebst (Bst->lchild);D eletebst (bst->rchild);} ++++++++++++++++++++++++++main test ++++++++++++++++++++++++++/int Main (int argc, char* argv[]) {tnode* BST = NULL; tnode* pnode = NULL; tnode* pparent = Null;int ncount = 10358;for (int i=0;i<ncount;i++) {if (Insertnode (Bst,i,pnode)) printf ("Insert%d succeeded \ n", i) ;} if (Searchbst (bst,ncount-1,pnode,pparent)) {printf ("Find succeeded \ n");} Deletenode (bst,1); if (! Searchbst (bst,1,pnode,pparent)) {printf ("Find failed");} Deletebst (BST); GetChar (); return 0;}
Python code
The first time to learn python, the code must have a lot of shortcomings, I hope to correct, temporarily achieve the following:
#BST. Py_debug = false#+++++++++++++++++++++struct realized by class+++++++++++++++++++++ #class tnode:def __init__ (sel f,key,left,right,parent): Self.key=key self.lchild=left self.rchild=right SELF.PARENT=PARENTCL Ptnode:def __init__ (Self,tnode): self. Tnode=tnode#+++++++++++++++++++++++++search operation+++++++++++++++++++++++++ #def SearchBST (BST,key,search, Parent): If None==bst or None==bst.key:return False if key==bst.key:parent. Tnode=bst.parent Search. Tnode=bst return True elif key<bst.key:parent. Tnode=bst return Searchbst (bst.lchild,key,search,parent) else:parent. Tnode=bst return Searchbst (bst.rchild,key,search,parent) return False#+++++++++++++++++++++++++insert operation+ ++++++++++++++++++++++++ #def Insertnode (bst,key,search): Pparent=ptnode (BST) Psearch=ptnode (BST) if _Debug==True: Import PDB Pdb.set_trace () if Searchbst (Bst,key,psearch,pparent): Return False parent=pparent.tnode search=psearch.tnode node=tnode (key,none,none,none) if N One==parent or None==parent.key:bst=node return True if Key>parent.key:parent.rchild=node Else:parent.lchild=node node.parent=parent Search=node return true#+++++++++++++++++++++++++delete Operat ion+++++++++++++++++++++++++ #def Deletenode (bst,key): Pparent=ptnode (BST) Psearch=ptnode (BST) if False==searchbst (bst,key,psearch,pparent): return False parent=pparent.tnode search=psearch.tnode if None!=search.lchild a nd none!=search.rchild:tmp=search while (none!=tmp.lchild): Tmp=tmp.lchild tmp.lchild=sear Ch.lchild if None!=parent and Search==parent.lchild:parent.lchild=search.rchild Search.rchil D.parent=parent elif none!=parent and Search==parent.rchild:parent.rchild=search.rchild Sear Ch.rchild.parent=parent Elif none!=search.lchild:if None!=parent and Search==parent.lchild:parent.lchild=search.lchild Search.lchild.parent=parent elif None!=parent and Search==parent.rchild:parent.rchild=search.lch ILD search.lchild.parent=parent elif none!=search.rchild:if none!=parent and Search==parent.lchild: Parent.lchild=search.rchild search.rchild.parent=parent elif none!=parent and Search==parent. Rchild:parent.rchild=search.rchild search.rchild.parent=parent Else:parent.lchild=none Parent.rchild=none del search Search=none return true#+++++++++++++++++++++++++release operation+++++++++++ ++++++++++++++ #def Deletebst (BST): If None!=bst:if none==bst.lchild and NONE==BST.RCHILD:TMP=BST.PA Rent if Tmp!=none and Tmp.lchild==bst:tmp.lchild=none elif Tmp!=none and tmp.rchild= =bst:tmp. Rhicld=none del BST bst=none return Deletebst (bst.lchild) Deletebst (bst.rc Hild) #+++++++++++++++++++++++++print operation+++++++++++++++++++++++++ #def Printbst (BST): If None==bst:re Turn print Bst.key printbst (bst.lchild) printbst (bst.rchild) #++++++++++++++++++++++++++test operation++++++++++++ ++++++++++++++ #if __name__== ' __main__ ': Bst=tnode (0,none,none,none) Pparent=ptnode (BST) Psearch=ptnode (BST) fo R i in range (1,4): If False==insertnode (bst,i,psearch.tnode): print ' Insert number%d Failed. '% ( i) print ' Print BST: ' Printbst (BST) if True==searchbst (bst,1,psearch,pparent): print ' Find 1 in BST sucess .' If True==deletenode (bst,1): print ' Delete 1 Success. ' If False==searchbst (bst,1,psearch,pparent): print ' Find 1 in BST Failed. ' Deletebst (BST) print ' Done '
4. Finding complexity of two-fork sorting tree
The worst complication is O (n), preferably O (1)
Reference: Data structure (c language version) Min Wu Weimin
Binary sort tree (c and Python are implemented separately)