Skip to content

Commit

Permalink
Add or Fix readme and documentation on the data structure section, fi…
Browse files Browse the repository at this point in the history
…xing issue shhossain#1
  • Loading branch information
Brandonawan committed Mar 11, 2023
1 parent e94fe1f commit fd6223b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 79 deletions.
10 changes: 5 additions & 5 deletions Data Structures/Stack.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
## Stack
A stack is a data structure that stores a collection of data values in a LIFO (last in, first out) order.\
A stack operations can only occur through one of its end, referred as the _top_ of the stack.\
Stack operations can only occur through one of its ends, referred to as the _top_ of the stack.\
The implementation could be an array-like (static and contiguous) or a linked-list-like (dynamic and dis-contiguous).
### Stack's Common Operations
#### Push
Add item to the top of the stack.
Add an item to the top of the stack.
##### Example
```
let stack = [4, 1, 0, 2];
stack.push(5);
// stack is now [5, 4, 1, 0, 2]
```
#### Pop
Remove item from the top of the stack.
Remove the item from the top of the stack.
##### Example
```
let stack = [4, 1, 0, 2];
Expand All @@ -21,11 +21,11 @@ let top = stack.pop();
// stack is now [1, 0, 2]
```
#### Peek
Some programming languages provides `peek` method to allow checking the value of the current top without removing the value from the stack.
Some programming languages provide a `peek` method to allow checking the value of the current top without removing the value from the stack.
##### Example
```
let stack = [4, 1, 0, 2];
let currentHead = stack.peek();
// currentHead is 4
// current head is 4
// stack remains [4, 1, 0, 2]
```
68 changes: 34 additions & 34 deletions Data Structures/Tree.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
## Tree

A tree is non-linear and a hierarchical data structure consisting of a collection of nodes such that each node of the tree stores a value and a list of references to other nodes (the “children”).
A tree is non-linear and has a hierarchical data structure consisting of a collection of nodes such that each node of the tree stores a value and a list of references to other nodes (the “children”).

### Example

```
Let array of numbers be [100, 7, 2, 17, 3, 25, 1, 36, 19]
Let the array of numbers be [100, 7, 2, 17, 3, 25, 1, 36, 19]
// The tree is a specialized method to organize and store data in the computer to be used more effectively
A tree representation of the array would look like this:
100
Expand All @@ -24,12 +24,12 @@ A tree representation of the array would look like this:

## Binary Search Tree

A BST (Binary Search Tree) also known as ordered or sorted binary tree is a rooted binary tree data structure with the key of each internal node being greater than all the keys in the respective node's left subtree and less than the ones in its right subtree
A BST (Binary Search Tree) also known as an ordered or sorted binary tree is a rooted binary tree data structure with the key of each internal node being greater than all the keys in the respective node's left subtree and less than the ones in its right subtree

### Example

```
Let array of numbers be [8, 13, 14, 6, 7, 4, 10, 1, 3]
Let the array of numbers be [8, 13, 14, 6, 7, 4, 10, 1, 3]
A BST representation of these numbers would look like this:
8
/ \
Expand All @@ -42,21 +42,21 @@ A BST representation of these numbers would look like this:

## AVL Tree

AVL Tree is invented by GM Adelson - Velsky and EM Landis in 1962. The tree is named AVL in honour of its inventors.
AVL Tree is invented by GM Adelson - Velsky and EM Landis in 1962. The tree is named AVL in honor of its inventors.

AVL Tree can be defined as height balanced binary search tree in which each node is associated with a balance factor which is calculated by subtracting the height of its right sub-tree from that of its left sub-tree.
AVL Tree can be defined as a height-balanced binary search tree in which each node is associated with a balance factor which is calculated by subtracting the height of its right sub-tree from that of its left sub-tree.

Tree is said to be balanced if balance factor of each node is in between -1 to 1, otherwise, the tree will be unbalanced and need to be balanced.
The tree is said to be balanced if the balance factor of each node is between -1 to 1, otherwise, the tree will be unbalanced and need to be balanced.
Balance Factor (k) = height (left(k)) - height (right(k))

If balance factor of any node is 1, it means that the left sub-tree is one level higher than the right sub-tree.
If balance factor of any node is 0, it means that the left sub-tree and right sub-tree contain equal height.
If balance factor of any node is -1, it means that the left sub-tree is one level lower than the right sub-tree.
If the balance factor of any node is 1, it means that the left sub-tree is one level higher than the right sub-tree.
If the balance factor of any node is 0, it means that the left sub-tree and right sub-tree contain equal height.
If the balance factor of any node is -1, it means that the left sub-tree is one level lower than the right sub-tree.

### Example

```
Let array of numbers be [8, 3, 10, 1, 6, 5, 14, 4, 7, 13]
Let the array of numbers be [8, 3, 10, 1, 6, 5, 14, 4, 7, 13]
Tree representation
8
/ \
Expand All @@ -83,19 +83,19 @@ Balance Factor (8) = 1

A Red Black Tree is a category of the self-balancing binary search tree. It was created in 1972 by Rudolf Bayer who termed them "symmetric binary B-trees."

A red-black tree is a Binary tree where a particular node has color as an extra attribute, either red or black. By check the node colors on any simple path from the root to a leaf, red-black trees secure that no such path is higher than twice as long as any other so that the tree is generally balanced.
A red-black tree is a Binary tree where a particular node has color as an extra attribute, either red or black. By checking the node colors on any simple path from the root to a leaf, red-black trees secure that no such path is higher than twice as long as any other so that the tree is generally balanced.

Properties of Red-Black Trees
A red-black tree must satisfy these properties:

1. The root is always black.
2. A nil is recognized to be black. This factor that every non-NIL node has two children.
2. A nil is recognized to be black. This factor is that every non-NIL node has two children.
3. Black Children Rule: The children of any red node are black.
4. Black Height Rule: For particular node v, there exists an integer bh (v) such that specific downward path from v to a nil has correctly bh (v) black real (i.e. non-nil) nodes. Call this portion the black height of v. We determine the black height of an RB tree to be the black height of its root.
4. Black Height Rule: For particular node v, there exists an integer bh (v) such that a specific downward path from v to nil has correctly bh (v) black real (i.e. non-nil) nodes. Call this portion the black height of v. We determine the black height of an RB tree to be the black height of its root.

A tree T is an almost red-black tree (ARB tree) if the root is red, but other conditions above hold.

Let array of numbers be [8, 3, 10, 1, 6, 5, 14, 13, 12]
Let the array of numbers be [8, 3, 10, 1, 6, 5, 14, 13, 12]
Tree representation
8
/ \
Expand All @@ -114,34 +114,34 @@ Color(5) = B
Color(14) = B
Color(13) = R
Color(12) = R
Here R denotes red color and B denotes black color.
Each leaf node is having two child nodes with black color and are considered as nil.
Here R denotes a red color and B denotes black color.
Each leaf node is having two child nodes with black color and is considered nil.

## Fenwick Tree

Fenwick tree, also known as binary indexed tree is a data structure that can efficiently update elements and calculate prefix sums in a table of numbers
Fenwick tree, also known as a binary indexed tree is a data structure that can efficiently update elements and calculate prefix sums in a table of numbers

### Example

```
If you have an array of numbers [5, 2, 9, -3, 5, 20, 10, -7, 2, 3]
A fenwick tree representation would look like this:
A Fenwick tree representation would look like this:
Value: 5 7 9 13 5 25 10 41 2 5
Index: 1 2 3 4 5 6 7 8 9 10 (starting from 1)
Let this fenwick tree be T
Let this Fenwick tree be T
```

### Summing in a Fenwick Tree
```
Suppose you were to do a RSQ(Ranged Query Sum) from index 1 to 7 so basically RSQ(1, 7)
Normally you would sum from index 1 to 7 of a normal array: 5 + 2 + 9 + (-3) + 5 + 20 + 10 = 48
For a Fenwick Tree, you would take the binary equivalent of 7 which is 0111, then you would go right to left of 0111 and switch the 1s to 0s and sum all of those index, you only stop when either the index is lower than the lower end of the RSQ or the binary becomes all 0s
For a Fenwick Tree, you would take the binary equivalent of 7 which is 0111, then you would go right to left of 0111 and switch the 1s to 0s and sum all of those indexes, you only stop when either the index is lower than the lower end of the RSQ or the binary becomes all 0s
The sum using Fenwick Tree would look like this: T(0111) + T(0110) + T(0100) + T(0000) = 10 + 25 + 13 = 48
```

### Update Value in Fenwick Tree
```
Suppose we were to add 10 to the value at index 4, the fenwick tree has to update since it is possible that further indexes are dependent on index 4
Suppose we were to add 10 to the value at index 4, the Fenwick tree has to update since further indexes may be dependent on index 4
To change the value at index 4, we would have to update the value at further indexes
Take the binary equivalent of index 4, 0100, now you traverse the binary from left to right starting from the most left 1 and switch the bit from 0 to 1 and the rest of the bit becomes 0
The operations would look something like this:
Expand All @@ -150,17 +150,17 @@ T[1000] + 10 = 41 + 10 = 51
T[10000] is out of bounds as 10000 would be 16 and the maximum index of T is 10
```
## Splay Tree
Splay trees are the self-balancing or self-adjusted binary search trees. In other words, we can say that the splay trees are the variants of the binary search trees
Splay trees are self-balancing or self-adjusted binary search trees. In other words, we can say that the splay trees are the variants of the binary search trees

A splay tree contains the same operations as a Binary search tree, i.e., Insertion, deletion and searching, but it also contains one more operation, i.e., splaying. So. all the operations in the splay tree are followed by splaying.
A splay tree contains the same operations as a Binary search tree, i.e., Insertion, deletion, and searching, but it also contains one more operation, i.e., splaying. So. all the operations in the splay tree are followed by splaying.

### Properties
1. Follow properties of binary search trees.
2. Self-balancing.
3. Recently accessed elements are quick to access again.
1. Follow the properties of binary search trees.
2. Self-balancing.
3. Recently accessed elements are quick to access again.

### Advantages of Splay Tree
1. In the splay tree, we do not need to store the extra information. In contrast, in AVL trees, we need to store the balance factor of each node that requires extra space, and Red-Black trees also require to store one extra bit of information that denotes the color of the node, either Red or Black.
1. In the splay tree, we do not need to store the extra information. In contrast, in AVL trees, we need to store the balance factor of each node that requires extra space, and Red-Black trees also require storing one extra bit of information that denotes the color of the node, either Red or Black.
2. It is the fastest type of Binary Search tree for various practical applications. It is used in Windows NT and GCC compilers.

### Drawbacks of Splay Tree
Expand Down Expand Up @@ -207,7 +207,7 @@ Implementation of right.rotation(T, x)
-1 30
To search any element in the splay tree, first, we will perform the standard binary search tree operation. As 7 is less than 10 so we will come to the left of the root node. After performing the search operation, we need to perform splaying. Here splaying means that the operation that we are performing on any element should become the root node after performing some rearrangements. The rearrangement of the tree will be done through the rotations.
To search any element in the splay tree, first, we will perform the standard binary search tree operation. As 7 is less than 10 so we will come to the left of the root node. After performing the search operation, we need to perform splaying. Here splaying means that the operation that we are performing on any element should become the root node after performing some rearrangements. The rearrangement of the tree will be done through rotations.
Zig rotations: The zig rotations are used when the item to be searched is either a root node or the child of a root node (i.e., left or the right child).
Expand All @@ -217,10 +217,10 @@ Case 1: If the search item is a root node of the tree.
Case 2: If the search item is a child of the root node, then the two scenarios will be there:
If the child is a left child, the right rotation would be performed, known as a zig right rotation.
If the child is a left child, the right rotation would be performed, known as a zig-right rotation.
If the child is a right child, the left rotation would be performed, known as a zig left rotation.
In the above example, we have to search 7 element in the tree. We will follow the below steps:
In the above example, we have to search for 7 elements in the tree. We will follow the below steps:
Step 1: First, we compare 7 with a root node. As 7 is less than 10, so it is a left child of the root node.
Expand All @@ -237,9 +237,9 @@ Step 2: Once the element is found, we will perform splaying. The right rotation
30
```
### Some Real life Applications of Tree data structure
1. Store hierarchical data, like folder structure, organization structure, XML/HTML data.
2. B-Tree and B+ Tree : They are used to implement indexing in databases.
### Some Real-life Applications of Tree data structure
1. Store hierarchical data, like folder structure, organization structure, and XML/HTML data.
2. B-Tree and B+ Tree: They are used to implement indexing in databases.
3. In Computer Graphics.
4. In java virtual machine.
5. Machine learning algorithm.
Expand Down
12 changes: 6 additions & 6 deletions Data Structures/Trie.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Trie follows some property that If two strings have a common prefix then they wi
## Need for Trie Data Structure?

A Trie data structure is used for storing and retrieval of data and the same operations could be done using another data structure which is Hash Table but Trie can perform these operations more efficiently than a Hash Table.
Moreover, Trie has its own advantage over the Hash table. A Trie data structure can be used for prefix-based searching whereas a Hash table can’t be used in the same way.
Moreover, Trie has its advantage over the Hash table. A Trie data structure can be used for prefix-based searching whereas a Hash table can’t be used in the same way.

## Advantages of Trie Data Structure over a Hash Table:

Expand All @@ -20,13 +20,13 @@ The A trie data structure has the following advantages over a hash table:

## Properties of a Trie Data Structure

Now we already know that Trie has a tree-like structure. So, it is very important to know its properties.
Now we know that Trie has a tree-like structure. So, it is very important to know its properties.
Below are some important properties of the Trie data structure:

- There is one root node in each Trie.
- Each node of a Trie represents a string and each edge represents a character.
- Every node consists of hashmaps or an array of pointers, with each index representing a character and a flag to indicate if any string ends at the current node.
- Trie data structure can contain any number of characters including alphabets, numbers, and special characters. But for this article, we will discuss strings with characters a-z. Therefore, only 26 pointers need for every node, where the 0th index represents ‘a and the 25th index represents ‘z’ characters.
- Trie data structure can contain any number of characters including alphabets, numbers, and special characters. But for this article, we will discuss strings with character a-z. Therefore, only 26 pointers need for every node, where the 0th index represents ‘a and the 25th index represents ‘z’ characters.
- Each path from the root to any node represents a word or string.

## Example
Expand All @@ -37,8 +37,8 @@ Below is a simple example of Trie data structure.

## How does Trie Data Structure work?

We already know that the Trie data structure can contain any number of characters including alphabets, numbers, and special characters. But for this article, we will discuss strings with characters a-z.
Therefore, only 26 pointers need for every node, where the 0th index represents ‘a and the 25th index represents ‘z’ characters.
We already know that the Trie data structure can contain any number of characters including alphabets, numbers, and special characters. But for this article, we will discuss strings with character a-z.
Therefore, only 26 pointers need for every node, where the 0th index represents ‘a and the 25th index represents ‘z’ characters.

Any lowercase English word can start with a-z, then the next letter of the word could be a-z, the third letter of the word again could be a-z, and so on. So for storing a word,
we need to take an array (container) of size 26 and initially, all the characters are empty as there are no words and it will look as shown below.
Expand All @@ -60,7 +60,7 @@ After “n“, the 3rd character is “d“, So mark the position “d” as use

The word “any” starts with “a” and the position of “a” in the root node has already been filled. So, no need to fill it again, just move to the node ‘a‘ in Trie.
For the second character ‘n‘ we can observe that the position of ‘n’ in the ‘a’ node has already been filled. So, no need to fill it again, just move to node ‘n’ in Trie.
For the last character ‘t‘ of the word, The position for ‘t‘ in the ‘n‘ node is not filled. So, filled the position of ‘t‘ in ‘n‘ node and move to ‘t‘ node. After storing the word “and” and “any” the Trie will look like this:
For the last character ‘t‘ of the word, The position for ‘t‘ in the ‘n‘ node is not filled. So, filled the position of ‘t‘ in the ‘n‘ node and move to the ‘t‘ node. After storing the word “and” and “any” the Trie will look like this:

- After storing the word “and” and “any” the Trie will look like this:

Expand Down
Loading

0 comments on commit fd6223b

Please sign in to comment.