diff --git a/docs/index.html b/docs/index.html index 793fd71..0727d04 100644 --- a/docs/index.html +++ b/docs/index.html @@ -956,6 +956,36 @@
A tree is a non-linear abstract data type with a hierarchy-based structure. It consists of nodes (where the data is stored) that are connected via links. The tree data structure stems from a single node called a root node and has subtrees connected to the root.
+Some basic terms related to Tree Data Structure are:
+Properties of Tree Data Structure
+The tree data structure can be created by creating the nodes dynamically with the help of the pointers. The tree in the memory can be represented as shown below:
+In Python, the structure of a node can be defined as:
+
+ class Node:
+ def __init__(self, data):
+ self.data = data
+ self.left = None
+ self.right = None
+
+ The above structure can only be defined for the binary trees because the binary tree can have utmost two children, and generic trees can have more than two children. The structure of the node for generic trees would be different as compared to the binary tree.
+There are various types of trees that are used in different applications. Some of the most common types of trees are:
+Here, binary name itself suggests two numbers, i.e., 0 and 1. In a binary tree, each node in a tree can have utmost two child nodes. Here, utmost means whether the node has 0 nodes, 1 node or 2 nodes. Binary Trees are general trees in which the root node can only hold up to maximum 2 subtrees: left subtree and right subtree.
+Traversal is a process to visit all the nodes of a tree and may print their values too. Because, all nodes are connected via edges (links) we always start from the root (head) node. That is, we cannot randomly access a node in a tree. There are three ways which we use to traverse a tree:
+ +In this traversal method, the root node is visited first, then the left subtree and finally the right subtree.
+Algorithm:
+Python Code:
+def Preorder_Traversal(root):
+ if root:
+ print(root.data)
+ Preorder_Traversal(root.left)
+ Preorder_Traversal(root.right)
+
+ Example:
+In this traversal method, the root node is visited last, hence the name. First we traverse the left subtree, then the right subtree and finally the root node.
+Algorithm:
+Until all nodes are traversed- +
Python Code:
+def Postorder_Traversal(root):
+ if root:
+ Postorder_Traversal(root.left)
+ Postorder_Traversal(root.right)
+ print(root.data)
+
+ Example:
+In this traversal method, the left subtree is visited first, then the root and later the right sub-tree. We should always remember that every node may represent a subtree itself.
+Algorithm:
+Python Code:
+def Inorder_Traversal(root):
+ if root:
+ Inorder_Traversal(root.left)
+ print(root.data)
+ Inorder_Traversal(root.right)
+
+ Example:
+A binary search tree follows some order to arrange the elements. In a Binary search tree, the value of left node must be smaller than the parent node, and the value of right node must be greater than the parent node. This rule is applied recursively to the left and right subtrees of the root.
+Searching means to find or locate a specific element or node in a data structure. In Binary search tree, searching a node is easy because elements in BST are stored in a specific order. The steps of searching a node in Binary Search tree are listed as follows
+Example:
+Suppose we have to find a number n in the above binary tree. So first we will compare n with the root of the tree. If the n is equal to the root then return the node's location. If n is smaller than the root, search in the left subtree or if n is larger than the root, search in the right subtree. Search recursively in the subtree until the node with same data is found. If not present in the tree, return "NOT FOUND!"
+ +A new key in BST is always inserted at the leaf. To insert an element in BST, we have to start searching from the root node; if the node to be inserted is less than the root node, then search for an empty location in the left subtree. Else, search for the empty location in the right subtree and insert the data. Insert in BST is similar to searching, as we always have to maintain the rule that the left subtree is smaller than the root, and right subtree is larger than the root.
+Example:
+In a binary search tree, we must delete a node from the tree by keeping in mind that the property of BST is not violated. To delete a node from BST, there are three possible situations occur:
+The node to be deleted is the leaf node
+It is the simplest case to delete a node in BST. Here, we have to replace the leaf node with NULL and simply free the allocated space.
+The node to be deleted has only one child
+In this case, we have to replace the target node with its child, and then delete the child node. It means that after replacing the target node with its child node, the child node will now contain the value to be deleted. So, we simply have to replace the child node with NULL and free up the allocated space.
+The node to be deleted has two children
+This case of deleting a node in BST is a bit complex among other two cases. In such a case, the steps to be followed are listed as follows:
+1. Time Complexity
+Operations | +Best case time complexity | +Average case time complexity | +Worst case time complexity | +
---|---|---|---|
Insertion | +O(log n) | +O(log n) | +O(n) | +
Deletion | +O(log n) | +O(log n) | +O(n) | +
Searching | +O(log n) | +O(log n) | +O(n) | +
2. Space Complexity
+Operations | +Space complexity | +
---|---|
Insertion | +O(n) | +
Deletion | +O(n) | +
Searching | +O(n) | +
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.
+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.
+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.
+Importance of AVL Tree
+AVL tree controls the height of the binary search tree by not letting it to be skewed. The time taken for all operations in a binary search tree of height h is O(h). However, it can be extended to O(n) if the BST becomes skewed (i.e. worst case). By limiting this height to log n, AVL tree imposes an upper bound on each operation to be O(log n) where n is the number of nodes.
+1. Insertion in AVL Tree
+Insertion in AVL tree is performed in the same way as it is performed in a binary search tree. However, it may lead to violation in the AVL tree property and therefore the tree may need balancing. The tree can be balanced by applying rotations.
+1. Deletion in AVL Tree
+Deletion can also be performed in the same way as it is performed in a binary search tree. Deletion may also disturb the balance of the tree therefore, various types of rotations are used to rebalance the tree.
+Complexity of Operations
+Algorithm | +Average case | +Worst case | +
---|---|---|
Space Complexity | +O(n) | +O(n) | +
Search | +O(log n) | +O(log n) | +
Insert | +O(log n) | +O(log n) | +
Delete | +O(log n) | +O(log n) | +
Using Insertion and Deletion Operation may disturb the balance of the Tree, therefore we use different AVL Rotations to restore the balance of the Tree
+The four types of AVL Rotations are:
+1. LL Rotation
+When BST becomes unbalanced, due to a node is inserted into the left subtree of the left subtree of C, then we perform LL rotation, LL rotation is clockwise rotation, which is applied on the edge below a node having balance factor 2.
+2. RR Rotation
+When BST becomes unbalanced, due to a node is inserted into the right subtree of the right subtree of A, then we perform RR rotation, RR rotation is an anticlockwise rotation, which is applied on the edge below a node having balance factor -2
+3. LR Rotation
+Double rotations are bit tougher than single rotation which has already explained above. LR rotation = RR rotation + LL rotation, i.e., first RR rotation is performed on subtree and then LL rotation is performed on full tree, by full tree we mean the first node from the path of inserted node whose balance factor is other than -1, 0, or 1.
+4. RL Rotation
+Double rotations are bit tougher than single rotation which has already explained above. R L rotation = LL rotation + RR rotation, i.e., first LL rotation is performed on subtree and then RR rotation is performed on full tree, by full tree we mean the first node from the path of inserted node whose balance factor is other than -1, 0, or 1.
+The red-Black tree is a binary search tree. The prerequisite of the red-black tree is that we should know about the binary search tree. In a binary search tree, the values of the nodes in the left subtree should be less than the value of the root node, and the values of the nodes in the right subtree should be greater than the value of the root node.
+Each node in the Red-black tree contains an extra bit that represents a color to ensure that the tree is balanced during any operations performed on the tree like insertion, deletion, etc. In a binary search tree, the searching, insertion and deletion take O(log2n) time in the average case, O(1) in the best case and O(n) in the worst case.
+Properties of Red-Black tree
+B Tree is a specialized m-way tree that can be widely used for disk access. A B-Tree of order m can have at most m-1 keys and m children. One of the main reason of using B tree is its capability to store large number of keys in a single node and large key values by keeping the height of the tree relatively small.
+A B tree of order m contains all the properties of an M way tree. In addition, it contains the following properties:
+B tree is used to index the data and provides fast access to the actual data stored on the disks since, the access to value stored in a large database that is stored on a disk is a very time consuming process.
+Searching an un-indexed and unsorted database containing n key values needs O(n) running time in worst case. However, if we use B Tree to index this database, it will be searched in O(log n) time in worst case.
+B+ Tree is an extension of B Tree which allows efficient insertion, deletion and search operations.
+In B Tree, Keys and records both can be stored in the internal as well as leaf nodes. Whereas, in B+ tree, records (data) can only be stored on the leaf nodes while internal nodes can only store the key values.
+The leaf nodes of a B+ tree are linked together in the form of a singly linked lists to make the search queries more efficient.
+B+ Tree are used to store the large amount of data which can not be stored in the main memory. Due to the fact that, size of main memory is always limited, the internal nodes (keys to access records) of the B+ tree are stored in the main memory whereas, leaf nodes are stored in the secondary memory.
+The operations supported in B+ trees are Insertion, deletion and searching with the time complexity of O(log n) for every operation.
+They are almost similar to the B tree operations as the base idea to store data in both data structures is same. However, the difference occurs as the data is stored only in the leaf nodes of a B+ trees, unlike B trees.
+