Directory
-
- Introduction to the Stretching tree
- c implementation of the stretching tree
- 1 node definition
- 2 rotation
- 3 Stretching the stretch of the tree
- 4 Search
- 4 insertion and deletion of stretching trees
- All code and references
1. Introduction to the stretching tree
The stretch tree (splay tree) is a search for binary trees that can be o(lo g n) To complete the insert, find, and delete operations within the
(1) The extension tree satisfies the nature of the search binary tree, the left child node is small root node, the right child node is greater than or equal to the root node.
(2) Stretching tree unique features: When a node is accessed, the extension tree rotates to make the node the root of the tree.
The starting point of the stretching tree is this: Taking into account the principle of locality (the next greater likelihood of the content being accessed will still be accessed) and the 28 principle (80% of the time will only access 20% of the nodes), in order to make the whole lookup time smaller, Nodes that are found to be highly frequent should be placed close to the root.
The following scenario is proposed: The tree is reconstructed after each lookup, moving the found node closer to the root. Stretching tree came into being, it is a self-adjusting form of the two-fork search tree, it will follow the path from a node to a tree root, through a series of rotations to move the node to the root.
So the relative "binary search tree" and "AVL Tree" focus on how to rotate the extension tree
2. C implementation of the stretching tree
The realization of the following extension tree is derived from the root cuttings method of the binary search tree (first inserting the node into the leaf and then recursively rotating to the root), we will rotate the found node to the root, which is equivalent to inserting the found node into the root:
2.1 Node Definition
typedef SplayTreeNode* SplayTree;struct SplayTreeNode { Item key; SplayTree left; SplayTree right;};
The stretch tree does not need to record extra values (such as the height of the AVL) to maintain the tree's information, saving memory.
2.2 Rotation
Two basic rotations are introduced: left-and right-handed
- when the found node is on the left subtree of the root node, the root is the axis, right, and the node is promoted to the root
//右旋--k2是根,k1是k2的左子树,将k1旋转成根 -- 以k2为原点向右旋SplayTree rotR(SplayTree k2) { = k2->left; k2->= k1->right; k1->= k2; return k1;}
- When the found node is on the right subtree of the root node, the node is promoted to the root with the root as the axis, the left hand
//左旋---k2是根,k1是k2的右子树,(k1的右子树非空)将k1旋转成根 -- 以k2为原点向左旋SplayTree rotL(SplayTree k2) { = k2->right; k2->= k1->left; k1->= k2; return k1;}
2.3 Stretching the stretch of the tree
< note > The algorithm is implemented without rigorous verification and self-creation;
If you have questions, refer to the classic algorithm:
Stretching tree (i) text parsing and C language implementation: http://www.cnblogs.com/skywang12345/p/3604238.html
Set the lookup node to x when finding the precursor node for x:
(1) x at the left of the current root, then right-hand, will and X close to the node up one step;
(2) X on the right side of the current root, then left, and X close to the node upward one step;
(3) The value of x equals the value of the current root, finds the end, and completes the rotation in the above two-step recursion process;
The lookup ends when the precursor node does not exist.
The recursive implementation process is bottom-up , when the lookup node hits, the parent node is rotated to the axis, the lookup node is lifted to the root; recursively, each step is rotated once on the path of the root and the lookup node , up to the original root.
//stretching: the node corresponding to the key is stretched to the root and returned to the root nodeSplaytree splay (splaytree tree, Item key) {if(Tree== NULL)returnTreeif(Key==Tree -Key//Hit returnTreeif(Key<Tree -Key) {//Left if(Tree -Left== NULL)returnTree Tree -Left=Splay (tree -Left, key); Tree=ROTR (tree); }Else{//Right if(Tree -Right== NULL)returnTree Tree -Right=Splay (tree -Right, key); Tree=Rotl (tree); }returnTree;}
2.4 Search
SplayTree SplayTreeSearch(SplayTree tree, Item key){ if==NULL|| tree->== key) return tree; if< tree->key) return SplayTreeSearch(tree->left, key); else return SplayTreeSearch(tree->right, key);}
2.4 Insertion and deletion of stretching trees
(1) Insert: and search binary tree insert same, omit
(2) Delete: Delete the node that has the key value in the extension tree.
First, look for the node in the stretch tree that has the key value: if it is not found, it is returned directly; if found, the node is rotated to the root node, then the node is deleted, and then the two subtree of the node is connected (the node selected by the root node and key adjacent );
/ * Delete the node with key value in the extension tree * parameter description: * Tree: Root node * key: Key value of the node to be deleted * Return: * root node * *Splaytree splaytreedelete (splaytree tree, Item key) {Splaytree X= NULL;if(Tree== NULL)returnTree Tree=Splay (tree, key);if(Tree== NULL)returnTreeif(Tree -Left!= NULL) {//The left side of the root, the node adjacent to key is rotated to rootX=Splay (tree -Left, key); X -Right=Tree -Right }Else{//tree->left = = NULLX=Tree -Right } Delete tree;returnx;}
3. All codes and references
#include <Stdio.H>#include <Stdlib.H>#define MAX(A, B) ((A>B?A:B) typedef int ITEM;TYPEDEF struct Splaytreenode splaytreenode;typedef splaytreenode*Splaytree;struct Splaytreenode {Item key; Splaytree left; Splaytree right;}; static int G_error= 0;//Error CodesSplaytree NewNode (Item key, Splaytree left, Splaytree right) {Splaytree x=(splaytree) malloc (sizeof (*x));if(x== NULL) {G_error= 1; Exit-1); } x -Key=Key X -Left=Left X -Right=Rightreturnx;} Splaytree Splaytreeinit () {returnNewNode (Ten,NULL,NULL); }//L----K2 is the root, K1 is the right sub-tree of K2, (K1 right subtree non-empty) will K1 to the root-the K2 as the Origin of the leftSplaytree Rotl (splaytree K2) {Splaytree K1=K2 -Right K2 -Right=K1 -Left K1 -Left=K2;returnK1;}//Right---K2 is the root, K1 is K2 's left subtree, the K1 is rotated to root--with the K2 as the origin point to the rightSplaytree ROTR (splaytree K2) {Splaytree K1=K2 -Left K2 -Left=K1 -Right K1 -Right=K2;returnK1;} Splaytree splay (splaytree tree, Item key) {if(Tree== NULL)returnTreeif(Key==Tree -KeyreturnTreeif(Key<Tree -Key) {//Left if(Tree -Left== NULL)returnTree Tree -Left=Splay (tree -Left, key); Tree=ROTR (tree); }Else{//Right if(Tree -Right== NULL)returnTree Tree -Right=Splay (tree -Right, key); Tree=Rotl (tree); }returnTree;} Splaytree Splaytreesearch (splaytree tree, Item key) {if(Tree== NULL ||Tree -Key==KeyreturnTreeif(Key<Tree -KeyreturnSplaytreesearch (tree -Left, key);Else returnSplaytreesearch (tree -Right, key);} Splaytree Splaytreeinsert (splaytree tree, Item key) {if(Tree== NULL)returnNewNode (Key,NULL,NULL);if(Key<Tree -Key) tree -Left=Splaytreeinsert (tree -Left, key);ElseTree -Right=Splaytreeinsert (tree -Right, key);returnTree;}voidTraversal (Splaytree tree) {if(Tree== NULL) {printf ("Nil\t");return; } printf ("%d\t", tree -Key); Traversal (tree -left); Traversal (tree -right);return;} Splaytree splaytreedelete (splaytree tree, Item key) {Splaytree X= NULL;if(Tree== NULL)returnTree Tree=Splay (tree, key);if(Tree== NULL)returnTreeif(Tree -Left!= NULL) {X=Splay (tree -Left, key); X -Right=Tree -Right }Else{//tree->left = = NULLX=Tree -Right } Delete tree;returnx;} int main () {Splaytree Splay_tree= NULL;//for (int i = 0; i <; i++) { //int key = rand ()%100; //Splay_tree = Splaytreeinsert (Splay_tree, key); //printf ("%d\t", key); //}Splay_tree=Splaytreeinsert (Splay_tree,1); Splay_tree=Splaytreeinsert (Splay_tree,5); Splay_tree=Splaytreeinsert (Splay_tree,4); Splay_tree=Splaytreeinsert (Splay_tree,2); Splay_tree=Splaytreeinsert (Splay_tree,6); printf"\ntraversal\n"); Traversal (Splay_tree); Splay_tree=Splay (Splay_tree,6); Splay_tree=Splaytreedelete (Splay_tree,4); printf"\ndeleted traversal\n"); Traversal (Splay_tree); GetChar ();}
Resources:
Stretching tree-Wikipedia: https://zh.wikipedia.org/wiki/%E4%BC%B8%E5%B1%95%E6%A0%91
Stretching tree (i) text parsing and C language implementation: http://www.cnblogs.com/skywang12345/p/3604238.html
Implementation of Binary search tree: http://blog.csdn.net/quzhongxin/article/details/45038399
Stretching tree-Two forks search tree Extension 2