Added augumented trees in binary trees guide (#21530)
This commit is contained in:
committed by
nik
parent
f0640891a6
commit
eb4c305722
@ -287,3 +287,18 @@ Complete Binary Tree: A Binary Tree is complete Binary Tree if all levels are co
|
|||||||
40 50 100 40
|
40 50 100 40
|
||||||
/ \ /
|
/ \ /
|
||||||
8 7 9
|
8 7 9
|
||||||
|
|
||||||
|
### Augumenting a BST
|
||||||
|
|
||||||
|
Sometimes we need to store some additional information with the traditional data structures to make our tasks easier.
|
||||||
|
For example, consider a scenario where you are supposed to find the ith smallest number in a set. You can use brute force here but we can reduce the complexity of the problem to O(lg n) by augumenting a red-black or any self-balancing tree (where n is the number of elements in the set). We can also compute rank of any element in O(lg n) time. Let us consider a case where we are augumenting a red-black tree to store the addional information needed. Besides the usual attributes, we can store number of internal nodes in the subtree rooted at x(size of the subtree rooted at x including the node itself).
|
||||||
|
Let x be any arbitary node of a tree.
|
||||||
|
|
||||||
|
x.size = x.left.size + x.right.size + 1
|
||||||
|
|
||||||
|
While augumenting the tree, we should keep in mind, that we should be able to maintain the augumented information as well as do other operations like insertion, deletion, updation in O(lg n) time.
|
||||||
|
|
||||||
|
Since, we know that the value of x.left.size will give us the number of nodes which preceed x in the inorder traversal of the tree. Thus, x.left.size + 1 is the rank of x within the subtree rooted at x.
|
||||||
|
|
||||||
|
### To read more about Augumented Search Trees
|
||||||
|
* [Augumented Search Trees](https://www.bowdoin.edu/~ltoma/teaching/cs231/fall10/Lectures/10-augmentedTrees/augtrees.pdf)
|
||||||
|
Reference in New Issue
Block a user