1. Concept
Binary Search Tree Properties: Set X is a node of a two-fork search tree, then:
A) x record any node y, Y.key < X.key
b) Y.key >= X.key of any node y in the right sub-tree of X
2. Data structure
1 structTreeNode2 {3TreeNode (intkey): Left (null), Right (NULL), parent (null), key (key) {};4treenode*Left ;5treenode*Right ;6treenode*parent;7 intkey;8 };9 Ten classTree One { A Public: - Tree (): M_root (NULL) {}; -treenode* Minimum (treenode* ); thetreenode* Maximum (treenode* ); - voidCreat (intKeys[],intn); - intInsertintkey); - intRemoveintkey); +Treenode* Search (intkey); - voidWalk (TreeNode *root); +treenode* Get_root ()Const; A at protected: -Inline treenode* node_creat (intkey); - voidTransplant (treenode* x, TreeNode *y); - - Private: -treenode*M_root; in};
3. Algorithm
3.1 Traversal
By the nature of the two-fork search tree, the result of the middle sequence traversal is the ordered arrangement of keys
1 void Tree::walk (TreeNode *root)2{3 if (root = NULL)4 return; 5 Walk (root-> left); 6 " " ; 7 Walk (root-> right); 8 }
3.2 Maximum/Minimum value
1treenode* Tree::minimum (TreeNode *root)2 {3TreeNode *visit =Root;4 if(Visit = =NULL)5 returnNULL;6 while(Visit->left! =NULL)7Visit = visit->Left ;8 returnvisit;9 }Ten Onetreenode* Tree::maximum (treenode*root) A { -TreeNode *visit =Root; - if(Visit = =NULL) the returnNULL; - while(Visit->right! =NULL) -Visit = visit->Right ; - returnvisit; +}
3.3 Find
1treenode* Tree::search (intkey)2 {3treenode* visit =M_root;4 5 while(Visit! =NULL)6 {7 if(Key = = Visit->key)8 returnvisit;9 if(Key < Visit->key)TenVisit = visit->Left ; One Else AVisit = visit->Right ; - } - the returnvisit; -}
3.4 Inserting
When inserted, it must be inserted into a leaf node, not between two nodes.
Find the insertion position and insert it first.
1 intTree::insert (intkey)2 {3treenode* z =NewTreeNode (key);4treenode* x =M_root;5Treenode* y =NULL;6 while(X! =NULL)7 {8y =x;9 if(Z->key < x->key)Tenx = x->Left ; One Else Ax = x->Right ; - } -Z->parent =y; the if(y = = NULL)//Tree is empty -M_root =Z; - Else if(Z->key < y->key) -Y->left =Z; + Else -Y->right =Z; + A return 0; at}
3.5 Delete
When deleted, the situation is more complex, for node Z to be deleted:
IF Z.left is NULL
Replace Z by Z.right
ELSE IF Z.right is NULL
Replace Z by Z.left
ELSE//Z contains left and right subtrees
Find the successor Y of Z, which is the smallest node with a key greater than Z.key, Y is in the right subtree of Z and y is not Zuozi
IF Y.parent is Z
Replace z by y, leaving Y.right
ELSE
Replace Z.right by Y,
Replace Z by y
1 //replace x by node y2 voidTree::transplant (treenode* x, TreeNode *y)3 {4 if(x->parent = = NULL)//x is root5M_root =y;6 Else if(X->parent->left = =x)7X->parent->left =y;8 Else9X->parent->right =y;Ten One if(Y! =NULL) AY->parent = x->parent; - } - the intTree::remove (intkey) - { -Treenode* y =NULL; -treenode* z =search (key); + if(Z = =NULL) - return-1; + A if(Z->left = =NULL) atTransplant (Z, z->Right ); - Else if(Z->right = =NULL) -Transplant (Z, z->Left ); - Else - { -y = minimum (z->Right ); in if(Y->parent! =z) - { toTransplant (y, y->right);//Replace y by right child + //Let Y is the parent of Z->right -Y->right = z->Right ; theY->right->parent =y; * } $ transplant (z, y);Panax NotoginsengY->left = z->Left ; -Y->left->parent =y; the } + A return 0; the}
Tips: Source code
Introduction to Algorithms Part3: two-fork search tree