diff --git a/src/data-structures/stack/Stack.js b/src/data-structures/stack/Stack.js index 0435adec..8d061412 100644 --- a/src/data-structures/stack/Stack.js +++ b/src/data-structures/stack/Stack.js @@ -1,67 +1,67 @@ -// import LinkedList from '../linked-list/LinkedList'; +import LinkedList from '../linked-list/LinkedList'; -// export default class Stack { -// constructor() { -// // We're going to implement Stack based on LinkedList since these -// // structures are quite similar. Compare push/pop operations of the Stack -// // with prepend/deleteHead operations of LinkedList. -// this.linkedList = new LinkedList(); -// } +export default class Stack { + constructor() { + // We're going to implement Stack based on LinkedList since these + // structures are quite similar. Compare push/pop operations of the Stack + // with prepend/deleteHead operations of LinkedList. + this.linkedList = new LinkedList(); + } -// /** -// * @return {boolean} -// */ -// isEmpty() { -// // The stack is empty if its linked list doesn't have a head. -// return !this.linkedList.head; -// } + /** + * @return {boolean} + */ + isEmpty() { + // The stack is empty if its linked list doesn't have a head. + return !this.linkedList.head; + } -// /** -// * @return {*} -// */ -// peek() { -// if (this.isEmpty()) { -// // If the linked list is empty then there is nothing to peek from. -// return null; -// } + /** + * @return {*} + */ + peek() { + if (this.isEmpty()) { + // If the linked list is empty then there is nothing to peek from. + return null; + } -// // Just read the value from the start of linked list without deleting it. -// return this.linkedList.head.value; -// } + // Just read the value from the start of linked list without deleting it. + return this.linkedList.head.value; + } -// /** -// * @param {*} value -// */ -// push(value) { -// // Pushing means to lay the value on top of the stack. Therefore let's just add -// // the new value at the start of the linked list. -// this.linkedList.prepend(value); -// } + /** + * @param {*} value + */ + push(value) { + // Pushing means to lay the value on top of the stack. Therefore let's just add + // the new value at the start of the linked list. + this.linkedList.prepend(value); + } -// /** -// * @return {*} -// */ -// pop() { -// // Let's try to delete the first node (the head) from the linked list. -// // If there is no head (the linked list is empty) just return null. -// const removedHead = this.linkedList.deleteHead(); -// return removedHead ? removedHead.value : null; -// } + /** + * @return {*} + */ + pop() { + // Let's try to delete the first node (the head) from the linked list. + // If there is no head (the linked list is empty) just return null. + const removedHead = this.linkedList.deleteHead(); + return removedHead ? removedHead.value : null; + } -// /** -// * @return {*[]} -// */ -// toArray() { -// return this.linkedList -// .toArray() -// .map((linkedListNode) => linkedListNode.value); -// } + /** + * @return {*[]} + */ + toArray() { + return this.linkedList + .toArray() + .map((linkedListNode) => linkedListNode.value); + } -// /** -// * @param {function} [callback] -// * @return {string} -// */ -// toString(callback) { -// return this.linkedList.toString(callback); -// } -// } + /** + * @param {function} [callback] + * @return {string} + */ + toString(callback) { + return this.linkedList.toString(callback); + } +} diff --git a/src/data-structures/tree/splay-tree/README.md b/src/data-structures/tree/splay-tree/README.md new file mode 100644 index 00000000..4057b210 --- /dev/null +++ b/src/data-structures/tree/splay-tree/README.md @@ -0,0 +1,107 @@ +# Splay Tree + +A **splay tree** is a binary search tree with the additional property that recently accessed elements are quick to access again. Like self-balancing binary search trees, a splay tree performs basic operations such as insertion, look-up and removal in **O(log n)** amortized time. For many sequences of non-random operations, splay trees perform better than other search trees, even performing better than **O(log n)** for sufficiently non-random patterns, all without requiring advance knowledge of the pattern. The splay tree was invented by Daniel Sleator and Robert Tarjan in 1985. + +All normal operations on a binary search tree are combined with one basic operation, called splaying. **Splaying** the tree for a certain element rearranges the tree so that the element is placed at the root of the tree. One way to do this with the basic search operation is to first perform a standard binary tree search for the element in question, and then use tree rotations in a specific fashion to bring the element to the top. Alternatively, a top-down algorithm can combine the search and the tree reorganization into a single phase. + + +![Splay Tree](https://study.com/cimages/multimages/16/splay_trees_graphic.png) + +## Applications + +1. A good example is a network router. A network router receives network packets at a high rate from incoming connections and must quickly decide on which outgoing wire to send each packet, based on the IP address in the packet. The router needs a big table (a map) that can be used to look up an IP address and find out which outgoing connection to use. If an IP address has been used once, it is likely to be used again, perhaps many times. So we can bulid the above look up table using splay tree. if we have look up for a ip address once it will come on top of tree, so next time that ip address come again ,we can find its entry on top and don’t have to go till bottom. + +2. Splay trees are typically used in the implementation of caches, memory allocators, garbage collectors, data compression, ropes (replacement of string used for long text strings), in Windows NT (in the virtual memory, networking, and file system code) etc. + +3. a example of a domain in which splay trees are used are Intrusion detection systems (IDS)which are an essential part of the security infrastructure. They are used to detect, identify and stop intruders. The administrators can rely on them to find out successful attacks and prevent a future use of known exploits. IDS are also considered as a complementary solution to firewall technology by recognizing attacks against the network that are missed by the firewall. Nevertheless, IDS are plagued with false positive alerts, letting security professionals to be overwhelmed by the analysis tasks. Therefore, IDS employ several techniques in order to increase the detection probability of suspect threats while reducing the risk of false positives. This leads to the construction of a decision tree such as the splay tree used to verify the traffic. The proposed strategy to detect intrusions using protocol analysis consists in extracting data from each received packet and then traversing a pre-built decision tree. + +4. Splay tree are used in the gcc compiler and GNU C++ library, the sed string editor,the most popular implementation of Unix malloc, Linux loadable kernel modules, and in much other software. + + +## Operations + +## Splaying + +When a node x is accessed, a splay operation is performed on x to move it to the root. To perform a splay operation we carry out a sequence of splay steps, each of which moves x closer to the root. By performing a splay operation on the node of interest after every access, the recently accessed nodes are kept near the root and the tree remains roughly balanced, so that we achieve the desired amortized time bounds. + +Each particular step depends on three factors: + +- Whether x is the left or right child of its parent node, p, +- whether p is the root or not, and if not +- whether p is the left or right child of its parent, g (the grandparent of x). + +It is important to remember to set gg (the great-grandparent of x) to now point to x after any splay operation. If gg is null, then x obviously is now the root and must be updated as such. + +There are three types of splay steps, each of which has two symmetric variants: left- and right-handed. For the sake of brevity, only one of these two is shown for each type. (In the following diagrams, circles indicate nodes of interest and triangles indicate sub-trees of arbitrary size.) The three types of splay steps are: + +Zig step: this step is done when p is the root. The tree is rotated on the edge between x and p. Zig steps exist to deal with the parity issue, will be done only as the last step in a splay operation, and only when x has odd depth at the beginning of the operation. + +![Splaying](https://upload.wikimedia.org/wikipedia/commons/thumb/2/2c/Splay_tree_zig.svg/350px-Splay_tree_zig.svg.png) + +**Zig-zig step:** this step is done when p is not the root and x and p are either both right children or are both left children. The picture below shows the case where x and p are both left children. The tree is rotated on the edge joining p with its parent g, then rotated on the edge joining x with p. Note that zig-zig steps are the only thing that differentiate splay trees from the rotate to root method introduced by Allen and Munro prior to the introduction of splay trees. + +![Zig - Zig Step](https://upload.wikimedia.org/wikipedia/commons/f/fd/Zigzig.gif) + +**Zig-zag step:** this step is done when p is not the root and x is a right child and p is a left child or vice versa. The tree is rotated on the edge between p and x, and then rotated on the resulting edge between x and g. + +![Zig - Zag Step](https://upload.wikimedia.org/wikipedia/commons/6/6f/Zigzag.gif) + + +## Join + +Given two trees S and T such that all elements of S are smaller than the elements of T, the following steps can be used to join them to a single tree: + +1) Splay the largest item in S. Now this item is in the root of S and has a null right child. +2) Set the right child of the new root to T. + +## Split +Given a tree and an element x, return two new trees: one containing all elements less than or equal to x and the other containing all elements greater than x. This can be done in the following way: + +- Splay x. Now it is in the root so the tree to its left contains all elements smaller than x and the tree to its + right - contains all element larger than x. +- Split the right subtree from the rest of the tree. + + +## Insertion +To insert a value x into a splay tree: + +- Insert x as with a normal binary search tree. +- when an item is inserted, a splay is performed. + +As a result, the newly inserted node x becomes the root of the tree. + +Alternatively: + +- Use the split operation to split the tree at the value of x to two sub-trees: S and T. +- Create a new tree in which x is the root, S is its left sub-tree and T its right sub-tree. + +## Deletion +To delete a node x, use the same method as with a binary search tree: + +If x has two children: + - Swap its value with that of either the rightmost node of its left sub tree (its in-order predecessor) or the + leftmost node of its right subtree (its in-order successor). + - Remove that node instead. + +In this way, deletion is reduced to the problem of removing a node with 0 or 1 children. Unlike a binary search tree, in a splay tree after deletion, we splay the parent of the removed node to the top of the tree. + +Alternatively: + +- The node to be deleted is first splayed, i.e. brought to the root of the tree and then deleted. leaves the tree with + two sub trees. +- The two sub-trees are then joined using a "join" operation. + +## Advantages +Good performance for a splay tree depends on the fact that it is self-optimizing, in that frequently accessed nodes will move nearer to the root where they can be accessed more quickly. The worst-case height—though unlikely—is O(n), with the average being O(log n). Having frequently-used nodes near the root is an advantage for many practical applications (also see locality of reference), and is particularly useful for implementing caches and garbage collection algorithms. + +Advantages include: + +- Comparable performance: Average-case performance is as efficient as other trees. +- Small memory footprint: Splay trees do not need to store any bookkeeping data. + +## Disadvantages +The most significant disadvantage of splay trees is that the height of a splay tree can be linear. For example, this will be the case after accessing all n elements in non-decreasing order. Since the height of a tree corresponds to the worst-case access time, this means that the actual cost of a single operation can be high. However the amortized access cost of this worst case is logarithmic, O(log n). Also, the expected access cost can be reduced to O(log n) by using a randomized variant. + +The representation of splay trees can change even when they are accessed in a 'read-only' manner (i.e. by find operations)This complicates the use of such splay trees in a multi-threaded environment. Specifically, extra management is needed if multiple threads are allowed to perform find operations concurrently. This also makes them unsuitable for general use in purely functional programming, although even there they can be used in limited ways to implement priority queues. + +Finally, when the access pattern is random, the additional splaying overhead adds a significant constant factor to the cost compared to less-dynamic alternatives. diff --git a/src/data-structures/tree/splay-tree/splaytree.js b/src/data-structures/tree/splay-tree/splaytree.js new file mode 100644 index 00000000..b8d539e7 --- /dev/null +++ b/src/data-structures/tree/splay-tree/splaytree.js @@ -0,0 +1,288 @@ +//Splay Tree + +// A splay tree is a binary search tree with the additional property that recently accessed elements are quick to access again. Like self-balancing binary search trees, a splay tree performs basic operations such as insertion, look-up and removal in O(log n) amortized time. For many sequences of non-random operations, splay trees perform better than other search trees, even performing better than O(log n) for sufficiently non-random patterns, all without requiring advance knowledge of the pattern. + +class Node { + constructor (key, data) { + this.key = key; + this.data = data; + this.left = null; + this.right = null; + this.parent = null; + this.cnt = 0; + } + } + // Construction of Splay Tree + export class SplayTree { + constructor (duplicate = true, comparator = (a, b) => a > b ? 1 : a < b ? -1 : 0) { + this.comparator = comparator; + this.duplicate = duplicate; + this._root = null; + this._size = 0; + } + get root() { + return this._root; + } + get size() { + return this._size; + } + + // Update a value in Tree + update (x) { + x.cnt = 1; + if (x.left) x.cnt += x.left.cnt; + if (x.right) x.cnt += x.right.cnt; + } + + // Rotate the Tree + rotate (root, x) { + let p = x.parent; + let b = null; + if (x === p.left) { + p.left = b = x.right; + x.right = p; + } else { + p.right = b = x.left; + x.left = p; + } + x.parent = p.parent; + p.parent = x; + if (b) { + b.parent = p; + } + if (x.parent) { + if (p === x.parent.left) { + x.parent.left = x; + } else { + x.parent.right = x; + } + } else { + root = x; + } + this.update(p); + this.update(x); + return root; + } + + // Splaying the Tree + splay (root, x) { + while (x.parent) { + let p = x.parent; + let g = p.parent; + if (g) this.rotate(root, (x === p.left) === (p === g.left) ? p : x); + root = this.rotate (root, x); + } + return root; + } + + // Insertion of values in Tree + insert (key, data = {}) { + let p = this._root; + let x = null; + if (!p) { + x = new Node(key, data); + this._root = x; + this._size++; + return x; + } + const TRUE = true; + while (TRUE) { + if (!this.duplicate && this.comparator(key, p.key) === 0) return x; + if (this.comparator(key, p.key) < 0) { + if (!p.left) { + x = new Node(key, data); + p.left = x; + x.parent = p; + this._root = this.splay(this._root, x); + break; + } + p = p.left; + } else { + if (!p.right) { + x = new Node(key, data); + p.right = x; + x.parent = p; + this._root = this.splay(this._root, x); + break; + } + p = p.right; + } + } + this._size++; + return x; + } + + // Merge Tree + merge (tree) { + let p = this._root; + if (!p && !tree.root) { + return null; + } + if (!tree.root) { + return p; + } + if (!p) { + this._root = tree.root; + this._size = tree.size; + return this._root; + } + const TRUE = true; + while (TRUE) { + if (this.comparator(tree.root.key, p.key) < 0) { + if (!p.left) { + p.left = tree.root; + tree.root.parent = p; + this._root = this.splay(this._root, tree.root); + break; + } + p = p.left; + } else { + if (!p.right) { + p.right = tree.root; + tree.root.parent = p; + this._root = this.splay(this._root, tree.root); + break; + } + p = p.right; + } + } + this._size += tree.size; + return tree.root; + } + + // Load Tree + load (keys, datas, nTrees = 100) { + const n = keys.length; + if (!datas || n !== datas.length) { + datas = []; + for (let i = 0; i < n; i++) { + datas.push({}); + } + } + const alloc = n / nTrees | 0; + const trees = []; + for (let i = 0; i < nTrees; i++) { + trees.push(new SplayTree()); + for (let j = 0; j < alloc; j++) { + trees[i].insert(keys[i*alloc+j], datas[i*alloc+j]); + } + this.merge(trees[i]); + } + for (let j = nTrees * alloc; j < n; j++) { + this.insert(keys[j], datas[j]); + } + } + + // Find a given key + find (key) { + let p = this._root; + if (!p) return false; + while (p) { + if (this.comparator(key, p.key) === 0) break; + if (this.comparator(key, p.key) < 0) { + if (!p.left) break; + p = p.left; + } else { + if (!p.right) break; + p = p.right; + } + } + this._root = this.splay(this._root, p); + return this.comparator(key, p.key) === 0; + } + + //Find Kth occurence of a value + findKth (k) { + if (k >= this._size) return false; + let p = this._root; + let TRUE = true; + while (TRUE) { + while (p.left && p.left.cnt > k) p = p.left; + if (p.left) k -= p.left.cnt; + if (!(k--)) break; + p = p.right; + } + this._root = this.splay(this._root, p); + return true; + } + + //Remove any value + remove (key) { + if (!this.find(key)) return false; + let p = this._root; + if (p.left) { + if (p.right) { + this._root = p.left; + this._root.parent = null; + let x = this._root; + while (x.right) x = x.right; + x.right = p.right; + p.right.parent = x; + p = null; + this._size--; + return true; + } + this._root = p.left; + this._root.parent = null; + p = null; + this._size--; + return true; + } + if (p.right) { + this._root = p.right; + this._root.parent = null; + p = null; + this._size--; + return true; + } + p = null; + this._root = null; + this._size = 0; + return true; + } + + // Clear the Tree + clear() { + this._root = null; + this._size = 0; + return this; + } + keys (order = 0) { + /* + * order 0: preOrder + * order 1: inOrder + * order 2: postOrder + */ + function _preOrder (p, ret) { + ret.push(p.key); + if (p.left) { + _preOrder(p.left, ret); + } + if (p.right) { + _preOrder(p.right, ret); + } + } + function _inOrder (p, ret) { + if (p.left) { + _inOrder(p.left, ret); + } + ret.push(p.key); + if (p.right) { + _inOrder(p.right, ret); + } + } + function _postOrder (p, ret) { + if (p.left) { + _postOrder(p.left, ret); + } + if (p.right) { + _postOrder(p.right, ret); + } + ret.push(p.key); + } + const f = [_preOrder, _inOrder, _postOrder]; + let ret = []; + f[order](this._root, ret); + return ret; + } + } \ No newline at end of file